Annotation of src/usr.bin/cvs/update.c, Revision 1.125
1.125 ! joris 1: /* $OpenBSD: update.c,v 1.124 2008/02/09 12:20:33 tobias 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.97 otto 18: #include <sys/stat.h>
19:
20: #include <errno.h>
21: #include <fcntl.h>
22: #include <string.h>
23: #include <unistd.h>
1.1 jfb 24:
25: #include "cvs.h"
1.59 joris 26: #include "diff.h"
1.80 joris 27: #include "remote.h"
1.1 jfb 28:
1.60 joris 29: int prune_dirs = 0;
1.108 joris 30: int print_stdout = 0;
1.63 joris 31: int build_dirs = 0;
1.124 tobias 32: int reset_option = 0;
33: int reset_tag = 0;
1.104 joris 34: char *cvs_specified_tag = NULL;
1.63 joris 35:
1.119 tobias 36: static char *koptstr;
1.125 ! joris 37: static char *dateflag = NULL;
1.124 tobias 38: static int Aflag = 0;
1.119 tobias 39:
1.65 joris 40: static void update_clear_conflict(struct cvs_file *);
41:
1.29 jfb 42: struct cvs_cmd cvs_cmd_update = {
1.116 tobias 43: CVS_OP_UPDATE, CVS_USE_WDIR, "update",
1.29 jfb 44: { "up", "upd" },
45: "Bring work tree in sync with repository",
46: "[-ACdflPpR] [-D date | -r rev] [-I ign] [-j rev] [-k mode] "
47: "[-t id] ...",
1.106 joris 48: "ACD:dfI:j:k:lPpQqRr:t:u",
1.29 jfb 49: NULL,
1.59 joris 50: cvs_update
1.18 joris 51: };
1.2 jfb 52:
1.59 joris 53: int
54: cvs_update(int argc, char **argv)
1.1 jfb 55: {
1.18 joris 56: int ch;
1.59 joris 57: char *arg = ".";
1.61 joris 58: int flags;
1.59 joris 59: struct cvs_recursion cr;
1.2 jfb 60:
1.80 joris 61: flags = CR_RECURSE_DIRS;
1.61 joris 62:
1.59 joris 63: while ((ch = getopt(argc, argv, cvs_cmd_update.cmd_opts)) != -1) {
1.1 jfb 64: switch (ch) {
65: case 'A':
1.124 tobias 66: Aflag = 1;
67: if (koptstr == NULL)
68: reset_option = 1;
69: if (cvs_specified_tag == NULL)
70: reset_tag = 1;
1.24 joris 71: break;
1.1 jfb 72: case 'C':
73: case 'D':
1.125 ! joris 74: dateflag = optarg;
! 75: cvs_specified_date = cvs_date_parse(dateflag);
1.32 xsa 76: break;
1.1 jfb 77: case 'd':
1.63 joris 78: build_dirs = 1;
1.24 joris 79: break;
1.1 jfb 80: case 'f':
1.2 jfb 81: break;
1.35 xsa 82: case 'I':
83: break;
84: case 'j':
85: break;
86: case 'k':
1.124 tobias 87: reset_option = 0;
1.119 tobias 88: koptstr = optarg;
89: kflag = rcs_kflag_get(koptstr);
90: if (RCS_KWEXP_INVAL(kflag)) {
91: cvs_log(LP_ERR,
92: "invalid RCS keyword expension mode");
93: fatal("%s", cvs_cmd_add.cmd_synopsis);
94: }
1.35 xsa 95: break;
1.1 jfb 96: case 'l':
1.61 joris 97: flags &= ~CR_RECURSE_DIRS;
1.2 jfb 98: break;
1.1 jfb 99: case 'P':
1.60 joris 100: prune_dirs = 1;
1.24 joris 101: break;
1.1 jfb 102: case 'p':
1.108 joris 103: print_stdout = 1;
1.79 xsa 104: cvs_noexec = 1;
1.27 xsa 105: break;
1.1 jfb 106: case 'Q':
107: case 'q':
1.2 jfb 108: break;
1.1 jfb 109: case 'R':
1.115 tobias 110: flags |= CR_RECURSE_DIRS;
1.2 jfb 111: break;
1.1 jfb 112: case 'r':
1.124 tobias 113: reset_tag = 0;
1.104 joris 114: cvs_specified_tag = optarg;
1.106 joris 115: break;
116: case 'u':
1.1 jfb 117: break;
118: default:
1.59 joris 119: fatal("%s", cvs_cmd_update.cmd_synopsis);
1.1 jfb 120: }
1.32 xsa 121: }
122:
1.59 joris 123: argc -= optind;
124: argv += optind;
1.2 jfb 125:
1.80 joris 126: if (current_cvsroot->cr_method == CVS_METHOD_LOCAL) {
127: cr.enterdir = cvs_update_enterdir;
1.118 tobias 128: cr.leavedir = prune_dirs ? cvs_update_leavedir : NULL;
1.80 joris 129: cr.fileproc = cvs_update_local;
130: flags |= CR_REPO;
131: } else {
1.82 joris 132: cvs_client_connect_to_server();
1.124 tobias 133: if (Aflag)
1.80 joris 134: cvs_client_send_request("Argument -A");
1.125 ! joris 135: if (dateflag != NULL)
! 136: cvs_client_send_request("Argument -D%s", dateflag);
1.80 joris 137: if (build_dirs)
138: cvs_client_send_request("Argument -d");
1.119 tobias 139: if (kflag)
140: cvs_client_send_request("Argument -k%s", koptstr);
1.80 joris 141: if (!(flags & CR_RECURSE_DIRS))
142: cvs_client_send_request("Argument -l");
143: if (prune_dirs)
144: cvs_client_send_request("Argument -P");
1.108 joris 145: if (print_stdout)
1.80 joris 146: cvs_client_send_request("Argument -p");
147:
1.110 joris 148: if (cvs_specified_tag != NULL)
149: cvs_client_send_request("Argument -r%s",
150: cvs_specified_tag);
151:
1.80 joris 152: cr.enterdir = NULL;
153: cr.leavedir = NULL;
154: cr.fileproc = cvs_client_sendfile;
155: }
156:
1.61 joris 157: cr.flags = flags;
1.49 joris 158:
1.59 joris 159: if (argc > 0)
160: cvs_file_run(argc, argv, &cr);
161: else
162: cvs_file_run(1, &arg, &cr);
1.80 joris 163:
164: if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
165: cvs_client_send_files(argv, argc);
166: cvs_client_senddir(".");
167: cvs_client_send_request("update");
168: cvs_client_get_responses();
169: }
1.35 xsa 170:
1.24 joris 171: return (0);
172: }
1.2 jfb 173:
1.59 joris 174: void
175: cvs_update_enterdir(struct cvs_file *cf)
1.2 jfb 176: {
1.59 joris 177: CVSENTRIES *entlist;
1.104 joris 178: char *entry, fpath[MAXPATHLEN];
1.59 joris 179:
180: cvs_log(LP_TRACE, "cvs_update_enterdir(%s)", cf->file_path);
181:
1.110 joris 182: cvs_file_classify(cf, cvs_directory_tag);
1.59 joris 183:
1.63 joris 184: if (cf->file_status == DIR_CREATE && build_dirs == 1) {
1.104 joris 185: cvs_mkpath(cf->file_path, cvs_specified_tag);
1.59 joris 186: if ((cf->fd = open(cf->file_path, O_RDONLY)) == -1)
1.87 xsa 187: fatal("cvs_update_enterdir: `%s': %s",
188: cf->file_path, strerror(errno));
1.59 joris 189:
1.114 tobias 190: if (cvs_cmdop != CVS_OP_EXPORT) {
191: (void)xasprintf(&entry, "D/%s////", cf->file_name);
1.59 joris 192:
1.114 tobias 193: entlist = cvs_ent_open(cf->file_wd);
194: cvs_ent_add(entlist, entry);
195: cvs_ent_close(entlist, ENT_SYNC);
196: xfree(entry);
197: }
1.94 xsa 198: } else if ((cf->file_status == DIR_CREATE && build_dirs == 0) ||
199: cf->file_status == FILE_UNKNOWN) {
1.67 joris 200: cf->file_status = FILE_SKIP;
1.124 tobias 201: } else if (reset_tag) {
1.104 joris 202: (void)xsnprintf(fpath, MAXPATHLEN, "%s/%s",
203: cf->file_path, CVS_PATH_TAG);
204: (void)unlink(fpath);
205: } else {
206: if (cvs_specified_tag != NULL)
207: cvs_write_tagfile(cf->file_path,
1.120 tobias 208: cvs_specified_tag, NULL);
1.60 joris 209: }
210: }
211:
212: void
213: cvs_update_leavedir(struct cvs_file *cf)
214: {
215: long base;
216: int nbytes;
1.81 xsa 217: int isempty;
1.60 joris 218: size_t bufsize;
219: struct stat st;
220: struct dirent *dp;
221: char *buf, *ebuf, *cp;
222: struct cvs_ent *ent;
223: struct cvs_ent_line *line;
224: CVSENTRIES *entlist;
225:
1.67 joris 226: cvs_log(LP_TRACE, "cvs_update_leavedir(%s)", cf->file_path);
1.86 joris 227:
1.105 joris 228: if (cvs_server_active == 1 && !strcmp(cf->file_name, "."))
229: return;
1.69 joris 230:
1.60 joris 231: if (fstat(cf->fd, &st) == -1)
232: fatal("cvs_update_leavedir: %s", strerror(errno));
233:
234: bufsize = st.st_size;
235: if (bufsize < st.st_blksize)
236: bufsize = st.st_blksize;
237:
238: isempty = 1;
239: buf = xmalloc(bufsize);
240:
1.73 joris 241: if (lseek(cf->fd, 0, SEEK_SET) == -1)
1.60 joris 242: fatal("cvs_update_leavedir: %s", strerror(errno));
243:
244: while ((nbytes = getdirentries(cf->fd, buf, bufsize, &base)) > 0) {
245: ebuf = buf + nbytes;
246: cp = buf;
247:
248: while (cp < ebuf) {
249: dp = (struct dirent *)cp;
250: if (!strcmp(dp->d_name, ".") ||
251: !strcmp(dp->d_name, "..") ||
1.95 joris 252: dp->d_fileno == 0) {
1.60 joris 253: cp += dp->d_reclen;
254: continue;
255: }
256:
257: if (!strcmp(dp->d_name, CVS_PATH_CVSDIR)) {
258: entlist = cvs_ent_open(cf->file_path);
259: TAILQ_FOREACH(line, &(entlist->cef_ent),
260: entries_list) {
261: ent = cvs_ent_parse(line->buf);
262:
263: if (ent->ce_status == CVS_ENT_REMOVED) {
264: isempty = 0;
265: cvs_ent_free(ent);
266: break;
267: }
268:
269: cvs_ent_free(ent);
270: }
271: cvs_ent_close(entlist, ENT_NOSYNC);
272: } else {
273: isempty = 0;
274: }
275:
276: if (isempty == 0)
277: break;
278:
279: cp += dp->d_reclen;
280: }
281: }
282:
283: if (nbytes == -1)
284: fatal("cvs_update_leavedir: %s", strerror(errno));
285:
286: xfree(buf);
287:
288: if (isempty == 1 && prune_dirs == 1) {
1.69 joris 289: /* XXX */
1.60 joris 290: cvs_rmdir(cf->file_path);
291:
1.114 tobias 292: if (cvs_server_active == 0 && cvs_cmdop != CVS_OP_EXPORT) {
1.88 joris 293: entlist = cvs_ent_open(cf->file_wd);
294: cvs_ent_remove(entlist, cf->file_name);
295: cvs_ent_close(entlist, ENT_SYNC);
296: }
1.2 jfb 297: }
298: }
299:
1.59 joris 300: void
301: cvs_update_local(struct cvs_file *cf)
1.2 jfb 302: {
1.112 joris 303: char *tag;
1.70 joris 304: int ret, flags;
1.59 joris 305: CVSENTRIES *entlist;
1.119 tobias 306: char kbuf[8], rbuf[CVS_REV_BUFSZ];
1.59 joris 307:
308: cvs_log(LP_TRACE, "cvs_update_local(%s)", cf->file_path);
1.37 joris 309:
1.59 joris 310: if (cf->file_type == CVS_DIR) {
1.114 tobias 311: if (cf->file_status == FILE_SKIP) {
312: if (cvs_cmdop == CVS_OP_EXPORT && verbosity > 0)
313: cvs_printf("? %s\n", cf->file_path);
1.63 joris 314: return;
1.114 tobias 315: }
1.63 joris 316:
1.59 joris 317: if (cf->file_status != FILE_UNKNOWN &&
318: verbosity > 1)
319: cvs_log(LP_NOTICE, "Updating %s", cf->file_path);
320: return;
1.26 jfb 321: }
322:
1.70 joris 323: flags = 0;
1.112 joris 324: if (cvs_specified_tag != NULL)
325: tag = cvs_specified_tag;
1.122 joris 326: else if (cf->file_ent != NULL && cf->file_ent->ce_tag != NULL)
327: tag = cf->file_ent->ce_tag;
1.112 joris 328: else
329: tag = cvs_directory_tag;
330:
331: cvs_file_classify(cf, tag);
332:
1.119 tobias 333: if (kflag)
334: rcs_kwexp_set(cf->file_rcs, kflag);
1.70 joris 335:
1.91 joris 336: if ((cf->file_status == FILE_UPTODATE ||
337: cf->file_status == FILE_MODIFIED) && cf->file_ent != NULL &&
1.124 tobias 338: cf->file_ent->ce_tag != NULL && reset_tag) {
1.91 joris 339: if (cf->file_status == FILE_MODIFIED)
340: cf->file_status = FILE_MERGE;
341: else
342: cf->file_status = FILE_CHECKOUT;
1.110 joris 343:
1.70 joris 344: cf->file_rcsrev = rcs_head_get(cf->file_rcs);
1.104 joris 345:
346: /* might be a bit overkill */
347: if (cvs_server_active == 1)
348: cvs_server_clear_sticky(cf->file_wd);
1.76 reyk 349: }
350:
1.117 tobias 351: if (print_stdout && cf->file_status != FILE_UNKNOWN &&
352: !cf->file_rcs->rf_dead) {
1.76 reyk 353: rcsnum_tostr(cf->file_rcsrev, rbuf, sizeof(rbuf));
1.109 tobias 354: if (verbosity > 1) {
355: cvs_log(LP_RCS, RCS_DIFF_DIV);
356: cvs_log(LP_RCS, "Checking out %s", cf->file_path);
357: cvs_log(LP_RCS, "RCS: %s", cf->file_rpath);
358: cvs_log(LP_RCS, "VERS: %s", rbuf);
359: cvs_log(LP_RCS, "***************");
360: }
1.112 joris 361: cvs_checkout_file(cf, cf->file_rcsrev, tag, CO_DUMP);
1.76 reyk 362: return;
1.70 joris 363: }
1.45 joris 364:
1.119 tobias 365: if (cf->file_ent != NULL) {
366: if (cf->file_ent->ce_opts == NULL) {
367: if (kflag)
368: cf->file_status = FILE_CHECKOUT;
369: } else {
370: if (kflag) {
371: (void)xsnprintf(kbuf, sizeof(kbuf),
372: "-k%s", cf->file_rcs->rf_expand);
373:
374: if (strcmp(kbuf, cf->file_ent->ce_opts))
375: cf->file_status = FILE_CHECKOUT;
1.124 tobias 376: } else if (reset_option)
1.119 tobias 377: cf->file_status = FILE_CHECKOUT;
378: }
379: }
380:
1.59 joris 381: switch (cf->file_status) {
382: case FILE_UNKNOWN:
383: cvs_printf("? %s\n", cf->file_path);
1.45 joris 384: break;
1.59 joris 385: case FILE_MODIFIED:
1.65 joris 386: ret = update_has_conflict_markers(cf);
387: if (cf->file_ent->ce_conflict != NULL && ret == 1) {
1.59 joris 388: cvs_printf("C %s\n", cf->file_path);
1.65 joris 389: } else {
390: if (cf->file_ent->ce_conflict != NULL && ret == 0)
391: update_clear_conflict(cf);
1.59 joris 392: cvs_printf("M %s\n", cf->file_path);
1.65 joris 393: }
1.45 joris 394: break;
1.59 joris 395: case FILE_ADDED:
396: cvs_printf("A %s\n", cf->file_path);
1.45 joris 397: break;
1.59 joris 398: case FILE_REMOVED:
399: cvs_printf("R %s\n", cf->file_path);
1.45 joris 400: break;
1.59 joris 401: case FILE_CONFLICT:
402: cvs_printf("C %s\n", cf->file_path);
403: break;
404: case FILE_LOST:
405: case FILE_CHECKOUT:
406: case FILE_PATCH:
1.124 tobias 407: if ((tag != NULL && !reset_tag) ||
1.110 joris 408: (((cf->file_ent != NULL) && cf->file_ent->ce_tag != NULL) &&
1.124 tobias 409: !reset_tag))
1.70 joris 410: flags = CO_SETSTICKY;
411:
1.112 joris 412: cvs_checkout_file(cf, cf->file_rcsrev, tag, flags);
1.65 joris 413: cvs_printf("U %s\n", cf->file_path);
1.100 joris 414: cvs_history_add(CVS_HISTORY_UPDATE_CO, cf, NULL);
1.59 joris 415: break;
416: case FILE_MERGE:
1.112 joris 417: cvs_checkout_file(cf, cf->file_rcsrev, tag, CO_MERGE);
1.65 joris 418:
419: if (diff3_conflicts != 0) {
420: cvs_printf("C %s\n", cf->file_path);
1.100 joris 421: cvs_history_add(CVS_HISTORY_UPDATE_MERGED_ERR,
422: cf, NULL);
1.65 joris 423: } else {
424: update_clear_conflict(cf);
425: cvs_printf("M %s\n", cf->file_path);
1.100 joris 426: cvs_history_add(CVS_HISTORY_UPDATE_MERGED, cf, NULL);
1.65 joris 427: }
1.59 joris 428: break;
429: case FILE_UNLINK:
430: (void)unlink(cf->file_path);
1.102 joris 431: if (cvs_server_active == 1)
1.112 joris 432: cvs_checkout_file(cf, cf->file_rcsrev, tag, CO_REMOVE);
1.59 joris 433: case FILE_REMOVE_ENTRY:
434: entlist = cvs_ent_open(cf->file_wd);
435: cvs_ent_remove(entlist, cf->file_name);
436: cvs_ent_close(entlist, ENT_SYNC);
1.100 joris 437: cvs_history_add(CVS_HISTORY_UPDATE_REMOVE, cf, NULL);
1.110 joris 438: break;
439: case FILE_UPTODATE:
440: if (cvs_cmdop != CVS_OP_UPDATE)
441: break;
442:
1.112 joris 443: if (tag != NULL && cf->file_rcs->rf_dead != 1 &&
1.111 joris 444: (cf->file_flags & FILE_HAS_TAG))
1.112 joris 445: cvs_checkout_file(cf, cf->file_rcsrev,
446: tag, CO_SETSTICKY);
1.45 joris 447: break;
448: default:
449: break;
450: }
1.65 joris 451: }
452:
453: static void
454: update_clear_conflict(struct cvs_file *cf)
455: {
456: time_t now;
457: CVSENTRIES *entlist;
1.103 xsa 458: char *entry, revbuf[CVS_REV_BUFSZ], timebuf[CVS_TIME_BUFSZ];
1.121 tobias 459: char sticky[CVS_ENT_MAXLINELEN];
1.65 joris 460:
461: cvs_log(LP_TRACE, "update_clear_conflict(%s)", cf->file_path);
462:
463: time(&now);
464: ctime_r(&now, timebuf);
1.113 tobias 465: timebuf[strcspn(timebuf, "\n")] = '\0';
1.65 joris 466:
467: rcsnum_tostr(cf->file_ent->ce_rev, revbuf, sizeof(revbuf));
468:
1.121 tobias 469: sticky[0] = '\0';
470: if (cf->file_ent->ce_tag != NULL)
471: (void)xsnprintf(sticky, sizeof(sticky), "T%s",
472: cf->file_ent->ce_tag);
473:
1.65 joris 474: entry = xmalloc(CVS_ENT_MAXLINELEN);
1.119 tobias 475: (void)xsnprintf(entry, CVS_ENT_MAXLINELEN, "/%s/%s/%s/%s/%s",
476: cf->file_name, revbuf, timebuf, cf->file_ent->ce_opts ? : "",
1.121 tobias 477: sticky);
1.65 joris 478:
479: entlist = cvs_ent_open(cf->file_wd);
480: cvs_ent_add(entlist, entry);
481: cvs_ent_close(entlist, ENT_SYNC);
482: xfree(entry);
483: }
484:
485: /*
486: * XXX - this is the way GNU cvs checks for outstanding conflicts
487: * in a file after a merge. It is a very very bad approach and
488: * should be looked at once opencvs is working decently.
489: */
490: int
491: update_has_conflict_markers(struct cvs_file *cf)
492: {
493: BUF *bp;
494: int conflict;
495: char *content;
496: struct cvs_line *lp;
497: struct cvs_lines *lines;
1.83 niallo 498: size_t len;
1.65 joris 499:
500: cvs_log(LP_TRACE, "update_has_conflict_markers(%s)", cf->file_path);
1.78 joris 501:
502: if (cf->fd == -1)
503: return (0);
1.65 joris 504:
1.72 joris 505: if ((bp = cvs_buf_load_fd(cf->fd, BUF_AUTOEXT)) == NULL)
1.65 joris 506: fatal("update_has_conflict_markers: failed to load %s",
507: cf->file_path);
508:
509: cvs_buf_putc(bp, '\0');
1.83 niallo 510: len = cvs_buf_len(bp);
1.65 joris 511: content = cvs_buf_release(bp);
1.83 niallo 512: if ((lines = cvs_splitlines(content, len)) == NULL)
1.65 joris 513: fatal("update_has_conflict_markers: failed to split lines");
514:
515: conflict = 0;
516: TAILQ_FOREACH(lp, &(lines->l_lines), l_list) {
517: if (lp->l_line == NULL)
518: continue;
519:
520: if (!strncmp(lp->l_line, RCS_CONFLICT_MARKER1,
1.107 tobias 521: sizeof(RCS_CONFLICT_MARKER1) - 1) ||
1.65 joris 522: !strncmp(lp->l_line, RCS_CONFLICT_MARKER2,
1.107 tobias 523: sizeof(RCS_CONFLICT_MARKER2) - 1) ||
1.65 joris 524: !strncmp(lp->l_line, RCS_CONFLICT_MARKER3,
1.107 tobias 525: sizeof(RCS_CONFLICT_MARKER3) - 1)) {
1.65 joris 526: conflict = 1;
527: break;
528: }
529: }
530:
531: cvs_freelines(lines);
1.83 niallo 532: xfree(content);
1.65 joris 533: return (conflict);
1.1 jfb 534: }