Annotation of src/usr.bin/cvs/update.c, Revision 1.66
1.66 ! joris 1: /* $OpenBSD: update.c,v 1.65 2006/05/28 01:24:28 joris Exp $ */
1.1 jfb 2: /*
1.59 joris 3: * Copyright (c) 2006 Joris Vink <joris@openbsd.org>
1.1 jfb 4: *
1.59 joris 5: * Permission to use, copy, modify, and distribute this software for any
6: * purpose with or without fee is hereby granted, provided that the above
7: * copyright notice and this permission notice appear in all copies.
1.1 jfb 8: *
1.59 joris 9: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1 jfb 16: */
17:
1.51 xsa 18: #include "includes.h"
1.1 jfb 19:
20: #include "cvs.h"
21: #include "log.h"
1.59 joris 22: #include "diff.h"
1.4 jfb 23: #include "proto.h"
1.1 jfb 24:
1.59 joris 25: int cvs_update(int, char **);
1.60 joris 26: int prune_dirs = 0;
1.63 joris 27: int build_dirs = 0;
28:
1.65 joris 29: static void update_clear_conflict(struct cvs_file *);
30:
1.63 joris 31: #define UPDATE_SKIP 100
1.18 joris 32:
1.29 jfb 33: struct cvs_cmd cvs_cmd_update = {
34: CVS_OP_UPDATE, CVS_REQ_UPDATE, "update",
35: { "up", "upd" },
36: "Bring work tree in sync with repository",
37: "[-ACdflPpR] [-D date | -r rev] [-I ign] [-j rev] [-k mode] "
38: "[-t id] ...",
1.35 xsa 39: "ACD:dfI:j:k:lPpQqRr:t:",
1.29 jfb 40: NULL,
1.59 joris 41: cvs_update
1.18 joris 42: };
1.2 jfb 43:
1.59 joris 44: int
45: cvs_update(int argc, char **argv)
1.1 jfb 46: {
1.18 joris 47: int ch;
1.59 joris 48: char *arg = ".";
1.61 joris 49: int flags;
1.59 joris 50: struct cvs_recursion cr;
1.2 jfb 51:
1.62 joris 52: flags = CR_REPO | CR_RECURSE_DIRS;
1.61 joris 53:
1.59 joris 54: while ((ch = getopt(argc, argv, cvs_cmd_update.cmd_opts)) != -1) {
1.1 jfb 55: switch (ch) {
56: case 'A':
1.24 joris 57: break;
1.1 jfb 58: case 'C':
59: case 'D':
1.32 xsa 60: break;
1.1 jfb 61: case 'd':
1.63 joris 62: build_dirs = 1;
1.24 joris 63: break;
1.1 jfb 64: case 'f':
1.2 jfb 65: break;
1.35 xsa 66: case 'I':
67: break;
68: case 'j':
69: break;
70: case 'k':
71: break;
1.1 jfb 72: case 'l':
1.61 joris 73: flags &= ~CR_RECURSE_DIRS;
1.2 jfb 74: break;
1.1 jfb 75: case 'P':
1.60 joris 76: prune_dirs = 1;
1.24 joris 77: break;
1.1 jfb 78: case 'p':
1.27 xsa 79: break;
1.1 jfb 80: case 'Q':
81: case 'q':
1.2 jfb 82: break;
1.1 jfb 83: case 'R':
1.2 jfb 84: break;
1.1 jfb 85: case 'r':
86: break;
87: default:
1.59 joris 88: fatal("%s", cvs_cmd_update.cmd_synopsis);
1.1 jfb 89: }
1.32 xsa 90: }
91:
1.59 joris 92: argc -= optind;
93: argv += optind;
1.2 jfb 94:
1.59 joris 95: cr.enterdir = cvs_update_enterdir;
1.60 joris 96: cr.leavedir = cvs_update_leavedir;
1.59 joris 97: cr.local = cvs_update_local;
98: cr.remote = NULL;
1.61 joris 99: cr.flags = flags;
1.49 joris 100:
1.59 joris 101: if (argc > 0)
102: cvs_file_run(argc, argv, &cr);
103: else
104: cvs_file_run(1, &arg, &cr);
1.35 xsa 105:
1.24 joris 106: return (0);
107: }
1.2 jfb 108:
1.59 joris 109: void
110: cvs_update_enterdir(struct cvs_file *cf)
1.2 jfb 111: {
1.59 joris 112: int l;
113: char *entry;
114: CVSENTRIES *entlist;
115:
116: cvs_log(LP_TRACE, "cvs_update_enterdir(%s)", cf->file_path);
117:
1.66 ! joris 118: cvs_file_classify(cf, 0);
1.59 joris 119:
1.63 joris 120: if (cf->file_status == DIR_CREATE && build_dirs == 1) {
1.59 joris 121: cvs_mkpath(cf->file_path);
122: if ((cf->fd = open(cf->file_path, O_RDONLY)) == -1)
123: fatal("cvs_update_enterdir: %s", strerror(errno));
124:
125: entry = xmalloc(CVS_ENT_MAXLINELEN);
126: l = snprintf(entry, CVS_ENT_MAXLINELEN, "D/%s////",
127: cf->file_name);
128: if (l == -1 || l >= CVS_ENT_MAXLINELEN)
129: fatal("cvs_update_enterdir: overflow");
130:
131: entlist = cvs_ent_open(cf->file_wd);
132: cvs_ent_add(entlist, entry);
133: cvs_ent_close(entlist, ENT_SYNC);
134: xfree(entry);
1.63 joris 135: } else if (cf->file_status == DIR_CREATE && build_dirs == 0) {
136: cf->file_status = UPDATE_SKIP;
1.60 joris 137: }
138: }
139:
140: void
141: cvs_update_leavedir(struct cvs_file *cf)
142: {
143: long base;
144: int nbytes;
145: int isempty;
146: size_t bufsize;
147: struct stat st;
148: struct dirent *dp;
149: char *buf, *ebuf, *cp;
150: struct cvs_ent *ent;
151: struct cvs_ent_line *line;
152: CVSENTRIES *entlist;
153:
154: if (fstat(cf->fd, &st) == -1)
155: fatal("cvs_update_leavedir: %s", strerror(errno));
156:
157: bufsize = st.st_size;
158: if (bufsize < st.st_blksize)
159: bufsize = st.st_blksize;
160:
161: isempty = 1;
162: buf = xmalloc(bufsize);
163:
164: if (lseek(cf->fd, SEEK_SET, 0) == -1)
165: fatal("cvs_update_leavedir: %s", strerror(errno));
166:
167: while ((nbytes = getdirentries(cf->fd, buf, bufsize, &base)) > 0) {
168: ebuf = buf + nbytes;
169: cp = buf;
170:
171: while (cp < ebuf) {
172: dp = (struct dirent *)cp;
173: if (!strcmp(dp->d_name, ".") ||
174: !strcmp(dp->d_name, "..") ||
175: dp->d_reclen == 0) {
176: cp += dp->d_reclen;
177: continue;
178: }
179:
180: if (!strcmp(dp->d_name, CVS_PATH_CVSDIR)) {
181: entlist = cvs_ent_open(cf->file_path);
182: TAILQ_FOREACH(line, &(entlist->cef_ent),
183: entries_list) {
184: ent = cvs_ent_parse(line->buf);
185:
186: if (ent->ce_status == CVS_ENT_REMOVED) {
187: isempty = 0;
188: cvs_ent_free(ent);
189: break;
190: }
191:
192: cvs_ent_free(ent);
193: }
194: cvs_ent_close(entlist, ENT_NOSYNC);
195: } else {
196: isempty = 0;
197: }
198:
199: if (isempty == 0)
200: break;
201:
202: cp += dp->d_reclen;
203: }
204: }
205:
206: if (nbytes == -1)
207: fatal("cvs_update_leavedir: %s", strerror(errno));
208:
209: xfree(buf);
210:
211: if (isempty == 1 && prune_dirs == 1) {
212: cvs_rmdir(cf->file_path);
213:
214: entlist = cvs_ent_open(cf->file_wd);
215: cvs_ent_remove(entlist, cf->file_name);
216: cvs_ent_close(entlist, ENT_SYNC);
1.2 jfb 217: }
218: }
219:
1.59 joris 220: void
221: cvs_update_local(struct cvs_file *cf)
1.2 jfb 222: {
1.65 joris 223: int ret;
224: BUF *bp;
1.59 joris 225: CVSENTRIES *entlist;
226:
227: cvs_log(LP_TRACE, "cvs_update_local(%s)", cf->file_path);
1.37 joris 228:
1.59 joris 229: if (cf->file_type == CVS_DIR) {
1.63 joris 230: if (cf->file_status == UPDATE_SKIP)
231: return;
232:
1.59 joris 233: if (cf->file_status != FILE_UNKNOWN &&
234: verbosity > 1)
235: cvs_log(LP_NOTICE, "Updating %s", cf->file_path);
236: return;
1.26 jfb 237: }
238:
1.65 joris 239: /*
240: * the bp buffer will be released inside rcs_kwexp_buf,
241: * which is called from cvs_checkout_file().
242: */
243: bp = NULL;
1.66 ! joris 244: cvs_file_classify(cf, 1);
1.45 joris 245:
1.59 joris 246: switch (cf->file_status) {
247: case FILE_UNKNOWN:
248: cvs_printf("? %s\n", cf->file_path);
1.45 joris 249: break;
1.59 joris 250: case FILE_MODIFIED:
1.65 joris 251: ret = update_has_conflict_markers(cf);
252: if (cf->file_ent->ce_conflict != NULL && ret == 1) {
1.59 joris 253: cvs_printf("C %s\n", cf->file_path);
1.65 joris 254: } else {
255: if (cf->file_ent->ce_conflict != NULL && ret == 0)
256: update_clear_conflict(cf);
1.59 joris 257: cvs_printf("M %s\n", cf->file_path);
1.65 joris 258: }
1.45 joris 259: break;
1.59 joris 260: case FILE_ADDED:
261: cvs_printf("A %s\n", cf->file_path);
1.45 joris 262: break;
1.59 joris 263: case FILE_REMOVED:
264: cvs_printf("R %s\n", cf->file_path);
1.45 joris 265: break;
1.59 joris 266: case FILE_CONFLICT:
267: cvs_printf("C %s\n", cf->file_path);
268: break;
269: case FILE_LOST:
270: case FILE_CHECKOUT:
271: case FILE_PATCH:
1.65 joris 272: bp = rcs_getrev(cf->file_rcs, cf->file_rcs->rf_head);
273: if (bp == NULL)
274: fatal("cvs_update_local: failed to get HEAD");
275:
276: cvs_checkout_file(cf, cf->file_rcs->rf_head, bp, 0);
277: cvs_printf("U %s\n", cf->file_path);
1.59 joris 278: break;
279: case FILE_MERGE:
1.65 joris 280: bp = cvs_diff3(cf->file_rcs, cf->file_path,
281: cf->file_ent->ce_rev, cf->file_rcs->rf_head, 1);
282: if (bp == NULL)
283: fatal("cvs_update_local: failed to merge");
284:
285: cvs_checkout_file(cf, cf->file_rcs->rf_head, bp, CO_MERGE);
286:
287: if (diff3_conflicts != 0) {
288: cvs_printf("C %s\n", cf->file_path);
289: } else {
290: update_clear_conflict(cf);
291: cvs_printf("M %s\n", cf->file_path);
292: }
1.59 joris 293: break;
294: case FILE_UNLINK:
295: (void)unlink(cf->file_path);
296: case FILE_REMOVE_ENTRY:
297: entlist = cvs_ent_open(cf->file_wd);
298: cvs_ent_remove(entlist, cf->file_name);
299: cvs_ent_close(entlist, ENT_SYNC);
1.45 joris 300: break;
301: default:
302: break;
303: }
1.65 joris 304: }
305:
306: static void
307: update_clear_conflict(struct cvs_file *cf)
308: {
309: int l;
310: time_t now;
311: CVSENTRIES *entlist;
312: char *entry, revbuf[16], timebuf[32];
313:
314: cvs_log(LP_TRACE, "update_clear_conflict(%s)", cf->file_path);
315:
316: time(&now);
317: ctime_r(&now, timebuf);
318: if (timebuf[strlen(timebuf) - 1] == '\n')
319: timebuf[strlen(timebuf) - 1] = '\0';
320:
321: rcsnum_tostr(cf->file_ent->ce_rev, revbuf, sizeof(revbuf));
322:
323: entry = xmalloc(CVS_ENT_MAXLINELEN);
324: l = snprintf(entry, CVS_ENT_MAXLINELEN, "/%s/%s/%s//",
325: cf->file_name, revbuf, timebuf);
326: if (l == -1 || l >= CVS_ENT_MAXLINELEN)
327: fatal("update_clear_conflict: overflow");
328:
329: entlist = cvs_ent_open(cf->file_wd);
330: cvs_ent_add(entlist, entry);
331: cvs_ent_close(entlist, ENT_SYNC);
332: xfree(entry);
333: }
334:
335: /*
336: * XXX - this is the way GNU cvs checks for outstanding conflicts
337: * in a file after a merge. It is a very very bad approach and
338: * should be looked at once opencvs is working decently.
339: */
340: int
341: update_has_conflict_markers(struct cvs_file *cf)
342: {
343: BUF *bp;
344: int conflict;
345: char *content;
346: struct cvs_line *lp;
347: struct cvs_lines *lines;
348:
349: cvs_log(LP_TRACE, "update_has_conflict_markers(%s)", cf->file_path);
350:
351: if ((bp = cvs_buf_load(cf->file_path, BUF_AUTOEXT)) == NULL)
352: fatal("update_has_conflict_markers: failed to load %s",
353: cf->file_path);
354:
355: cvs_buf_putc(bp, '\0');
356: content = cvs_buf_release(bp);
357: if ((lines = cvs_splitlines(content)) == NULL)
358: fatal("update_has_conflict_markers: failed to split lines");
359:
360: xfree(content);
361:
362: conflict = 0;
363: TAILQ_FOREACH(lp, &(lines->l_lines), l_list) {
364: if (lp->l_line == NULL)
365: continue;
366:
367: if (!strncmp(lp->l_line, RCS_CONFLICT_MARKER1,
368: strlen(RCS_CONFLICT_MARKER1)) ||
369: !strncmp(lp->l_line, RCS_CONFLICT_MARKER2,
370: strlen(RCS_CONFLICT_MARKER2)) ||
371: !strncmp(lp->l_line, RCS_CONFLICT_MARKER3,
372: strlen(RCS_CONFLICT_MARKER3))) {
373: conflict = 1;
374: break;
375: }
376: }
377:
378: cvs_freelines(lines);
379: return (conflict);
1.1 jfb 380: }