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