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