Annotation of src/usr.bin/cvs/update.c, Revision 1.111
1.111 ! joris 1: /* $OpenBSD: update.c,v 1.110 2007/09/22 16:01:22 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.70 joris 290: int ret, flags;
1.59 joris 291: CVSENTRIES *entlist;
1.103 xsa 292: char rbuf[CVS_REV_BUFSZ];
1.59 joris 293:
294: cvs_log(LP_TRACE, "cvs_update_local(%s)", cf->file_path);
1.37 joris 295:
1.59 joris 296: if (cf->file_type == CVS_DIR) {
1.67 joris 297: if (cf->file_status == FILE_SKIP)
1.63 joris 298: return;
299:
1.59 joris 300: if (cf->file_status != FILE_UNKNOWN &&
301: verbosity > 1)
302: cvs_log(LP_NOTICE, "Updating %s", cf->file_path);
303: return;
1.26 jfb 304: }
305:
1.70 joris 306: flags = 0;
1.110 joris 307: cvs_file_classify(cf, cvs_directory_tag);
1.70 joris 308:
1.91 joris 309: if ((cf->file_status == FILE_UPTODATE ||
310: cf->file_status == FILE_MODIFIED) && cf->file_ent != NULL &&
1.70 joris 311: cf->file_ent->ce_tag != NULL && reset_stickies == 1) {
1.91 joris 312: if (cf->file_status == FILE_MODIFIED)
313: cf->file_status = FILE_MERGE;
314: else
315: cf->file_status = FILE_CHECKOUT;
1.110 joris 316:
1.70 joris 317: cf->file_rcsrev = rcs_head_get(cf->file_rcs);
1.104 joris 318:
319: /* might be a bit overkill */
320: if (cvs_server_active == 1)
321: cvs_server_clear_sticky(cf->file_wd);
1.76 reyk 322: }
323:
1.108 joris 324: if (print_stdout && cf->file_status != FILE_UNKNOWN) {
1.76 reyk 325: rcsnum_tostr(cf->file_rcsrev, rbuf, sizeof(rbuf));
1.109 tobias 326: if (verbosity > 1) {
327: cvs_log(LP_RCS, RCS_DIFF_DIV);
328: cvs_log(LP_RCS, "Checking out %s", cf->file_path);
329: cvs_log(LP_RCS, "RCS: %s", cf->file_rpath);
330: cvs_log(LP_RCS, "VERS: %s", rbuf);
331: cvs_log(LP_RCS, "***************");
332: }
1.85 joris 333: cvs_checkout_file(cf, cf->file_rcsrev, CO_DUMP);
1.76 reyk 334: return;
1.70 joris 335: }
1.45 joris 336:
1.59 joris 337: switch (cf->file_status) {
338: case FILE_UNKNOWN:
339: cvs_printf("? %s\n", cf->file_path);
1.45 joris 340: break;
1.59 joris 341: case FILE_MODIFIED:
1.65 joris 342: ret = update_has_conflict_markers(cf);
343: if (cf->file_ent->ce_conflict != NULL && ret == 1) {
1.59 joris 344: cvs_printf("C %s\n", cf->file_path);
1.65 joris 345: } else {
346: if (cf->file_ent->ce_conflict != NULL && ret == 0)
347: update_clear_conflict(cf);
1.59 joris 348: cvs_printf("M %s\n", cf->file_path);
1.65 joris 349: }
1.45 joris 350: break;
1.59 joris 351: case FILE_ADDED:
352: cvs_printf("A %s\n", cf->file_path);
1.45 joris 353: break;
1.59 joris 354: case FILE_REMOVED:
355: cvs_printf("R %s\n", cf->file_path);
1.45 joris 356: break;
1.59 joris 357: case FILE_CONFLICT:
358: cvs_printf("C %s\n", cf->file_path);
359: break;
360: case FILE_LOST:
361: case FILE_CHECKOUT:
362: case FILE_PATCH:
1.110 joris 363: if (cvs_directory_tag != NULL ||
364: (((cf->file_ent != NULL) && cf->file_ent->ce_tag != NULL) &&
365: (reset_stickies != 1)))
1.70 joris 366: flags = CO_SETSTICKY;
367:
1.85 joris 368: cvs_checkout_file(cf, cf->file_rcsrev, flags);
1.65 joris 369: cvs_printf("U %s\n", cf->file_path);
1.100 joris 370: cvs_history_add(CVS_HISTORY_UPDATE_CO, cf, NULL);
1.59 joris 371: break;
372: case FILE_MERGE:
1.85 joris 373: cvs_checkout_file(cf, cf->file_rcsrev, CO_MERGE);
1.65 joris 374:
375: if (diff3_conflicts != 0) {
376: cvs_printf("C %s\n", cf->file_path);
1.100 joris 377: cvs_history_add(CVS_HISTORY_UPDATE_MERGED_ERR,
378: cf, NULL);
1.65 joris 379: } else {
380: update_clear_conflict(cf);
381: cvs_printf("M %s\n", cf->file_path);
1.100 joris 382: cvs_history_add(CVS_HISTORY_UPDATE_MERGED, cf, NULL);
1.65 joris 383: }
1.59 joris 384: break;
385: case FILE_UNLINK:
386: (void)unlink(cf->file_path);
1.102 joris 387: if (cvs_server_active == 1)
388: cvs_checkout_file(cf, cf->file_rcsrev, CO_REMOVE);
1.59 joris 389: case FILE_REMOVE_ENTRY:
390: entlist = cvs_ent_open(cf->file_wd);
391: cvs_ent_remove(entlist, cf->file_name);
392: cvs_ent_close(entlist, ENT_SYNC);
1.100 joris 393: cvs_history_add(CVS_HISTORY_UPDATE_REMOVE, cf, NULL);
1.110 joris 394: break;
395: case FILE_UPTODATE:
396: if (cvs_cmdop != CVS_OP_UPDATE)
397: break;
398:
1.111 ! joris 399: if (cvs_directory_tag != NULL && cf->file_rcs->rf_dead != 1 &&
! 400: (cf->file_flags & FILE_HAS_TAG))
1.110 joris 401: cvs_checkout_file(cf, cf->file_rcsrev, CO_SETSTICKY);
1.45 joris 402: break;
403: default:
404: break;
405: }
1.65 joris 406: }
407:
408: static void
409: update_clear_conflict(struct cvs_file *cf)
410: {
411: time_t now;
412: CVSENTRIES *entlist;
1.103 xsa 413: char *entry, revbuf[CVS_REV_BUFSZ], timebuf[CVS_TIME_BUFSZ];
1.65 joris 414:
415: cvs_log(LP_TRACE, "update_clear_conflict(%s)", cf->file_path);
416:
417: time(&now);
418: ctime_r(&now, timebuf);
419: if (timebuf[strlen(timebuf) - 1] == '\n')
420: timebuf[strlen(timebuf) - 1] = '\0';
421:
422: rcsnum_tostr(cf->file_ent->ce_rev, revbuf, sizeof(revbuf));
423:
424: entry = xmalloc(CVS_ENT_MAXLINELEN);
1.92 xsa 425: (void)xsnprintf(entry, CVS_ENT_MAXLINELEN, "/%s/%s/%s//",
1.65 joris 426: cf->file_name, revbuf, timebuf);
427:
428: entlist = cvs_ent_open(cf->file_wd);
429: cvs_ent_add(entlist, entry);
430: cvs_ent_close(entlist, ENT_SYNC);
431: xfree(entry);
432: }
433:
434: /*
435: * XXX - this is the way GNU cvs checks for outstanding conflicts
436: * in a file after a merge. It is a very very bad approach and
437: * should be looked at once opencvs is working decently.
438: */
439: int
440: update_has_conflict_markers(struct cvs_file *cf)
441: {
442: BUF *bp;
443: int conflict;
444: char *content;
445: struct cvs_line *lp;
446: struct cvs_lines *lines;
1.83 niallo 447: size_t len;
1.65 joris 448:
449: cvs_log(LP_TRACE, "update_has_conflict_markers(%s)", cf->file_path);
1.78 joris 450:
451: if (cf->fd == -1)
452: return (0);
1.65 joris 453:
1.72 joris 454: if ((bp = cvs_buf_load_fd(cf->fd, BUF_AUTOEXT)) == NULL)
1.65 joris 455: fatal("update_has_conflict_markers: failed to load %s",
456: cf->file_path);
457:
458: cvs_buf_putc(bp, '\0');
1.83 niallo 459: len = cvs_buf_len(bp);
1.65 joris 460: content = cvs_buf_release(bp);
1.83 niallo 461: if ((lines = cvs_splitlines(content, len)) == NULL)
1.65 joris 462: fatal("update_has_conflict_markers: failed to split lines");
463:
464: conflict = 0;
465: TAILQ_FOREACH(lp, &(lines->l_lines), l_list) {
466: if (lp->l_line == NULL)
467: continue;
468:
469: if (!strncmp(lp->l_line, RCS_CONFLICT_MARKER1,
1.107 tobias 470: sizeof(RCS_CONFLICT_MARKER1) - 1) ||
1.65 joris 471: !strncmp(lp->l_line, RCS_CONFLICT_MARKER2,
1.107 tobias 472: sizeof(RCS_CONFLICT_MARKER2) - 1) ||
1.65 joris 473: !strncmp(lp->l_line, RCS_CONFLICT_MARKER3,
1.107 tobias 474: sizeof(RCS_CONFLICT_MARKER3) - 1)) {
1.65 joris 475: conflict = 1;
476: break;
477: }
478: }
479:
480: cvs_freelines(lines);
1.83 niallo 481: xfree(content);
1.65 joris 482: return (conflict);
1.1 jfb 483: }