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