Annotation of src/usr.bin/cvs/update.c, Revision 1.80
1.80 ! joris 1: /* $OpenBSD: update.c,v 1.79 2006/07/03 07:09:35 xsa 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.80 ! joris 23: #include "remote.h"
1.1 jfb 24:
1.60 joris 25: int prune_dirs = 0;
1.77 reyk 26: int print = 0;
1.63 joris 27: int build_dirs = 0;
1.70 joris 28: int reset_stickies = 0;
29: static char *tag = NULL;
1.63 joris 30:
1.65 joris 31: static void update_clear_conflict(struct cvs_file *);
32:
1.29 jfb 33: struct cvs_cmd cvs_cmd_update = {
1.74 joris 34: CVS_OP_UPDATE, 0, "update",
1.29 jfb 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.80 ! joris 52: flags = 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.70 joris 57: reset_stickies = 1;
1.24 joris 58: break;
1.1 jfb 59: case 'C':
60: case 'D':
1.71 joris 61: tag = optarg;
1.32 xsa 62: break;
1.1 jfb 63: case 'd':
1.63 joris 64: build_dirs = 1;
1.24 joris 65: break;
1.1 jfb 66: case 'f':
1.2 jfb 67: break;
1.35 xsa 68: case 'I':
69: break;
70: case 'j':
71: break;
72: case 'k':
73: break;
1.1 jfb 74: case 'l':
1.61 joris 75: flags &= ~CR_RECURSE_DIRS;
1.2 jfb 76: break;
1.1 jfb 77: case 'P':
1.60 joris 78: prune_dirs = 1;
1.24 joris 79: break;
1.1 jfb 80: case 'p':
1.77 reyk 81: print = 1;
1.79 xsa 82: cvs_noexec = 1;
1.27 xsa 83: break;
1.1 jfb 84: case 'Q':
85: case 'q':
1.2 jfb 86: break;
1.1 jfb 87: case 'R':
1.2 jfb 88: break;
1.1 jfb 89: case 'r':
1.70 joris 90: tag = optarg;
1.1 jfb 91: break;
92: default:
1.59 joris 93: fatal("%s", cvs_cmd_update.cmd_synopsis);
1.1 jfb 94: }
1.32 xsa 95: }
96:
1.59 joris 97: argc -= optind;
98: argv += optind;
1.2 jfb 99:
1.80 ! joris 100: if (current_cvsroot->cr_method == CVS_METHOD_LOCAL) {
! 101: cr.enterdir = cvs_update_enterdir;
! 102: cr.leavedir = cvs_update_leavedir;
! 103: cr.fileproc = cvs_update_local;
! 104: flags |= CR_REPO;
! 105: } else {
! 106: if (reset_stickies)
! 107: cvs_client_send_request("Argument -A");
! 108: if (build_dirs)
! 109: cvs_client_send_request("Argument -d");
! 110: if (!(flags & CR_RECURSE_DIRS))
! 111: cvs_client_send_request("Argument -l");
! 112: if (prune_dirs)
! 113: cvs_client_send_request("Argument -P");
! 114: if (print)
! 115: cvs_client_send_request("Argument -p");
! 116:
! 117: cr.enterdir = NULL;
! 118: cr.leavedir = NULL;
! 119: cr.fileproc = cvs_client_sendfile;
! 120: }
! 121:
1.61 joris 122: cr.flags = flags;
1.49 joris 123:
1.59 joris 124: if (argc > 0)
125: cvs_file_run(argc, argv, &cr);
126: else
127: cvs_file_run(1, &arg, &cr);
1.80 ! joris 128:
! 129: if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
! 130: cvs_client_send_files(argv, argc);
! 131: cvs_client_senddir(".");
! 132: cvs_client_send_request("update");
! 133: cvs_client_get_responses();
! 134: }
1.35 xsa 135:
1.24 joris 136: return (0);
137: }
1.2 jfb 138:
1.59 joris 139: void
140: cvs_update_enterdir(struct cvs_file *cf)
1.2 jfb 141: {
1.59 joris 142: int l;
143: char *entry;
144: CVSENTRIES *entlist;
145:
146: cvs_log(LP_TRACE, "cvs_update_enterdir(%s)", cf->file_path);
147:
1.68 joris 148: cvs_file_classify(cf, NULL, 0);
1.59 joris 149:
1.63 joris 150: if (cf->file_status == DIR_CREATE && build_dirs == 1) {
1.59 joris 151: cvs_mkpath(cf->file_path);
152: if ((cf->fd = open(cf->file_path, O_RDONLY)) == -1)
153: fatal("cvs_update_enterdir: %s", strerror(errno));
154:
155: entry = xmalloc(CVS_ENT_MAXLINELEN);
156: l = snprintf(entry, CVS_ENT_MAXLINELEN, "D/%s////",
157: cf->file_name);
158: if (l == -1 || l >= CVS_ENT_MAXLINELEN)
159: fatal("cvs_update_enterdir: overflow");
160:
161: entlist = cvs_ent_open(cf->file_wd);
162: cvs_ent_add(entlist, entry);
163: cvs_ent_close(entlist, ENT_SYNC);
164: xfree(entry);
1.63 joris 165: } else if (cf->file_status == DIR_CREATE && build_dirs == 0) {
1.67 joris 166: cf->file_status = FILE_SKIP;
1.60 joris 167: }
168: }
169:
170: void
171: cvs_update_leavedir(struct cvs_file *cf)
172: {
173: long base;
174: int nbytes;
1.69 joris 175: int isempty, l;
1.60 joris 176: size_t bufsize;
177: struct stat st;
178: struct dirent *dp;
179: char *buf, *ebuf, *cp;
180: struct cvs_ent *ent;
181: struct cvs_ent_line *line;
182: CVSENTRIES *entlist;
1.69 joris 183: char *export;
1.60 joris 184:
1.67 joris 185: cvs_log(LP_TRACE, "cvs_update_leavedir(%s)", cf->file_path);
186:
1.69 joris 187: if (cvs_cmdop == CVS_OP_EXPORT) {
188: export = xmalloc(MAXPATHLEN);
189: l = snprintf(export, MAXPATHLEN, "%s/%s", cf->file_path,
190: CVS_PATH_CVSDIR);
191: if (l == -1 || l >= MAXPATHLEN)
192: fatal("cvs_update_leavedir: overflow");
193:
194: /* XXX */
195: if (cvs_rmdir(export) == -1)
196: fatal("cvs_update_leavedir: %s: %s:", export,
197: strerror(errno));
198:
199: xfree(export);
200: return;
201: }
202:
1.60 joris 203: if (fstat(cf->fd, &st) == -1)
204: fatal("cvs_update_leavedir: %s", strerror(errno));
205:
206: bufsize = st.st_size;
207: if (bufsize < st.st_blksize)
208: bufsize = st.st_blksize;
209:
210: isempty = 1;
211: buf = xmalloc(bufsize);
212:
1.73 joris 213: if (lseek(cf->fd, 0, SEEK_SET) == -1)
1.60 joris 214: fatal("cvs_update_leavedir: %s", strerror(errno));
215:
216: while ((nbytes = getdirentries(cf->fd, buf, bufsize, &base)) > 0) {
217: ebuf = buf + nbytes;
218: cp = buf;
219:
220: while (cp < ebuf) {
221: dp = (struct dirent *)cp;
222: if (!strcmp(dp->d_name, ".") ||
223: !strcmp(dp->d_name, "..") ||
224: dp->d_reclen == 0) {
225: cp += dp->d_reclen;
226: continue;
227: }
228:
229: if (!strcmp(dp->d_name, CVS_PATH_CVSDIR)) {
230: entlist = cvs_ent_open(cf->file_path);
231: TAILQ_FOREACH(line, &(entlist->cef_ent),
232: entries_list) {
233: ent = cvs_ent_parse(line->buf);
234:
235: if (ent->ce_status == CVS_ENT_REMOVED) {
236: isempty = 0;
237: cvs_ent_free(ent);
238: break;
239: }
240:
241: cvs_ent_free(ent);
242: }
243: cvs_ent_close(entlist, ENT_NOSYNC);
244: } else {
245: isempty = 0;
246: }
247:
248: if (isempty == 0)
249: break;
250:
251: cp += dp->d_reclen;
252: }
253: }
254:
255: if (nbytes == -1)
256: fatal("cvs_update_leavedir: %s", strerror(errno));
257:
258: xfree(buf);
259:
260: if (isempty == 1 && prune_dirs == 1) {
1.69 joris 261: /* XXX */
1.60 joris 262: cvs_rmdir(cf->file_path);
263:
264: entlist = cvs_ent_open(cf->file_wd);
265: cvs_ent_remove(entlist, cf->file_name);
266: cvs_ent_close(entlist, ENT_SYNC);
1.2 jfb 267: }
268: }
269:
1.59 joris 270: void
271: cvs_update_local(struct cvs_file *cf)
1.2 jfb 272: {
1.65 joris 273: BUF *bp;
1.70 joris 274: int ret, flags;
1.59 joris 275: CVSENTRIES *entlist;
1.76 reyk 276: char rbuf[16];
1.59 joris 277:
278: cvs_log(LP_TRACE, "cvs_update_local(%s)", cf->file_path);
1.37 joris 279:
1.59 joris 280: if (cf->file_type == CVS_DIR) {
1.67 joris 281: if (cf->file_status == FILE_SKIP)
1.63 joris 282: return;
283:
1.59 joris 284: if (cf->file_status != FILE_UNKNOWN &&
285: verbosity > 1)
286: cvs_log(LP_NOTICE, "Updating %s", cf->file_path);
287: return;
1.26 jfb 288: }
289:
1.65 joris 290: /*
291: * the bp buffer will be released inside rcs_kwexp_buf,
292: * which is called from cvs_checkout_file().
293: */
1.70 joris 294: flags = 0;
1.65 joris 295: bp = NULL;
1.70 joris 296: cvs_file_classify(cf, tag, 1);
297:
298: if (cf->file_status == FILE_UPTODATE && cf->file_ent != NULL &&
299: cf->file_ent->ce_tag != NULL && reset_stickies == 1) {
300: cf->file_status = FILE_CHECKOUT;
301: cf->file_rcsrev = rcs_head_get(cf->file_rcs);
1.76 reyk 302: }
303:
1.77 reyk 304: if (print && cf->file_status != FILE_UNKNOWN) {
1.76 reyk 305: bp = rcs_getrev(cf->file_rcs, cf->file_rcsrev);
306: if (bp == NULL)
307: fatal("cvs_update_local: failed to get HEAD");
308: rcsnum_tostr(cf->file_rcsrev, rbuf, sizeof(rbuf));
309: if (verbosity > 1)
310: cvs_printf("%s\nChecking out %s\n"
311: "RCS:\t%s\nVERS:\t%s\n***************\n",
312: RCS_DIFF_DIV, cf->file_path, cf->file_rpath, rbuf);
313: cvs_checkout_file(cf, cf->file_rcsrev, bp, CO_DUMP);
314: return;
1.70 joris 315: }
1.45 joris 316:
1.59 joris 317: switch (cf->file_status) {
318: case FILE_UNKNOWN:
319: cvs_printf("? %s\n", cf->file_path);
1.45 joris 320: break;
1.59 joris 321: case FILE_MODIFIED:
1.65 joris 322: ret = update_has_conflict_markers(cf);
323: if (cf->file_ent->ce_conflict != NULL && ret == 1) {
1.59 joris 324: cvs_printf("C %s\n", cf->file_path);
1.65 joris 325: } else {
326: if (cf->file_ent->ce_conflict != NULL && ret == 0)
327: update_clear_conflict(cf);
1.59 joris 328: cvs_printf("M %s\n", cf->file_path);
1.65 joris 329: }
1.45 joris 330: break;
1.59 joris 331: case FILE_ADDED:
332: cvs_printf("A %s\n", cf->file_path);
1.45 joris 333: break;
1.59 joris 334: case FILE_REMOVED:
335: cvs_printf("R %s\n", cf->file_path);
1.45 joris 336: break;
1.59 joris 337: case FILE_CONFLICT:
338: cvs_printf("C %s\n", cf->file_path);
339: break;
340: case FILE_LOST:
341: case FILE_CHECKOUT:
342: case FILE_PATCH:
1.68 joris 343: bp = rcs_getrev(cf->file_rcs, cf->file_rcsrev);
1.65 joris 344: if (bp == NULL)
345: fatal("cvs_update_local: failed to get HEAD");
346:
1.70 joris 347: if (tag != NULL)
348: flags = CO_SETSTICKY;
349:
350: cvs_checkout_file(cf, cf->file_rcsrev, bp, flags);
1.65 joris 351: cvs_printf("U %s\n", cf->file_path);
1.59 joris 352: break;
353: case FILE_MERGE:
1.72 joris 354: bp = cvs_diff3(cf->file_rcs, cf->file_path, cf->fd,
1.68 joris 355: cf->file_ent->ce_rev, cf->file_rcsrev, 1);
1.65 joris 356: if (bp == NULL)
357: fatal("cvs_update_local: failed to merge");
358:
1.68 joris 359: cvs_checkout_file(cf, cf->file_rcsrev, bp, CO_MERGE);
1.65 joris 360:
361: if (diff3_conflicts != 0) {
362: cvs_printf("C %s\n", cf->file_path);
363: } else {
364: update_clear_conflict(cf);
365: cvs_printf("M %s\n", cf->file_path);
366: }
1.59 joris 367: break;
368: case FILE_UNLINK:
369: (void)unlink(cf->file_path);
370: case FILE_REMOVE_ENTRY:
371: entlist = cvs_ent_open(cf->file_wd);
372: cvs_ent_remove(entlist, cf->file_name);
373: cvs_ent_close(entlist, ENT_SYNC);
1.45 joris 374: break;
375: default:
376: break;
377: }
1.65 joris 378: }
379:
380: static void
381: update_clear_conflict(struct cvs_file *cf)
382: {
383: int l;
384: time_t now;
385: CVSENTRIES *entlist;
386: char *entry, revbuf[16], timebuf[32];
387:
388: cvs_log(LP_TRACE, "update_clear_conflict(%s)", cf->file_path);
389:
390: time(&now);
391: ctime_r(&now, timebuf);
392: if (timebuf[strlen(timebuf) - 1] == '\n')
393: timebuf[strlen(timebuf) - 1] = '\0';
394:
395: rcsnum_tostr(cf->file_ent->ce_rev, revbuf, sizeof(revbuf));
396:
397: entry = xmalloc(CVS_ENT_MAXLINELEN);
398: l = snprintf(entry, CVS_ENT_MAXLINELEN, "/%s/%s/%s//",
399: cf->file_name, revbuf, timebuf);
400: if (l == -1 || l >= CVS_ENT_MAXLINELEN)
401: fatal("update_clear_conflict: overflow");
402:
403: entlist = cvs_ent_open(cf->file_wd);
404: cvs_ent_add(entlist, entry);
405: cvs_ent_close(entlist, ENT_SYNC);
406: xfree(entry);
407: }
408:
409: /*
410: * XXX - this is the way GNU cvs checks for outstanding conflicts
411: * in a file after a merge. It is a very very bad approach and
412: * should be looked at once opencvs is working decently.
413: */
414: int
415: update_has_conflict_markers(struct cvs_file *cf)
416: {
417: BUF *bp;
418: int conflict;
419: char *content;
420: struct cvs_line *lp;
421: struct cvs_lines *lines;
422:
423: cvs_log(LP_TRACE, "update_has_conflict_markers(%s)", cf->file_path);
1.78 joris 424:
425: if (cf->fd == -1)
426: return (0);
1.65 joris 427:
1.72 joris 428: if ((bp = cvs_buf_load_fd(cf->fd, BUF_AUTOEXT)) == NULL)
1.65 joris 429: fatal("update_has_conflict_markers: failed to load %s",
430: cf->file_path);
431:
432: cvs_buf_putc(bp, '\0');
433: content = cvs_buf_release(bp);
434: if ((lines = cvs_splitlines(content)) == NULL)
435: fatal("update_has_conflict_markers: failed to split lines");
436:
437: xfree(content);
438:
439: conflict = 0;
440: TAILQ_FOREACH(lp, &(lines->l_lines), l_list) {
441: if (lp->l_line == NULL)
442: continue;
443:
444: if (!strncmp(lp->l_line, RCS_CONFLICT_MARKER1,
445: strlen(RCS_CONFLICT_MARKER1)) ||
446: !strncmp(lp->l_line, RCS_CONFLICT_MARKER2,
447: strlen(RCS_CONFLICT_MARKER2)) ||
448: !strncmp(lp->l_line, RCS_CONFLICT_MARKER3,
449: strlen(RCS_CONFLICT_MARKER3))) {
450: conflict = 1;
451: break;
452: }
453: }
454:
455: cvs_freelines(lines);
456: return (conflict);
1.1 jfb 457: }