Annotation of src/usr.bin/cvs/update.c, Revision 1.71
1.71 ! joris 1: /* $OpenBSD: update.c,v 1.70 2006/06/03 19:07:13 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.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.4 jfb 23: #include "proto.h"
1.1 jfb 24:
1.59 joris 25: int cvs_update(int, char **);
1.60 joris 26: int prune_dirs = 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 = {
34: CVS_OP_UPDATE, CVS_REQ_UPDATE, "update",
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.62 joris 52: flags = CR_REPO | 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.27 xsa 81: break;
1.1 jfb 82: case 'Q':
83: case 'q':
1.2 jfb 84: break;
1.1 jfb 85: case 'R':
1.2 jfb 86: break;
1.1 jfb 87: case 'r':
1.70 joris 88: tag = optarg;
1.1 jfb 89: break;
90: default:
1.59 joris 91: fatal("%s", cvs_cmd_update.cmd_synopsis);
1.1 jfb 92: }
1.32 xsa 93: }
94:
1.59 joris 95: argc -= optind;
96: argv += optind;
1.2 jfb 97:
1.59 joris 98: cr.enterdir = cvs_update_enterdir;
1.60 joris 99: cr.leavedir = cvs_update_leavedir;
1.59 joris 100: cr.local = cvs_update_local;
101: cr.remote = NULL;
1.61 joris 102: cr.flags = flags;
1.49 joris 103:
1.59 joris 104: if (argc > 0)
105: cvs_file_run(argc, argv, &cr);
106: else
107: cvs_file_run(1, &arg, &cr);
1.35 xsa 108:
1.24 joris 109: return (0);
110: }
1.2 jfb 111:
1.59 joris 112: void
113: cvs_update_enterdir(struct cvs_file *cf)
1.2 jfb 114: {
1.59 joris 115: int l;
116: char *entry;
117: CVSENTRIES *entlist;
118:
119: cvs_log(LP_TRACE, "cvs_update_enterdir(%s)", cf->file_path);
120:
1.68 joris 121: cvs_file_classify(cf, NULL, 0);
1.59 joris 122:
1.63 joris 123: if (cf->file_status == DIR_CREATE && build_dirs == 1) {
1.59 joris 124: cvs_mkpath(cf->file_path);
125: if ((cf->fd = open(cf->file_path, O_RDONLY)) == -1)
126: fatal("cvs_update_enterdir: %s", strerror(errno));
127:
128: entry = xmalloc(CVS_ENT_MAXLINELEN);
129: l = snprintf(entry, CVS_ENT_MAXLINELEN, "D/%s////",
130: cf->file_name);
131: if (l == -1 || l >= CVS_ENT_MAXLINELEN)
132: fatal("cvs_update_enterdir: overflow");
133:
134: entlist = cvs_ent_open(cf->file_wd);
135: cvs_ent_add(entlist, entry);
136: cvs_ent_close(entlist, ENT_SYNC);
137: xfree(entry);
1.63 joris 138: } else if (cf->file_status == DIR_CREATE && build_dirs == 0) {
1.67 joris 139: cf->file_status = FILE_SKIP;
1.60 joris 140: }
141: }
142:
143: void
144: cvs_update_leavedir(struct cvs_file *cf)
145: {
146: long base;
147: int nbytes;
1.69 joris 148: int isempty, l;
1.60 joris 149: size_t bufsize;
150: struct stat st;
151: struct dirent *dp;
152: char *buf, *ebuf, *cp;
153: struct cvs_ent *ent;
154: struct cvs_ent_line *line;
155: CVSENTRIES *entlist;
1.69 joris 156: char *export;
1.60 joris 157:
1.67 joris 158: cvs_log(LP_TRACE, "cvs_update_leavedir(%s)", cf->file_path);
159:
1.69 joris 160: if (cvs_cmdop == CVS_OP_EXPORT) {
161: export = xmalloc(MAXPATHLEN);
162: l = snprintf(export, MAXPATHLEN, "%s/%s", cf->file_path,
163: CVS_PATH_CVSDIR);
164: if (l == -1 || l >= MAXPATHLEN)
165: fatal("cvs_update_leavedir: overflow");
166:
167: /* XXX */
168: if (cvs_rmdir(export) == -1)
169: fatal("cvs_update_leavedir: %s: %s:", export,
170: strerror(errno));
171:
172: xfree(export);
173: return;
174: }
175:
1.60 joris 176: if (fstat(cf->fd, &st) == -1)
177: fatal("cvs_update_leavedir: %s", strerror(errno));
178:
179: bufsize = st.st_size;
180: if (bufsize < st.st_blksize)
181: bufsize = st.st_blksize;
182:
183: isempty = 1;
184: buf = xmalloc(bufsize);
185:
186: if (lseek(cf->fd, SEEK_SET, 0) == -1)
187: fatal("cvs_update_leavedir: %s", strerror(errno));
188:
189: while ((nbytes = getdirentries(cf->fd, buf, bufsize, &base)) > 0) {
190: ebuf = buf + nbytes;
191: cp = buf;
192:
193: while (cp < ebuf) {
194: dp = (struct dirent *)cp;
195: if (!strcmp(dp->d_name, ".") ||
196: !strcmp(dp->d_name, "..") ||
197: dp->d_reclen == 0) {
198: cp += dp->d_reclen;
199: continue;
200: }
201:
202: if (!strcmp(dp->d_name, CVS_PATH_CVSDIR)) {
203: entlist = cvs_ent_open(cf->file_path);
204: TAILQ_FOREACH(line, &(entlist->cef_ent),
205: entries_list) {
206: ent = cvs_ent_parse(line->buf);
207:
208: if (ent->ce_status == CVS_ENT_REMOVED) {
209: isempty = 0;
210: cvs_ent_free(ent);
211: break;
212: }
213:
214: cvs_ent_free(ent);
215: }
216: cvs_ent_close(entlist, ENT_NOSYNC);
217: } else {
218: isempty = 0;
219: }
220:
221: if (isempty == 0)
222: break;
223:
224: cp += dp->d_reclen;
225: }
226: }
227:
228: if (nbytes == -1)
229: fatal("cvs_update_leavedir: %s", strerror(errno));
230:
231: xfree(buf);
232:
233: if (isempty == 1 && prune_dirs == 1) {
1.69 joris 234: /* XXX */
1.60 joris 235: cvs_rmdir(cf->file_path);
236:
237: entlist = cvs_ent_open(cf->file_wd);
238: cvs_ent_remove(entlist, cf->file_name);
239: cvs_ent_close(entlist, ENT_SYNC);
1.2 jfb 240: }
241: }
242:
1.59 joris 243: void
244: cvs_update_local(struct cvs_file *cf)
1.2 jfb 245: {
1.65 joris 246: BUF *bp;
1.70 joris 247: int ret, flags;
1.59 joris 248: CVSENTRIES *entlist;
249:
250: cvs_log(LP_TRACE, "cvs_update_local(%s)", cf->file_path);
1.37 joris 251:
1.59 joris 252: if (cf->file_type == CVS_DIR) {
1.67 joris 253: if (cf->file_status == FILE_SKIP)
1.63 joris 254: return;
255:
1.59 joris 256: if (cf->file_status != FILE_UNKNOWN &&
257: verbosity > 1)
258: cvs_log(LP_NOTICE, "Updating %s", cf->file_path);
259: return;
1.26 jfb 260: }
261:
1.65 joris 262: /*
263: * the bp buffer will be released inside rcs_kwexp_buf,
264: * which is called from cvs_checkout_file().
265: */
1.70 joris 266: flags = 0;
1.65 joris 267: bp = NULL;
1.70 joris 268: cvs_file_classify(cf, tag, 1);
269:
270: if (cf->file_status == FILE_UPTODATE && cf->file_ent != NULL &&
271: cf->file_ent->ce_tag != NULL && reset_stickies == 1) {
272: cf->file_status = FILE_CHECKOUT;
273: cf->file_rcsrev = rcs_head_get(cf->file_rcs);
274: }
1.45 joris 275:
1.59 joris 276: switch (cf->file_status) {
277: case FILE_UNKNOWN:
278: cvs_printf("? %s\n", cf->file_path);
1.45 joris 279: break;
1.59 joris 280: case FILE_MODIFIED:
1.65 joris 281: ret = update_has_conflict_markers(cf);
282: if (cf->file_ent->ce_conflict != NULL && ret == 1) {
1.59 joris 283: cvs_printf("C %s\n", cf->file_path);
1.65 joris 284: } else {
285: if (cf->file_ent->ce_conflict != NULL && ret == 0)
286: update_clear_conflict(cf);
1.59 joris 287: cvs_printf("M %s\n", cf->file_path);
1.65 joris 288: }
1.45 joris 289: break;
1.59 joris 290: case FILE_ADDED:
291: cvs_printf("A %s\n", cf->file_path);
1.45 joris 292: break;
1.59 joris 293: case FILE_REMOVED:
294: cvs_printf("R %s\n", cf->file_path);
1.45 joris 295: break;
1.59 joris 296: case FILE_CONFLICT:
297: cvs_printf("C %s\n", cf->file_path);
298: break;
299: case FILE_LOST:
300: case FILE_CHECKOUT:
301: case FILE_PATCH:
1.68 joris 302: bp = rcs_getrev(cf->file_rcs, cf->file_rcsrev);
1.65 joris 303: if (bp == NULL)
304: fatal("cvs_update_local: failed to get HEAD");
305:
1.70 joris 306: if (tag != NULL)
307: flags = CO_SETSTICKY;
308:
309: cvs_checkout_file(cf, cf->file_rcsrev, bp, flags);
1.65 joris 310: cvs_printf("U %s\n", cf->file_path);
1.59 joris 311: break;
312: case FILE_MERGE:
1.65 joris 313: bp = cvs_diff3(cf->file_rcs, cf->file_path,
1.68 joris 314: cf->file_ent->ce_rev, cf->file_rcsrev, 1);
1.65 joris 315: if (bp == NULL)
316: fatal("cvs_update_local: failed to merge");
317:
1.68 joris 318: cvs_checkout_file(cf, cf->file_rcsrev, bp, CO_MERGE);
1.65 joris 319:
320: if (diff3_conflicts != 0) {
321: cvs_printf("C %s\n", cf->file_path);
322: } else {
323: update_clear_conflict(cf);
324: cvs_printf("M %s\n", cf->file_path);
325: }
1.59 joris 326: break;
327: case FILE_UNLINK:
328: (void)unlink(cf->file_path);
329: case FILE_REMOVE_ENTRY:
330: entlist = cvs_ent_open(cf->file_wd);
331: cvs_ent_remove(entlist, cf->file_name);
332: cvs_ent_close(entlist, ENT_SYNC);
1.45 joris 333: break;
334: default:
335: break;
336: }
1.65 joris 337: }
338:
339: static void
340: update_clear_conflict(struct cvs_file *cf)
341: {
342: int l;
343: time_t now;
344: CVSENTRIES *entlist;
345: char *entry, revbuf[16], timebuf[32];
346:
347: cvs_log(LP_TRACE, "update_clear_conflict(%s)", cf->file_path);
348:
349: time(&now);
350: ctime_r(&now, timebuf);
351: if (timebuf[strlen(timebuf) - 1] == '\n')
352: timebuf[strlen(timebuf) - 1] = '\0';
353:
354: rcsnum_tostr(cf->file_ent->ce_rev, revbuf, sizeof(revbuf));
355:
356: entry = xmalloc(CVS_ENT_MAXLINELEN);
357: l = snprintf(entry, CVS_ENT_MAXLINELEN, "/%s/%s/%s//",
358: cf->file_name, revbuf, timebuf);
359: if (l == -1 || l >= CVS_ENT_MAXLINELEN)
360: fatal("update_clear_conflict: overflow");
361:
362: entlist = cvs_ent_open(cf->file_wd);
363: cvs_ent_add(entlist, entry);
364: cvs_ent_close(entlist, ENT_SYNC);
365: xfree(entry);
366: }
367:
368: /*
369: * XXX - this is the way GNU cvs checks for outstanding conflicts
370: * in a file after a merge. It is a very very bad approach and
371: * should be looked at once opencvs is working decently.
372: */
373: int
374: update_has_conflict_markers(struct cvs_file *cf)
375: {
376: BUF *bp;
377: int conflict;
378: char *content;
379: struct cvs_line *lp;
380: struct cvs_lines *lines;
381:
382: cvs_log(LP_TRACE, "update_has_conflict_markers(%s)", cf->file_path);
383:
384: if ((bp = cvs_buf_load(cf->file_path, BUF_AUTOEXT)) == NULL)
385: fatal("update_has_conflict_markers: failed to load %s",
386: cf->file_path);
387:
388: cvs_buf_putc(bp, '\0');
389: content = cvs_buf_release(bp);
390: if ((lines = cvs_splitlines(content)) == NULL)
391: fatal("update_has_conflict_markers: failed to split lines");
392:
393: xfree(content);
394:
395: conflict = 0;
396: TAILQ_FOREACH(lp, &(lines->l_lines), l_list) {
397: if (lp->l_line == NULL)
398: continue;
399:
400: if (!strncmp(lp->l_line, RCS_CONFLICT_MARKER1,
401: strlen(RCS_CONFLICT_MARKER1)) ||
402: !strncmp(lp->l_line, RCS_CONFLICT_MARKER2,
403: strlen(RCS_CONFLICT_MARKER2)) ||
404: !strncmp(lp->l_line, RCS_CONFLICT_MARKER3,
405: strlen(RCS_CONFLICT_MARKER3))) {
406: conflict = 1;
407: break;
408: }
409: }
410:
411: cvs_freelines(lines);
412: return (conflict);
1.1 jfb 413: }