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