Annotation of src/usr.bin/cvs/update.c, Revision 1.49
1.49 ! joris 1: /* $OpenBSD: update.c,v 1.48 2005/12/22 14:59:54 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:
27: #include <sys/stat.h>
28:
29: #include <errno.h>
1.34 xsa 30: #include <fcntl.h>
1.1 jfb 31: #include <stdio.h>
32: #include <stdlib.h>
1.34 xsa 33: #include <string.h>
1.1 jfb 34: #include <unistd.h>
35:
36: #include "cvs.h"
37: #include "log.h"
1.4 jfb 38: #include "proto.h"
1.45 joris 39: #include "diff.h"
1.1 jfb 40:
1.42 xsa 41: static int cvs_update_init(struct cvs_cmd *, int, char **, int *);
42: static int cvs_update_pre_exec(struct cvsroot *);
43: static int cvs_update_remote(CVSFILE *, void *);
44: static int cvs_update_local(CVSFILE *, void *);
1.18 joris 45:
1.29 jfb 46: struct cvs_cmd cvs_cmd_update = {
47: CVS_OP_UPDATE, CVS_REQ_UPDATE, "update",
48: { "up", "upd" },
49: "Bring work tree in sync with repository",
50: "[-ACdflPpR] [-D date | -r rev] [-I ign] [-j rev] [-k mode] "
51: "[-t id] ...",
1.35 xsa 52: "ACD:dfI:j:k:lPpQqRr:t:",
1.29 jfb 53: NULL,
1.37 joris 54: CF_SORT | CF_RECURSE | CF_IGNORE | CF_NOSYMS,
1.29 jfb 55: cvs_update_init,
56: cvs_update_pre_exec,
1.26 jfb 57: cvs_update_remote,
1.29 jfb 58: cvs_update_local,
59: NULL,
60: NULL,
1.18 joris 61: CVS_CMD_ALLOWSPEC | CVS_CMD_SENDARGS2 | CVS_CMD_SENDDIR
62: };
1.2 jfb 63:
1.35 xsa 64: static char *date, *rev, *koptstr;
1.31 joris 65: static int dflag, Aflag;
1.35 xsa 66: static int kflag = RCS_KWEXP_DEFAULT;
1.24 joris 67:
1.26 jfb 68: static int
1.29 jfb 69: cvs_update_init(struct cvs_cmd *cmd, int argc, char **argv, int *arg)
1.1 jfb 70: {
1.18 joris 71: int ch;
1.2 jfb 72:
1.31 joris 73: dflag = Aflag = 0;
1.32 xsa 74: date = NULL;
75: rev = NULL;
1.24 joris 76:
1.29 jfb 77: while ((ch = getopt(argc, argv, cmd->cmd_opts)) != -1) {
1.1 jfb 78: switch (ch) {
79: case 'A':
1.24 joris 80: Aflag = 1;
81: break;
1.1 jfb 82: case 'C':
83: case 'D':
1.32 xsa 84: date = optarg;
85: break;
1.1 jfb 86: case 'd':
1.24 joris 87: dflag = 1;
88: break;
1.1 jfb 89: case 'f':
1.2 jfb 90: break;
1.35 xsa 91: case 'I':
92: break;
93: case 'j':
94: break;
95: case 'k':
96: koptstr = optarg;
97: kflag = rcs_kflag_get(koptstr);
98: if (RCS_KWEXP_INVAL(kflag)) {
99: cvs_log(LP_ERR,
100: "invalid RCS keyword expansion mode");
101: rcs_kflag_usage();
102: return (CVS_EX_USAGE);
103: }
104: break;
1.1 jfb 105: case 'l':
1.29 jfb 106: cmd->file_flags &= ~CF_RECURSE;
1.2 jfb 107: break;
1.1 jfb 108: case 'P':
1.31 joris 109: cmd->cmd_flags |= CVS_CMD_PRUNEDIRS;
1.24 joris 110: break;
1.1 jfb 111: case 'p':
1.27 xsa 112: cvs_noexec = 1; /* no locks will be created */
113: break;
1.1 jfb 114: case 'Q':
115: case 'q':
1.2 jfb 116: break;
1.1 jfb 117: case 'R':
1.29 jfb 118: cmd->file_flags |= CF_RECURSE;
1.2 jfb 119: break;
1.1 jfb 120: case 'r':
1.32 xsa 121: rev = optarg;
1.1 jfb 122: break;
123: default:
1.21 joris 124: return (CVS_EX_USAGE);
1.1 jfb 125: }
1.32 xsa 126: }
127:
1.18 joris 128: *arg = optind;
1.2 jfb 129: return (0);
130: }
131:
1.26 jfb 132: static int
1.29 jfb 133: cvs_update_pre_exec(struct cvsroot *root)
1.24 joris 134: {
1.36 joris 135: if (root->cr_method != CVS_METHOD_LOCAL) {
1.49 ! joris 136: if (cvs_cmd_update.cmd_flags & CVS_CMD_PRUNEDIRS)
! 137: cvs_sendarg(root, "-P", 0);
! 138:
! 139: if (Aflag)
! 140: cvs_sendarg(root, "-A", 0);
! 141:
! 142: if (dflag)
! 143: cvs_sendarg(root, "-d", 0);
! 144:
! 145: if (rev != NULL) {
! 146: cvs_sendarg(root, "-r", 0);
! 147: cvs_sendarg(root, rev, 0);
! 148: }
! 149:
! 150: if (date != NULL) {
! 151: cvs_sendarg(root, "-D", 0);
! 152: cvs_sendarg(root, date, 0);
! 153: }
1.36 joris 154: }
1.35 xsa 155:
1.24 joris 156: return (0);
157: }
1.2 jfb 158:
159: /*
1.26 jfb 160: * cvs_update_remote()
1.2 jfb 161: *
1.12 jfb 162: * Update a single file. In the case where we act as client, send any
163: * pertinent information about that file to the server.
1.2 jfb 164: */
1.26 jfb 165: static int
166: cvs_update_remote(CVSFILE *cf, void *arg)
1.2 jfb 167: {
1.44 xsa 168: char fpath[MAXPATHLEN];
1.2 jfb 169: struct cvsroot *root;
170:
1.12 jfb 171: root = CVS_DIR_ROOT(cf);
1.9 jfb 172:
1.13 jfb 173: if (cf->cf_type == DT_DIR) {
1.26 jfb 174: if (cf->cf_cvstat == CVS_FST_UNKNOWN)
1.49 ! joris 175: cvs_sendreq(root, CVS_REQ_QUESTIONABLE, cf->cf_name);
1.26 jfb 176: else
1.49 ! joris 177: cvs_senddir(root, cf);
! 178: return (0);
1.2 jfb 179: }
180:
1.12 jfb 181: cvs_file_getpath(cf, fpath, sizeof(fpath));
1.2 jfb 182:
1.49 ! joris 183: cvs_sendentry(root, cf);
1.41 joris 184:
185: if (!(cf->cf_flags & CVS_FILE_ONDISK))
186: return (0);
1.2 jfb 187:
1.26 jfb 188: switch (cf->cf_cvstat) {
189: case CVS_FST_UNKNOWN:
1.49 ! joris 190: cvs_sendreq(root, CVS_REQ_QUESTIONABLE, cf->cf_name);
1.26 jfb 191: break;
192: case CVS_FST_UPTODATE:
1.49 ! joris 193: cvs_sendreq(root, CVS_REQ_UNCHANGED, cf->cf_name);
1.26 jfb 194: break;
195: case CVS_FST_ADDED:
196: case CVS_FST_MODIFIED:
1.49 ! joris 197: cvs_sendreq(root, CVS_REQ_MODIFIED, cf->cf_name);
! 198: cvs_sendfile(root, fpath);
1.26 jfb 199: break;
200: default:
201: break;
1.2 jfb 202: }
203:
1.49 ! joris 204: return (0);
1.2 jfb 205: }
206:
207: /*
1.26 jfb 208: * cvs_update_local()
1.2 jfb 209: */
1.26 jfb 210: static int
211: cvs_update_local(CVSFILE *cf, void *arg)
1.2 jfb 212: {
1.45 joris 213: int ret, islocal, revdiff;
1.39 xsa 214: char fpath[MAXPATHLEN], rcspath[MAXPATHLEN];
1.45 joris 215: char *repo;
1.26 jfb 216: RCSFILE *rf;
1.45 joris 217: RCSNUM *frev;
218: BUF *fbuf;
219: struct cvsroot *root;
1.26 jfb 220:
1.45 joris 221: revdiff = ret = 0;
1.26 jfb 222: rf = NULL;
1.45 joris 223: islocal = (cvs_cmdop != CVS_OP_SERVER);
1.26 jfb 224:
1.45 joris 225: root = CVS_DIR_ROOT(cf);
226: repo = CVS_DIR_REPO(cf);
1.29 jfb 227: cvs_file_getpath(cf, fpath, sizeof(fpath));
228:
1.37 joris 229: if (cf->cf_cvstat == CVS_FST_UNKNOWN) {
1.46 joris 230: if (verbosity > 1)
231: cvs_printf("? %s\n", fpath);
1.37 joris 232: return (CVS_EX_OK);
233: }
234:
1.26 jfb 235: if (cf->cf_type == DT_DIR) {
1.45 joris 236: if (verbosity > 1)
237: cvs_log(LP_NOTICE, "Updating %s", fpath);
1.26 jfb 238: return (CVS_EX_OK);
239: }
240:
1.48 xsa 241: cvs_rcs_getpath(cf, rcspath, sizeof(rcspath));
1.1 jfb 242:
1.45 joris 243: /*
244: * Only open the RCS file for files that have not been added.
245: */
246: if (cf->cf_cvstat != CVS_FST_ADDED) {
247: rf = rcs_open(rcspath, RCS_READ);
248:
249: /*
250: * If there is no RCS file available in the repository
251: * directory that matches this file, it's gone.
252: * XXX: so what about the Attic?
253: */
254: if (rf == NULL) {
255: cvs_log(LP_WARN, "%s is no longer in the repository",
256: fpath);
257: if (cvs_checkout_rev(NULL, NULL, cf, fpath,
258: islocal, CHECKOUT_REV_REMOVED) < 0)
259: return (CVS_EX_FILE);
260: return (CVS_EX_OK);
261: }
262: }
263:
264: /* set keyword expansion */
265: /* XXX look at cf->cf_opts as well for this */
266: if (rcs_kwexp_set(rf, kflag) < 0) {
267: if (rf != NULL)
268: rcs_close(rf);
1.26 jfb 269: return (CVS_EX_DATA);
270: }
271:
1.45 joris 272: /* fill in the correct revision */
273: if (rev != NULL) {
274: if ((frev = rcsnum_parse(rev)) == NULL) {
275: if (rf != NULL)
276: rcs_close(rf);
277: return (CVS_EX_DATA);
278: }
279: } else {
280: frev = rf->rf_head;
281: }
1.26 jfb 282:
1.45 joris 283: /*
284: * Compare the headrevision with the revision we currently have.
285: */
286: if (rf != NULL && cf->cf_lrev != NULL)
287: revdiff = rcsnum_cmp(cf->cf_lrev, frev, 0);
288:
289: switch (cf->cf_cvstat) {
290: case CVS_FST_MODIFIED:
291: /*
292: * If the file has been modified but there is a newer version
293: * available, we try to merge it into the existing changes.
294: */
295: if (revdiff == 1) {
296: fbuf = cvs_diff3(rf, fpath, cf->cf_lrev, frev);
297: if (fbuf == NULL) {
298: cvs_log(LP_ERR, "merge failed");
299: break;
300: }
301:
302: /*
303: * Please note fbuf will be free'd in cvs_checkout_rev
304: */
305: if (cvs_checkout_rev(rf, frev, cf, fpath, islocal,
306: CHECKOUT_REV_MERGED, fbuf) != -1) {
307: cvs_printf("%c %s\n",
308: (diff3_conflicts > 0) ? 'C' : 'M',
309: fpath);
310: if (diff3_conflicts > 0)
311: cf->cf_cvstat = CVS_FST_CONFLICT;
312: }
313: } else {
314: cvs_printf("M %s\n", fpath);
315: }
316: break;
317: case CVS_FST_ADDED:
318: cvs_printf("A %s\n", fpath);
319: break;
320: case CVS_FST_REMOVED:
321: cvs_printf("R %s\n", fpath);
322: break;
323: case CVS_FST_CONFLICT:
324: cvs_printf("C %s\n", fpath);
325: break;
326: case CVS_FST_LOST:
327: if (cvs_checkout_rev(rf, frev, cf, fpath, islocal,
1.47 joris 328: CHECKOUT_REV_UPDATED) != -1) {
1.45 joris 329: cf->cf_cvstat = CVS_FST_UPTODATE;
330: cvs_printf("U %s\n", fpath);
331: }
332: break;
333: case CVS_FST_UPTODATE:
334: if (revdiff == 1) {
335: if (cvs_checkout_rev(rf, frev, cf, fpath, islocal,
1.47 joris 336: CHECKOUT_REV_UPDATED) != -1)
1.45 joris 337: cvs_printf("P %s\n", fpath);
338: }
339: break;
340: default:
341: break;
342: }
343:
344: if ((frev != NULL) && (frev != rf->rf_head))
345: rcsnum_free(frev);
346: if (rf != NULL)
347: rcs_close(rf);
348:
349: return (CVS_EX_OK);
1.1 jfb 350: }