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