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