Annotation of src/usr.bin/cvs/update.c, Revision 1.51
1.51 ! xsa 1: /* $OpenBSD: update.c,v 1.50 2005/12/30 16:42:13 xsa Exp $ */
1.1 jfb 2: /*
3: * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
1.11 tedu 4: * All rights reserved.
1.1 jfb 5: *
1.11 tedu 6: * Redistribution and use in source and binary forms, with or without
7: * modification, are permitted provided that the following conditions
8: * are met:
1.1 jfb 9: *
1.11 tedu 10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
1.1 jfb 12: * 2. The name of the author may not be used to endorse or promote products
1.11 tedu 13: * derived from this software without specific prior written permission.
1.1 jfb 14: *
15: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
16: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
17: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
18: * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
1.11 tedu 24: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.1 jfb 25: */
26:
1.51 ! xsa 27: #include "includes.h"
1.1 jfb 28:
29: #include "cvs.h"
30: #include "log.h"
1.4 jfb 31: #include "proto.h"
1.45 joris 32: #include "diff.h"
1.1 jfb 33:
1.42 xsa 34: static int cvs_update_init(struct cvs_cmd *, int, char **, int *);
35: static int cvs_update_pre_exec(struct cvsroot *);
36: static int cvs_update_remote(CVSFILE *, void *);
37: static int cvs_update_local(CVSFILE *, void *);
1.18 joris 38:
1.29 jfb 39: struct cvs_cmd cvs_cmd_update = {
40: CVS_OP_UPDATE, CVS_REQ_UPDATE, "update",
41: { "up", "upd" },
42: "Bring work tree in sync with repository",
43: "[-ACdflPpR] [-D date | -r rev] [-I ign] [-j rev] [-k mode] "
44: "[-t id] ...",
1.35 xsa 45: "ACD:dfI:j:k:lPpQqRr:t:",
1.29 jfb 46: NULL,
1.37 joris 47: CF_SORT | CF_RECURSE | CF_IGNORE | CF_NOSYMS,
1.29 jfb 48: cvs_update_init,
49: cvs_update_pre_exec,
1.26 jfb 50: cvs_update_remote,
1.29 jfb 51: cvs_update_local,
52: NULL,
53: NULL,
1.18 joris 54: CVS_CMD_ALLOWSPEC | CVS_CMD_SENDARGS2 | CVS_CMD_SENDDIR
55: };
1.2 jfb 56:
1.35 xsa 57: static char *date, *rev, *koptstr;
1.31 joris 58: static int dflag, Aflag;
1.35 xsa 59: static int kflag = RCS_KWEXP_DEFAULT;
1.24 joris 60:
1.26 jfb 61: static int
1.29 jfb 62: cvs_update_init(struct cvs_cmd *cmd, int argc, char **argv, int *arg)
1.1 jfb 63: {
1.18 joris 64: int ch;
1.2 jfb 65:
1.31 joris 66: dflag = Aflag = 0;
1.32 xsa 67: date = NULL;
68: rev = NULL;
1.24 joris 69:
1.29 jfb 70: while ((ch = getopt(argc, argv, cmd->cmd_opts)) != -1) {
1.1 jfb 71: switch (ch) {
72: case 'A':
1.24 joris 73: Aflag = 1;
74: break;
1.1 jfb 75: case 'C':
76: case 'D':
1.32 xsa 77: date = optarg;
78: break;
1.1 jfb 79: case 'd':
1.24 joris 80: dflag = 1;
81: break;
1.1 jfb 82: case 'f':
1.2 jfb 83: break;
1.35 xsa 84: case 'I':
85: break;
86: case 'j':
87: break;
88: case 'k':
89: koptstr = optarg;
90: kflag = rcs_kflag_get(koptstr);
91: if (RCS_KWEXP_INVAL(kflag)) {
92: cvs_log(LP_ERR,
93: "invalid RCS keyword expansion mode");
94: rcs_kflag_usage();
95: return (CVS_EX_USAGE);
96: }
97: break;
1.1 jfb 98: case 'l':
1.29 jfb 99: cmd->file_flags &= ~CF_RECURSE;
1.2 jfb 100: break;
1.1 jfb 101: case 'P':
1.31 joris 102: cmd->cmd_flags |= CVS_CMD_PRUNEDIRS;
1.24 joris 103: break;
1.1 jfb 104: case 'p':
1.27 xsa 105: cvs_noexec = 1; /* no locks will be created */
106: break;
1.1 jfb 107: case 'Q':
108: case 'q':
1.2 jfb 109: break;
1.1 jfb 110: case 'R':
1.29 jfb 111: cmd->file_flags |= CF_RECURSE;
1.2 jfb 112: break;
1.1 jfb 113: case 'r':
1.32 xsa 114: rev = optarg;
1.1 jfb 115: break;
116: default:
1.21 joris 117: return (CVS_EX_USAGE);
1.1 jfb 118: }
1.32 xsa 119: }
120:
1.18 joris 121: *arg = optind;
1.2 jfb 122: return (0);
123: }
124:
1.26 jfb 125: static int
1.29 jfb 126: cvs_update_pre_exec(struct cvsroot *root)
1.24 joris 127: {
1.36 joris 128: if (root->cr_method != CVS_METHOD_LOCAL) {
1.49 joris 129: if (cvs_cmd_update.cmd_flags & CVS_CMD_PRUNEDIRS)
130: cvs_sendarg(root, "-P", 0);
131:
1.50 xsa 132: if (Aflag == 1)
1.49 joris 133: cvs_sendarg(root, "-A", 0);
134:
1.50 xsa 135: if (dflag == 1)
1.49 joris 136: cvs_sendarg(root, "-d", 0);
137:
138: if (rev != NULL) {
139: cvs_sendarg(root, "-r", 0);
140: cvs_sendarg(root, rev, 0);
141: }
142:
143: if (date != NULL) {
144: cvs_sendarg(root, "-D", 0);
145: cvs_sendarg(root, date, 0);
146: }
1.36 joris 147: }
1.35 xsa 148:
1.24 joris 149: return (0);
150: }
1.2 jfb 151:
152: /*
1.26 jfb 153: * cvs_update_remote()
1.2 jfb 154: *
1.12 jfb 155: * Update a single file. In the case where we act as client, send any
156: * pertinent information about that file to the server.
1.2 jfb 157: */
1.26 jfb 158: static int
159: cvs_update_remote(CVSFILE *cf, void *arg)
1.2 jfb 160: {
1.44 xsa 161: char fpath[MAXPATHLEN];
1.2 jfb 162: struct cvsroot *root;
163:
1.12 jfb 164: root = CVS_DIR_ROOT(cf);
1.9 jfb 165:
1.13 jfb 166: if (cf->cf_type == DT_DIR) {
1.26 jfb 167: if (cf->cf_cvstat == CVS_FST_UNKNOWN)
1.49 joris 168: cvs_sendreq(root, CVS_REQ_QUESTIONABLE, cf->cf_name);
1.26 jfb 169: else
1.49 joris 170: cvs_senddir(root, cf);
171: return (0);
1.2 jfb 172: }
173:
1.12 jfb 174: cvs_file_getpath(cf, fpath, sizeof(fpath));
1.2 jfb 175:
1.49 joris 176: cvs_sendentry(root, cf);
1.41 joris 177:
178: if (!(cf->cf_flags & CVS_FILE_ONDISK))
179: return (0);
1.2 jfb 180:
1.26 jfb 181: switch (cf->cf_cvstat) {
182: case CVS_FST_UNKNOWN:
1.49 joris 183: cvs_sendreq(root, CVS_REQ_QUESTIONABLE, cf->cf_name);
1.26 jfb 184: break;
185: case CVS_FST_UPTODATE:
1.49 joris 186: cvs_sendreq(root, CVS_REQ_UNCHANGED, cf->cf_name);
1.26 jfb 187: break;
188: case CVS_FST_ADDED:
189: case CVS_FST_MODIFIED:
1.49 joris 190: cvs_sendreq(root, CVS_REQ_MODIFIED, cf->cf_name);
191: cvs_sendfile(root, fpath);
1.26 jfb 192: break;
193: default:
194: break;
1.2 jfb 195: }
196:
1.49 joris 197: return (0);
1.2 jfb 198: }
199:
200: /*
1.26 jfb 201: * cvs_update_local()
1.2 jfb 202: */
1.26 jfb 203: static int
204: cvs_update_local(CVSFILE *cf, void *arg)
1.2 jfb 205: {
1.45 joris 206: int ret, islocal, revdiff;
1.39 xsa 207: char fpath[MAXPATHLEN], rcspath[MAXPATHLEN];
1.45 joris 208: char *repo;
1.26 jfb 209: RCSFILE *rf;
1.45 joris 210: RCSNUM *frev;
211: BUF *fbuf;
212: struct cvsroot *root;
1.26 jfb 213:
1.45 joris 214: revdiff = ret = 0;
1.26 jfb 215: rf = NULL;
1.45 joris 216: islocal = (cvs_cmdop != CVS_OP_SERVER);
1.26 jfb 217:
1.45 joris 218: root = CVS_DIR_ROOT(cf);
219: repo = CVS_DIR_REPO(cf);
1.29 jfb 220: cvs_file_getpath(cf, fpath, sizeof(fpath));
221:
1.37 joris 222: if (cf->cf_cvstat == CVS_FST_UNKNOWN) {
1.46 joris 223: if (verbosity > 1)
224: cvs_printf("? %s\n", fpath);
1.37 joris 225: return (CVS_EX_OK);
226: }
227:
1.26 jfb 228: if (cf->cf_type == DT_DIR) {
1.45 joris 229: if (verbosity > 1)
230: cvs_log(LP_NOTICE, "Updating %s", fpath);
1.26 jfb 231: return (CVS_EX_OK);
232: }
233:
1.48 xsa 234: cvs_rcs_getpath(cf, rcspath, sizeof(rcspath));
1.1 jfb 235:
1.45 joris 236: /*
237: * Only open the RCS file for files that have not been added.
238: */
239: if (cf->cf_cvstat != CVS_FST_ADDED) {
240: rf = rcs_open(rcspath, RCS_READ);
241:
242: /*
243: * If there is no RCS file available in the repository
244: * directory that matches this file, it's gone.
245: * XXX: so what about the Attic?
246: */
247: if (rf == NULL) {
248: cvs_log(LP_WARN, "%s is no longer in the repository",
249: fpath);
250: if (cvs_checkout_rev(NULL, NULL, cf, fpath,
251: islocal, CHECKOUT_REV_REMOVED) < 0)
252: return (CVS_EX_FILE);
253: return (CVS_EX_OK);
254: }
255: }
256:
257: /* set keyword expansion */
258: /* XXX look at cf->cf_opts as well for this */
259: if (rcs_kwexp_set(rf, kflag) < 0) {
260: if (rf != NULL)
261: rcs_close(rf);
1.26 jfb 262: return (CVS_EX_DATA);
263: }
264:
1.45 joris 265: /* fill in the correct revision */
266: if (rev != NULL) {
267: if ((frev = rcsnum_parse(rev)) == NULL) {
268: if (rf != NULL)
269: rcs_close(rf);
270: return (CVS_EX_DATA);
271: }
272: } else {
273: frev = rf->rf_head;
274: }
1.26 jfb 275:
1.45 joris 276: /*
277: * Compare the headrevision with the revision we currently have.
278: */
279: if (rf != NULL && cf->cf_lrev != NULL)
280: revdiff = rcsnum_cmp(cf->cf_lrev, frev, 0);
281:
282: switch (cf->cf_cvstat) {
283: case CVS_FST_MODIFIED:
284: /*
285: * If the file has been modified but there is a newer version
286: * available, we try to merge it into the existing changes.
287: */
288: if (revdiff == 1) {
289: fbuf = cvs_diff3(rf, fpath, cf->cf_lrev, frev);
290: if (fbuf == NULL) {
291: cvs_log(LP_ERR, "merge failed");
292: break;
293: }
294:
295: /*
296: * Please note fbuf will be free'd in cvs_checkout_rev
297: */
298: if (cvs_checkout_rev(rf, frev, cf, fpath, islocal,
299: CHECKOUT_REV_MERGED, fbuf) != -1) {
300: cvs_printf("%c %s\n",
301: (diff3_conflicts > 0) ? 'C' : 'M',
302: fpath);
303: if (diff3_conflicts > 0)
304: cf->cf_cvstat = CVS_FST_CONFLICT;
305: }
306: } else {
307: cvs_printf("M %s\n", fpath);
308: }
309: break;
310: case CVS_FST_ADDED:
311: cvs_printf("A %s\n", fpath);
312: break;
313: case CVS_FST_REMOVED:
314: cvs_printf("R %s\n", fpath);
315: break;
316: case CVS_FST_CONFLICT:
317: cvs_printf("C %s\n", fpath);
318: break;
319: case CVS_FST_LOST:
320: if (cvs_checkout_rev(rf, frev, cf, fpath, islocal,
1.47 joris 321: CHECKOUT_REV_UPDATED) != -1) {
1.45 joris 322: cf->cf_cvstat = CVS_FST_UPTODATE;
323: cvs_printf("U %s\n", fpath);
324: }
325: break;
326: case CVS_FST_UPTODATE:
327: if (revdiff == 1) {
328: if (cvs_checkout_rev(rf, frev, cf, fpath, islocal,
1.47 joris 329: CHECKOUT_REV_UPDATED) != -1)
1.45 joris 330: cvs_printf("P %s\n", fpath);
331: }
332: break;
333: default:
334: break;
335: }
336:
337: if ((frev != NULL) && (frev != rf->rf_head))
338: rcsnum_free(frev);
339: if (rf != NULL)
340: rcs_close(rf);
341:
342: return (CVS_EX_OK);
1.1 jfb 343: }