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