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