Annotation of src/usr.bin/rcs/ci.c, Revision 1.142
1.142 ! joris 1: /* $OpenBSD: ci.c,v 1.141 2006/04/13 00:58:25 ray Exp $ */
1.1 niallo 2: /*
1.98 niallo 3: * Copyright (c) 2005, 2006 Niall O'Higgins <niallo@openbsd.org>
1.1 niallo 4: * All rights reserved.
5: *
6: * Redistribution and use in source and binary forms, with or without
7: * modification, are permitted provided that the following cinditions
8: * are met:
9: *
10: * 1. Redistributions of source cide must retain the above cipyright
11: * notice, this list of cinditions and the following disclaimer.
12: * 2. The name of the author may not be used to endorse or promote products
13: * derived from this software without specific prior written permission.
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
24: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25: */
26:
1.91 xsa 27: #include "includes.h"
1.1 niallo 28:
1.92 xsa 29: #include "rcsprog.h"
1.4 niallo 30: #include "diff.h"
1.9 niallo 31:
1.136 xsa 32: #define CI_OPTSTRING "d::f::I::i::j::k::l::m:M::N:n:qr::s:Tt:u::Vw:x::z::"
1.107 deraadt 33: #define DATE_NOW -1
34: #define DATE_MTIME -2
35:
36: #define KW_ID "Id"
37: #define KW_AUTHOR "Author"
38: #define KW_DATE "Date"
39: #define KW_STATE "State"
40: #define KW_REVISION "Revision"
41:
42: #define KW_TYPE_ID 1
43: #define KW_TYPE_AUTHOR 2
44: #define KW_TYPE_DATE 3
45: #define KW_TYPE_STATE 4
46: #define KW_TYPE_REVISION 5
47:
48: #define KW_NUMTOKS_ID 10
49: #define KW_NUMTOKS_AUTHOR 3
50: #define KW_NUMTOKS_DATE 4
51: #define KW_NUMTOKS_STATE 3
52: #define KW_NUMTOKS_REVISION 3
1.58 niallo 53:
1.115 niallo 54: #define RCSNUM_ZERO_ENDING(x) (x->rn_id[x->rn_len - 1] == 0)
55:
1.98 niallo 56: extern struct rcs_kw rcs_expkw[];
57:
1.58 niallo 58: struct checkin_params {
59: int flags, openflags;
60: mode_t fmode;
61: time_t date;
62: RCSFILE *file;
63: RCSNUM *frev, *newrev;
64: char fpath[MAXPATHLEN], *rcs_msg, *username, *deltatext, *filename;
1.98 niallo 65: char *author, *state;
66: const char *symbol, *description;
1.58 niallo 67: };
68:
1.94 niallo 69: static int checkin_attach_symbol(struct checkin_params *);
70: static int checkin_checklock(struct checkin_params *);
1.68 xsa 71: static char *checkin_diff_file(struct checkin_params *);
72: static char *checkin_getdesc(void);
73: static char *checkin_getinput(const char *);
74: static char *checkin_getlogmsg(RCSNUM *, RCSNUM *);
75: static int checkin_init(struct checkin_params *);
1.107 deraadt 76: static int checkin_keywordscan(char *, RCSNUM **, time_t *, char **,
1.98 niallo 77: char **);
1.107 deraadt 78: static int checkin_keywordtype(char *);
1.94 niallo 79: static int checkin_mtimedate(struct checkin_params *);
1.107 deraadt 80: static void checkin_parsekeyword(char *, RCSNUM **, time_t *, char **,
1.98 niallo 81: char **);
1.94 niallo 82: static int checkin_update(struct checkin_params *);
83: static void checkin_revert(struct checkin_params *);
1.4 niallo 84:
1.1 niallo 85: void
86: checkin_usage(void)
87: {
88: fprintf(stderr,
1.119 xsa 89: "usage: ci [-jMNqV] [-d[date]] [-f[rev]] [-I[rev]] [-i[rev]]\n"
90: " [-j[rev]] [-k[rev]] [-l[rev]] [-M[rev]] [-mmsg]\n"
91: " [-Nsymbol] [-nsymbol] [-r[rev]] [-sstate] [-tfile|str]\n"
92: " [-u[rev]] [-wusername] [-xsuffixes] [-ztz] file ...\n");
1.58 niallo 93: }
94:
1.1 niallo 95: /*
96: * checkin_main()
97: *
98: * Handler for the `ci' program.
99: * Returns 0 on success, or >0 on error.
100: */
101: int
102: checkin_main(int argc, char **argv)
103: {
1.58 niallo 104: int i, ch, status;
1.139 ray 105: char *rev_str;
1.58 niallo 106: struct checkin_params pb;
1.1 niallo 107:
1.58 niallo 108: pb.date = DATE_NOW;
109: pb.file = NULL;
1.98 niallo 110: pb.rcs_msg = pb.username = pb.author = pb.state = NULL;
1.134 niallo 111: pb.symbol = pb.description = pb.deltatext = NULL;
1.58 niallo 112: pb.newrev = NULL;
1.128 niallo 113: pb.flags = status = 0;
114: pb.fmode = S_IRUSR|S_IRGRP|S_IROTH;
1.120 joris 115: pb.flags = INTERACTIVE;
1.90 niallo 116: pb.openflags = RCS_RDWR|RCS_CREATE|RCS_PARSE_FULLY;
1.139 ray 117: rev_str = NULL;
1.9 niallo 118:
1.58 niallo 119: while ((ch = rcs_getopt(argc, argv, CI_OPTSTRING)) != -1) {
1.1 niallo 120: switch (ch) {
1.20 niallo 121: case 'd':
1.25 niallo 122: if (rcs_optarg == NULL)
1.58 niallo 123: pb.date = DATE_MTIME;
1.87 xsa 124: else if ((pb.date = cvs_date_parse(rcs_optarg)) <= 0)
125: fatal("invalid date");
1.20 niallo 126: break;
1.29 niallo 127: case 'f':
1.139 ray 128: rcs_setrevstr(&rev_str, rcs_optarg);
1.58 niallo 129: pb.flags |= FORCE;
1.29 niallo 130: break;
1.1 niallo 131: case 'h':
132: (usage)();
133: exit(0);
1.121 ray 134: /* NOTREACHED */
1.119 xsa 135: case 'I':
1.139 ray 136: rcs_setrevstr(&rev_str, rcs_optarg);
1.119 xsa 137: pb.flags |= INTERACTIVE;
138: break;
1.58 niallo 139: case 'i':
1.139 ray 140: rcs_setrevstr(&rev_str, rcs_optarg);
1.58 niallo 141: pb.openflags |= RCS_CREATE;
1.66 niallo 142: pb.flags |= CI_INIT;
1.58 niallo 143: break;
144: case 'j':
1.139 ray 145: rcs_setrevstr(&rev_str, rcs_optarg);
1.58 niallo 146: pb.openflags &= ~RCS_CREATE;
1.66 niallo 147: pb.flags &= ~CI_INIT;
1.58 niallo 148: break;
1.98 niallo 149: case 'k':
1.139 ray 150: rcs_setrevstr(&rev_str, rcs_optarg);
1.98 niallo 151: pb.flags |= CI_KEYWORDSCAN;
152: break;
1.30 niallo 153: case 'l':
1.139 ray 154: rcs_setrevstr(&rev_str, rcs_optarg);
1.58 niallo 155: pb.flags |= CO_LOCK;
1.54 niallo 156: break;
157: case 'M':
1.139 ray 158: rcs_setrevstr(&rev_str, rcs_optarg);
1.58 niallo 159: pb.flags |= CO_REVDATE;
1.30 niallo 160: break;
1.1 niallo 161: case 'm':
1.58 niallo 162: pb.rcs_msg = rcs_optarg;
1.87 xsa 163: if (pb.rcs_msg == NULL)
164: fatal("missing message for -m option");
1.58 niallo 165: pb.flags &= ~INTERACTIVE;
1.3 joris 166: break;
1.42 niallo 167: case 'N':
1.84 joris 168: pb.symbol = xstrdup(rcs_optarg);
1.87 xsa 169: if (rcs_sym_check(pb.symbol) != 1)
170: fatal("invalid symbol `%s'", pb.symbol);
1.58 niallo 171: pb.flags |= CI_SYMFORCE;
1.42 niallo 172: break;
1.38 niallo 173: case 'n':
1.84 joris 174: pb.symbol = xstrdup(rcs_optarg);
1.87 xsa 175: if (rcs_sym_check(pb.symbol) != 1)
176: fatal("invalid symbol `%s'", pb.symbol);
1.38 niallo 177: break;
1.3 joris 178: case 'q':
179: verbose = 0;
1.1 niallo 180: break;
1.30 niallo 181: case 'r':
1.139 ray 182: rcs_setrevstr(&rev_str, rcs_optarg);
1.58 niallo 183: pb.flags |= CI_DEFAULT;
1.9 niallo 184: break;
1.51 niallo 185: case 's':
1.58 niallo 186: pb.state = rcs_optarg;
1.87 xsa 187: if (rcs_state_check(pb.state) < 0)
188: fatal("invalid state `%s'", pb.state);
1.67 xsa 189: break;
190: case 'T':
191: pb.flags |= PRESERVETIME;
1.51 niallo 192: break;
1.80 niallo 193: case 't':
1.84 joris 194: pb.description = xstrdup(rcs_optarg);
1.80 niallo 195: break;
1.9 niallo 196: case 'u':
1.139 ray 197: rcs_setrevstr(&rev_str, rcs_optarg);
1.58 niallo 198: pb.flags |= CO_UNLOCK;
1.9 niallo 199: break;
1.30 niallo 200: case 'V':
201: printf("%s\n", rcs_version);
202: exit(0);
1.121 ray 203: /* NOTREACHED */
1.31 niallo 204: case 'w':
1.84 joris 205: pb.author = xstrdup(rcs_optarg);
1.64 xsa 206: break;
207: case 'x':
1.125 ray 208: /* Use blank extension if none given. */
209: rcs_suffixes = rcs_optarg ? rcs_optarg : "";
1.110 joris 210: break;
211: case 'z':
212: timezone_flag = rcs_optarg;
1.31 niallo 213: break;
1.1 niallo 214: default:
215: (usage)();
216: exit(1);
217: }
218: }
219:
1.24 joris 220: argc -= rcs_optind;
221: argv += rcs_optind;
222:
1.1 niallo 223: if (argc == 0) {
224: cvs_log(LP_ERR, "no input file");
225: (usage)();
226: exit(1);
227: }
228:
1.86 xsa 229: if ((pb.username = getlogin()) == NULL)
230: fatal("getlogin failed");
1.31 niallo 231:
232:
1.1 niallo 233: for (i = 0; i < argc; i++) {
1.58 niallo 234: pb.filename = argv[i];
1.1 niallo 235:
1.58 niallo 236: /*
237: * Test for existence of ,v file. If we are expected to
238: * create one, set NEWFILE flag.
239: */
1.71 niallo 240: if (rcs_statfile(pb.filename, pb.fpath, sizeof(pb.fpath)) < 0) {
241: if (pb.openflags & RCS_CREATE)
242: pb.flags |= NEWFILE;
243: else {
244: cvs_log(LP_ERR, "No existing RCS file");
245: status = 1;
246: continue;
247: }
1.66 niallo 248: } else {
249: if (pb.flags & CI_INIT) {
250: cvs_log(LP_ERR, "%s already exists", pb.fpath);
251: status = 1;
252: continue;
253: }
1.58 niallo 254: pb.openflags &= ~RCS_CREATE;
1.66 niallo 255: }
1.63 niallo 256: /*
257: * If we are to create a new ,v file, we must decide where it
258: * should go.
259: */
260: if (pb.flags & NEWFILE) {
1.117 ray 261: char *fpath = rcs_choosefile(pb.filename);
1.63 niallo 262: if (fpath == NULL) {
263: status = 1;
264: continue;
265: }
266: strlcpy(pb.fpath, fpath, sizeof(pb.fpath));
1.84 joris 267: xfree(fpath);
1.63 niallo 268: }
269:
1.58 niallo 270: pb.file = rcs_open(pb.fpath, pb.openflags, pb.fmode);
271:
1.87 xsa 272: if (pb.file == NULL)
273: fatal("failed to open rcsfile '%s'", pb.fpath);
1.29 niallo 274:
1.58 niallo 275: if (verbose == 1)
276: printf("%s <-- %s\n", pb.fpath, pb.filename);
1.139 ray 277:
278: /* XXX - Should we rcsnum_free(pb.newrev)? */
279: if (rev_str != NULL)
1.141 ray 280: if ((pb.newrev = rcs_getrevnum(rev_str, pb.file)) ==
281: NULL)
282: fatal("invalid revision: %s", rev_str);
1.107 deraadt 283:
1.142 ! joris 284: /* XXX - support for commiting to a file without revisions */
! 285: if (pb.file->rf_ndelta == 0) {
! 286: pb.flags |= NEWFILE;
! 287: pb.file->rf_flags |= RCS_CREATE;
! 288: }
! 289:
1.63 niallo 290: if (pb.flags & NEWFILE)
291: status = checkin_init(&pb);
1.15 niallo 292: else
1.63 niallo 293: status = checkin_update(&pb);
1.114 niallo 294:
295: /* reset NEWFILE flag */
296: pb.flags &= ~NEWFILE;
1.129 niallo 297:
298: rcs_close(pb.file);
1.4 niallo 299: }
300:
1.114 niallo 301: if ((verbose == 1) && (status == 0))
1.93 xsa 302: printf("done\n");
303:
1.16 niallo 304: return (status);
1.4 niallo 305: }
306:
1.59 niallo 307: /*
308: * checkin_diff_file()
309: *
1.65 xsa 310: * Generate the diff between the working file and a revision.
1.59 niallo 311: * Returns pointer to a char array on success, NULL on failure.
312: */
1.4 niallo 313: static char *
1.58 niallo 314: checkin_diff_file(struct checkin_params *pb)
1.4 niallo 315: {
316: char path1[MAXPATHLEN], path2[MAXPATHLEN];
317: BUF *b1, *b2, *b3;
318: char rbuf[64], *deltatext;
319:
1.112 joris 320: b1 = b2 = b3 = NULL;
321: deltatext = NULL;
1.58 niallo 322: rcsnum_tostr(pb->frev, rbuf, sizeof(rbuf));
1.4 niallo 323:
1.58 niallo 324: if ((b1 = cvs_buf_load(pb->filename, BUF_AUTOEXT)) == NULL) {
325: cvs_log(LP_ERR, "failed to load file: '%s'", pb->filename);
1.112 joris 326: goto out;
1.1 niallo 327: }
328:
1.58 niallo 329: if ((b2 = rcs_getrev(pb->file, pb->frev)) == NULL) {
1.4 niallo 330: cvs_log(LP_ERR, "failed to load revision");
1.112 joris 331: goto out;
1.4 niallo 332: }
333:
1.57 xsa 334: if ((b3 = cvs_buf_alloc((size_t)128, BUF_AUTOEXT)) == NULL) {
1.4 niallo 335: cvs_log(LP_ERR, "failed to allocated buffer for diff");
1.112 joris 336: goto out;
1.4 niallo 337: }
338:
1.50 xsa 339: strlcpy(path1, rcs_tmpdir, sizeof(path1));
340: strlcat(path1, "/diff1.XXXXXXXXXX", sizeof(path1));
1.123 niallo 341: cvs_buf_write_stmp(b1, path1, 0600);
1.112 joris 342:
1.4 niallo 343: cvs_buf_free(b1);
1.112 joris 344: b1 = NULL;
1.4 niallo 345:
1.50 xsa 346: strlcpy(path2, rcs_tmpdir, sizeof(path2));
347: strlcat(path2, "/diff2.XXXXXXXXXX", sizeof(path2));
1.123 niallo 348: cvs_buf_write_stmp(b2, path2, 0600);
1.112 joris 349:
1.4 niallo 350: cvs_buf_free(b2);
1.112 joris 351: b2 = NULL;
1.4 niallo 352:
1.5 niallo 353: diff_format = D_RCSDIFF;
1.4 niallo 354: cvs_diffreg(path1, path2, b3);
355:
356: cvs_buf_putc(b3, '\0');
357: deltatext = (char *)cvs_buf_release(b3);
1.112 joris 358: b3 = NULL;
359:
360: out:
361: cvs_worklist_run(&rcs_temp_files, cvs_worklist_unlink);
362:
363: if (b1 != NULL)
364: cvs_buf_free(b1);
365: if (b2 != NULL)
366: cvs_buf_free(b2);
367: if (b3 != NULL)
368: cvs_buf_free(b3);
1.4 niallo 369:
370: return (deltatext);
1.6 niallo 371: }
372:
373: /*
1.65 xsa 374: * checkin_getlogmsg()
1.59 niallo 375: *
1.6 niallo 376: * Get log message from user interactively.
1.59 niallo 377: * Returns pointer to a char array on success, NULL on failure.
1.6 niallo 378: */
379: static char *
1.34 niallo 380: checkin_getlogmsg(RCSNUM *rev, RCSNUM *rev2)
1.6 niallo 381: {
1.58 niallo 382: char *rcs_msg, nrev[16], prev[16];
1.6 niallo 383: RCSNUM *tmprev;
384:
1.7 niallo 385: rcs_msg = NULL;
1.6 niallo 386: tmprev = rcsnum_alloc();
387: rcsnum_cpy(rev, tmprev, 16);
1.9 niallo 388: rcsnum_tostr(tmprev, prev, sizeof(prev));
1.12 niallo 389: if (rev2 == NULL)
390: rcsnum_tostr(rcsnum_inc(tmprev), nrev, sizeof(nrev));
391: else
392: rcsnum_tostr(rev2, nrev, sizeof(nrev));
1.6 niallo 393: rcsnum_free(tmprev);
394:
1.47 niallo 395: if (verbose == 1)
396: printf("new revision: %s; previous revision: %s\n", nrev,
397: prev);
1.32 joris 398:
1.107 deraadt 399: rcs_msg = checkin_getinput("enter log message, terminated with a "
400: "single '.' or end of file:\n>> ");
1.58 niallo 401: return (rcs_msg);
402: }
403:
404:
405: /*
406: * checkin_getdesc()
407: *
408: * Get file description interactively.
1.59 niallo 409: * Returns pointer to a char array on success, NULL on failure.
1.58 niallo 410: */
411: static char *
412: checkin_getdesc()
413: {
414: char *description;
415:
1.107 deraadt 416: description = checkin_getinput("enter description, terminated with "
417: "single '.' or end of file:\n"
418: "NOTE: This is NOT the log message!\n>> ");
1.58 niallo 419: return (description);
420: }
421:
422: /*
423: * checkin_getinput()
424: *
1.59 niallo 425: * Get some input from the user, in RCS style, prompting with message <prompt>.
426: * Returns pointer to a char array on success, NULL on failure.
1.58 niallo 427: */
428: static char *
429: checkin_getinput(const char *prompt)
430: {
431: BUF *inputbuf;
432: char *input, buf[128];
433:
434: if ((inputbuf = cvs_buf_alloc((size_t)64, BUF_AUTOEXT)) == NULL) {
435: cvs_log(LP_ERR, "failed to allocate input buffer");
436: return (NULL);
437: }
438:
1.119 xsa 439: if (isatty(STDIN_FILENO))
440: fprintf(stderr, "%s", prompt);
441:
1.6 niallo 442: for (;;) {
443: fgets(buf, (int)sizeof(buf), stdin);
1.9 niallo 444: if (feof(stdin) || ferror(stdin) || buf[0] == '.')
1.6 niallo 445: break;
1.58 niallo 446: cvs_buf_append(inputbuf, buf, strlen(buf));
1.119 xsa 447:
448: if (isatty(STDIN_FILENO))
449: fprintf(stderr, ">> ");
1.6 niallo 450: }
1.32 joris 451:
1.58 niallo 452: cvs_buf_putc(inputbuf, '\0');
453: input = (char *)cvs_buf_release(inputbuf);
454:
455: return (input);
456: }
457:
458: /*
1.63 niallo 459: * checkin_update()
460: *
461: * Do a checkin to an existing RCS file.
462: *
463: * On success, return 0. On error return -1.
464: */
465: static int
466: checkin_update(struct checkin_params *pb)
467: {
1.77 xsa 468: char *filec, numb1[64], numb2[64];
1.128 niallo 469: struct stat st;
1.63 niallo 470: BUF *bp;
471:
1.82 joris 472: /*
473: * XXX this is wrong, we need to get the revision the user
1.85 xsa 474: * has the lock for. So we can decide if we want to create a
1.82 joris 475: * branch or not. (if it's not current HEAD we need to branch).
476: */
1.63 niallo 477: pb->frev = pb->file->rf_head;
478:
1.126 niallo 479: /* Load file contents */
480: if ((bp = cvs_buf_load(pb->filename, BUF_AUTOEXT)) == NULL) {
481: rcs_close(pb->file);
482: return (-1);
483: }
484:
485: cvs_buf_putc(bp, '\0');
486: filec = (char *)cvs_buf_release(bp);
487:
1.115 niallo 488: /* If this is a zero-ending RCSNUM eg 4.0, increment it (eg to 4.1) */
489: if ((pb->newrev != NULL) && (RCSNUM_ZERO_ENDING(pb->newrev)))
490: pb->newrev = rcsnum_inc(pb->newrev);
491:
1.82 joris 492: if (checkin_checklock(pb) < 0)
1.126 niallo 493: goto fail;
1.82 joris 494:
495: /* If revision passed on command line is less than HEAD, bail.
496: * XXX only applies to ci -r1.2 foo for example if HEAD is > 1.2 and
497: * there is no lock set for the user.
498: */
1.63 niallo 499: if ((pb->newrev != NULL)
500: && (rcsnum_cmp(pb->newrev, pb->frev, 0) > 0)) {
1.77 xsa 501: cvs_log(LP_ERR,
502: "%s: revision %s too low; must be higher than %s",
503: pb->file->rf_path,
504: rcsnum_tostr(pb->newrev, numb1, sizeof(numb1)),
505: rcsnum_tostr(pb->frev, numb2, sizeof(numb2)));
1.126 niallo 506: goto fail;
1.63 niallo 507: }
508:
1.104 niallo 509: /*
510: * Set the date of the revision to be the last modification
511: * time of the working file if -d has no argument.
512: */
513: if (pb->date == DATE_MTIME
514: && (checkin_mtimedate(pb) < 0))
1.126 niallo 515: goto fail;
1.104 niallo 516:
517: /* Date from argv/mtime must be more recent than HEAD */
518: if (pb->date != DATE_NOW) {
519: time_t head_date = rcs_rev_getdate(pb->file, pb->frev);
520: if (pb->date <= head_date) {
1.113 xsa 521: char dbuf1[256], dbuf2[256], *fmt;
522: struct tm *t, *t_head;
523:
524: fmt = "%Y/%m/%d %H:%M:%S";
525:
526: t = localtime(&pb->date);
527: strftime(dbuf1, sizeof(dbuf1), fmt, t);
528: t_head = localtime(&head_date);
529: strftime(dbuf2, sizeof(dbuf2), fmt, t_head);
530:
531: fatal("%s: Date %s preceeds %s in revision %s.",
532: pb->file->rf_path, dbuf1, dbuf2,
1.104 niallo 533: rcsnum_tostr(pb->frev, numb2, sizeof(numb2)));
534: }
535: }
536:
1.73 xsa 537: /* Get RCS patch */
1.63 niallo 538: if ((pb->deltatext = checkin_diff_file(pb)) == NULL) {
539: cvs_log(LP_ERR, "failed to get diff");
1.126 niallo 540: goto fail;
1.63 niallo 541: }
542:
543: /*
544: * If -f is not specified and there are no differences, tell
545: * the user and revert to latest version.
546: */
547: if (!(pb->flags & FORCE) && (strlen(pb->deltatext) < 1)) {
548: checkin_revert(pb);
1.126 niallo 549: goto fail;
1.63 niallo 550: }
551:
1.73 xsa 552: /* If no log message specified, get it interactively. */
1.63 niallo 553: if (pb->flags & INTERACTIVE)
554: pb->rcs_msg = checkin_getlogmsg(pb->frev, pb->newrev);
555:
1.82 joris 556: if (rcs_lock_remove(pb->file, pb->username, pb->frev) < 0) {
1.63 niallo 557: if (rcs_errno != RCS_ERR_NOENT)
1.82 joris 558: cvs_log(LP_WARN, "failed to remove lock");
559: else if (!(pb->flags & CO_LOCK))
560: cvs_log(LP_WARN, "previous revision was not locked; "
561: "ignoring -l option");
1.63 niallo 562: }
563:
1.73 xsa 564: /* Current head revision gets the RCS patch as rd_text */
1.87 xsa 565: if (rcs_deltatext_set(pb->file, pb->frev, pb->deltatext) == -1)
566: fatal("failed to set new rd_text for head rev");
1.63 niallo 567:
1.73 xsa 568: /* Now add our new revision */
1.63 niallo 569: if (rcs_rev_add(pb->file,
570: (pb->newrev == NULL ? RCS_HEAD_REV : pb->newrev),
1.82 joris 571: pb->rcs_msg, pb->date, pb->author) != 0) {
1.63 niallo 572: cvs_log(LP_ERR, "failed to add new revision");
1.126 niallo 573: goto fail;
1.63 niallo 574: }
575:
576: /*
577: * If we are checking in to a non-default (ie user-specified)
578: * revision, set head to this revision.
579: */
1.131 xsa 580: if (pb->newrev != NULL) {
581: if (rcs_head_set(pb->file, pb->newrev) < 0)
582: fatal("rcs_head_set failed");
583: } else
1.63 niallo 584: pb->newrev = pb->file->rf_head;
585:
1.73 xsa 586: /* New head revision has to contain entire file; */
1.107 deraadt 587: if (rcs_deltatext_set(pb->file, pb->frev, filec) == -1)
1.87 xsa 588: fatal("failed to set new head revision");
1.63 niallo 589:
1.73 xsa 590: /* Attach a symbolic name to this revision if specified. */
1.63 niallo 591: if (pb->symbol != NULL
592: && (checkin_attach_symbol(pb) < 0))
1.126 niallo 593: goto fail;
1.63 niallo 594:
1.73 xsa 595: /* Set the state of this revision if specified. */
1.63 niallo 596: if (pb->state != NULL)
597: (void)rcs_state_set(pb->file, pb->newrev, pb->state);
598:
1.128 niallo 599: /* Maintain RCSFILE permissions */
600: if (stat(pb->filename, &st) == -1)
601: goto fail;
602:
603: /* Strip all the write bits */
604: pb->file->rf_mode = st.st_mode &
605: (S_IXUSR|S_IXGRP|S_IXOTH|S_IRUSR|S_IRGRP|S_IROTH);
606:
1.84 joris 607: xfree(pb->deltatext);
608: xfree(filec);
1.63 niallo 609: (void)unlink(pb->filename);
1.140 deraadt 610:
1.128 niallo 611: /* Write out RCSFILE before calling checkout_rev() */
612: rcs_write(pb->file);
1.63 niallo 613:
1.73 xsa 614: /* Do checkout if -u or -l are specified. */
1.63 niallo 615: if (((pb->flags & CO_LOCK) || (pb->flags & CO_UNLOCK))
616: && !(pb->flags & CI_DEFAULT))
617: checkout_rev(pb->file, pb->newrev, pb->filename, pb->flags,
1.89 joris 618: pb->username, pb->author, NULL, NULL);
1.63 niallo 619:
620: if (pb->flags & INTERACTIVE) {
1.84 joris 621: xfree(pb->rcs_msg);
1.63 niallo 622: pb->rcs_msg = NULL;
623: }
624: return (0);
1.126 niallo 625:
626: fail:
627: xfree(filec);
1.134 niallo 628: if (pb->deltatext != NULL)
629: xfree(pb->deltatext);
1.126 niallo 630: return (-1);
1.63 niallo 631: }
632:
633: /*
1.58 niallo 634: * checkin_init()
1.65 xsa 635: *
1.58 niallo 636: * Does an initial check in, just enough to create the new ,v file
1.63 niallo 637: * On success, return 0. On error return -1.
1.58 niallo 638: */
1.63 niallo 639: static int
1.58 niallo 640: checkin_init(struct checkin_params *pb)
641: {
1.80 niallo 642: BUF *bp, *dp;
1.93 xsa 643: char *filec, numb[64];
1.115 niallo 644: int fetchlog = 0;
1.80 niallo 645: const char *rcs_desc;
1.128 niallo 646: struct stat st;
1.58 niallo 647:
1.115 niallo 648: /* If this is a zero-ending RCSNUM eg 4.0, increment it (eg to 4.1) */
649: if ((pb->newrev != NULL) && (RCSNUM_ZERO_ENDING(pb->newrev))) {
650: pb->frev = rcsnum_alloc();
651: rcsnum_cpy(pb->newrev, pb->frev, 0);
652: pb->newrev = rcsnum_inc(pb->newrev);
653: fetchlog = 1;
654: }
655:
1.73 xsa 656: /* Load file contents */
1.58 niallo 657: if ((bp = cvs_buf_load(pb->filename, BUF_AUTOEXT)) == NULL) {
1.106 niallo 658: rcs_close(pb->file);
1.63 niallo 659: return (-1);
1.58 niallo 660: }
661:
1.124 xsa 662: cvs_buf_putc(bp, '\0');
1.58 niallo 663: filec = (char *)cvs_buf_release(bp);
664:
1.98 niallo 665: /* Get default values from working copy if -k specified */
666: if (pb->flags & CI_KEYWORDSCAN)
667: checkin_keywordscan(filec, &pb->newrev, &pb->date, &pb->state,
668: &pb->author);
669:
1.142 ! joris 670: if (pb->file->rf_ndelta == 0)
! 671: goto skipdesc;
! 672:
1.73 xsa 673: /* Get description from user */
1.80 niallo 674: if (pb->description == NULL)
675: rcs_desc = (const char *)checkin_getdesc();
676: else {
677: if (*pb->description == '-') {
678: pb->description++;
1.81 niallo 679: rcs_desc = (const char *)pb->description;
680: } else {
1.80 niallo 681: dp = cvs_buf_load(pb->description, BUF_AUTOEXT);
682: if (dp == NULL) {
683: cvs_log(LP_ERR,
684: "failed to load description file '%s'",
685: pb->description);
1.126 niallo 686: goto fail;
1.80 niallo 687: }
1.124 xsa 688: cvs_buf_putc(dp, '\0');
1.80 niallo 689: rcs_desc = (const char *)cvs_buf_release(dp);
690: }
691: }
1.58 niallo 692: rcs_desc_set(pb->file, rcs_desc);
1.142 ! joris 693:
! 694: skipdesc:
1.58 niallo 695:
1.115 niallo 696: /*
697: * If the user had specified a zero-ending revision number e.g. 4
698: * emulate odd GNU behaviour and fetch log message.
699: */
700: if (fetchlog == 1) {
701: pb->rcs_msg = checkin_getlogmsg(pb->frev, pb->newrev);
702: rcsnum_free(pb->frev);
703: }
1.133 joris 704:
1.97 niallo 705: /*
706: * Set the date of the revision to be the last modification
707: * time of the working file if -d has no argument.
708: */
709: if (pb->date == DATE_MTIME
710: && (checkin_mtimedate(pb) < 0))
1.126 niallo 711: goto fail;
1.97 niallo 712:
1.73 xsa 713: /* Now add our new revision */
1.96 niallo 714: if (rcs_rev_add(pb->file,
715: (pb->newrev == NULL ? RCS_HEAD_REV : pb->newrev),
1.107 deraadt 716: (pb->rcs_msg == NULL ? "Initial revision" : pb->rcs_msg),
1.103 niallo 717: pb->date, pb->author) != 0) {
1.58 niallo 718: cvs_log(LP_ERR, "failed to add new revision");
1.126 niallo 719: goto fail;
1.63 niallo 720: }
1.133 joris 721:
1.63 niallo 722: /*
723: * If we are checking in to a non-default (ie user-specified)
724: * revision, set head to this revision.
725: */
1.131 xsa 726: if (pb->newrev != NULL) {
727: if (rcs_head_set(pb->file, pb->newrev) < 0)
728: fatal("rcs_head_set failed");
729: } else
1.63 niallo 730: pb->newrev = pb->file->rf_head;
731:
1.73 xsa 732: /* New head revision has to contain entire file; */
1.66 niallo 733: if (rcs_deltatext_set(pb->file, pb->file->rf_head, filec) == -1) {
1.63 niallo 734: cvs_log(LP_ERR, "failed to set new head revision");
1.126 niallo 735: goto fail;
1.58 niallo 736: }
1.133 joris 737:
1.73 xsa 738: /* Attach a symbolic name to this revision if specified. */
1.66 niallo 739: if (pb->symbol != NULL
740: && (checkin_attach_symbol(pb) < 0))
1.126 niallo 741: goto fail;
1.66 niallo 742:
1.73 xsa 743: /* Set the state of this revision if specified. */
1.66 niallo 744: if (pb->state != NULL)
745: (void)rcs_state_set(pb->file, pb->newrev, pb->state);
746:
1.128 niallo 747: /* Inherit RCSFILE permissions from file being checked in */
748: if (stat(pb->filename, &st) == -1)
749: goto fail;
750: /* Strip all the write bits */
751: pb->file->rf_mode = st.st_mode &
752: (S_IXUSR|S_IXGRP|S_IXOTH|S_IRUSR|S_IRGRP|S_IROTH);
753:
1.84 joris 754: xfree(filec);
1.66 niallo 755: (void)unlink(pb->filename);
756:
1.128 niallo 757: /* Write out RCSFILE before calling checkout_rev() */
758: rcs_write(pb->file);
759:
1.73 xsa 760: /* Do checkout if -u or -l are specified. */
1.66 niallo 761: if (((pb->flags & CO_LOCK) || (pb->flags & CO_UNLOCK))
1.128 niallo 762: && !(pb->flags & CI_DEFAULT)) {
1.66 niallo 763: checkout_rev(pb->file, pb->newrev, pb->filename, pb->flags,
1.89 joris 764: pb->username, pb->author, NULL, NULL);
1.128 niallo 765: }
766:
1.63 niallo 767:
1.119 xsa 768: if (verbose == 1) {
769: fprintf(stderr, "initial revision: %s\n",
770: rcsnum_tostr(pb->newrev, numb, sizeof(numb)));
771: }
1.93 xsa 772:
1.63 niallo 773: return (0);
1.126 niallo 774: fail:
775: xfree(filec);
776: return (-1);
1.58 niallo 777: }
778:
1.59 niallo 779: /*
780: * checkin_attach_symbol()
781: *
782: * Attempt to attach the specified symbol to the revision.
783: * On success, return 0. On error return -1.
784: */
1.58 niallo 785: static int
786: checkin_attach_symbol(struct checkin_params *pb)
787: {
788: char rbuf[16];
789: int ret;
790: if (verbose == 1)
791: printf("symbol: %s\n", pb->symbol);
1.130 xsa 792: if (pb->flags & CI_SYMFORCE) {
793: if (rcs_sym_remove(pb->file, pb->symbol) < 0) {
794: if (rcs_errno != RCS_ERR_NOENT) {
795: cvs_log(LP_ERR,
796: "problem removing symbol: %s", pb->symbol);
797: return (-1);
798: }
799: }
800: }
1.58 niallo 801: if ((ret = rcs_sym_add(pb->file, pb->symbol, pb->newrev) == -1)
802: && (rcs_errno == RCS_ERR_DUPENT)) {
803: rcsnum_tostr(rcs_sym_getrev(pb->file, pb->symbol),
804: rbuf, sizeof(rbuf));
805: cvs_log(LP_ERR,
806: "symbolic name %s already bound to %s",
807: pb->symbol, rbuf);
808: return (-1);
809: } else if (ret == -1) {
810: cvs_log(LP_ERR, "problem adding symbol: %s",
811: pb->symbol);
812: return (-1);
813: }
814: return (0);
815: }
816:
1.59 niallo 817: /*
818: * checkin_revert()
819: *
820: * If there are no differences between the working file and the latest revision
821: * and the -f flag is not specified, simply revert to the latest version and
822: * warn the user.
823: *
824: */
1.58 niallo 825: static void
826: checkin_revert(struct checkin_params *pb)
827: {
828: char rbuf[16];
829:
830: rcsnum_tostr(pb->frev, rbuf, sizeof(rbuf));
831: cvs_log(LP_WARN,
832: "file is unchanged; reverting to previous revision %s",
833: rbuf);
1.137 niallo 834: pb->flags |= CO_REVERT;
1.58 niallo 835: (void)unlink(pb->filename);
836: if ((pb->flags & CO_LOCK) || (pb->flags & CO_UNLOCK))
837: checkout_rev(pb->file, pb->frev, pb->filename,
1.89 joris 838: pb->flags, pb->username, pb->author, NULL, NULL);
1.127 xsa 839: if (rcs_lock_remove(pb->file, pb->username, pb->frev) < 0)
840: if (rcs_errno != RCS_ERR_NOENT)
841: cvs_log(LP_WARN, "failed to remove lock");
1.58 niallo 842: }
843:
1.59 niallo 844: /*
845: * checkin_checklock()
846: *
847: * Check for the existence of a lock on the file. If there are no locks, or it
848: * is not locked by the correct user, return -1. Otherwise, return 0.
849: */
1.58 niallo 850: static int
851: checkin_checklock(struct checkin_params *pb)
852: {
853: struct rcs_lock *lkp;
854:
1.82 joris 855: TAILQ_FOREACH(lkp, &(pb->file->rf_locks), rl_list) {
856: if ((!strcmp(lkp->rl_name, pb->username)) &&
857: (!rcsnum_cmp(lkp->rl_num, pb->frev, 0)))
858: return (0);
1.58 niallo 859: }
1.32 joris 860:
1.82 joris 861: cvs_log(LP_ERR,
862: "%s: no lock set by %s", pb->file->rf_path, pb->username);
863: return (-1);
1.61 niallo 864: }
865:
866: /*
1.62 niallo 867: * checkin_mtimedate()
1.61 niallo 868: *
869: * Set the date of the revision to be the last modification
1.62 niallo 870: * time of the working file.
1.61 niallo 871: *
872: * On success, return 0. On error return -1.
873: */
874: static int
1.62 niallo 875: checkin_mtimedate(struct checkin_params *pb)
1.61 niallo 876: {
877: struct stat sb;
878: if (stat(pb->filename, &sb) != 0) {
1.72 xsa 879: cvs_log(LP_ERRNO, "failed to stat `%s'", pb->filename);
1.61 niallo 880: return (-1);
881: }
882: pb->date = (time_t)sb.st_mtimespec.tv_sec;
1.58 niallo 883: return (0);
1.98 niallo 884: }
885:
886: /*
887: * checkin_keywordscan()
888: *
889: * Searches working file for keyword values to determine its revision
890: * number, creation date and author, and uses these values instead of
891: * calculating them locally.
892: *
893: * Params: The data buffer to scan and pointers to pointers of variables in
894: * which to store the outputs.
895: *
896: * On success, return 0. On error return -1.
897: */
898: static int
899: checkin_keywordscan(char *data, RCSNUM **rev, time_t *date, char **author,
900: char **state)
901: {
1.122 ray 902: size_t end;
903: u_int j, found;
1.98 niallo 904: char *c, *kwstr, *start, buf[128];
905:
906: c = start = kwstr = NULL;
907:
1.122 ray 908: found = 0;
1.98 niallo 909:
1.122 ray 910: for (c = data; *c != '\0'; c++) {
1.98 niallo 911: if (*c == '$') {
912: start = c;
1.118 deraadt 913: c++;
1.98 niallo 914: if (!isalpha(*c)) {
915: c = start;
916: continue;
917: }
918: /* look for any matching keywords */
1.107 deraadt 919: found = 0;
920: for (j = 0; j < 10; j++) {
921: if (!strncmp(c, rcs_expkw[j].kw_str,
922: strlen(rcs_expkw[j].kw_str))) {
923: found = 1;
924: kwstr = rcs_expkw[j].kw_str;
925: break;
926: }
927: }
928:
929: /* unknown keyword, continue looking */
930: if (found == 0) {
931: c = start;
932: continue;
933: }
1.98 niallo 934:
935: c += strlen(kwstr);
936: if (*c != ':' && *c != '$') {
937: c = start;
938: continue;
939: }
940:
941: if (*c == ':') {
942: while (*c++) {
943: if (*c == '$') {
944: end = c - start + 2;
945: if (end >= sizeof(buf))
946: fatal("keyword buffer"
947: " too small!");
948: strlcpy(buf, start, end);
949: checkin_parsekeyword(buf, rev,
1.107 deraadt 950: date, author, state);
1.98 niallo 951: break;
952: }
953: }
954:
955: if (*c != '$') {
956: c = start;
957: continue;
958: }
959: }
960: }
961: }
962: if (found == 0)
963: return (-1);
964: else
965: return (0);
966: }
967:
968: /*
969: * checkin_keywordtype()
970: *
971: * Given an RCS keyword string, determine what type of string it is.
972: * This enables us to know what data should be in it.
973: *
974: * Returns type on success, or -1 on failure.
1.107 deraadt 975: */
1.98 niallo 976: static int
977: checkin_keywordtype(char *keystring)
978: {
979: char *p;
1.107 deraadt 980:
1.98 niallo 981: p = keystring;
1.118 deraadt 982: p++;
1.98 niallo 983: if (strncmp(p, KW_ID, strlen(KW_ID)) == 0)
984: return (KW_TYPE_ID);
985: else if (strncmp(p, KW_AUTHOR, strlen(KW_AUTHOR)) == 0)
986: return (KW_TYPE_AUTHOR);
987: else if (strncmp(p, KW_DATE, strlen(KW_DATE)) == 0)
988: return (KW_TYPE_DATE);
989: else if (strncmp(p, KW_STATE, strlen(KW_STATE)) == 0)
990: return (KW_TYPE_STATE);
991: else if (strncmp(p, KW_REVISION, strlen(KW_REVISION)) == 0)
992: return (KW_TYPE_REVISION);
993: else
994: return (-1);
995: }
996:
997: /*
998: * checkin_parsekeyword()
999: *
1000: * Do the actual parsing of an RCS keyword string, setting the values passed
1001: * to the function to whatever is found.
1002: *
1003: */
1004: static void
1005: checkin_parsekeyword(char *keystring, RCSNUM **rev, time_t *date,
1006: char **author, char **state)
1007: {
1008: char *tokens[10], *p, *datestring;
1009: size_t len = 0;
1.99 niallo 1010: int i = 0;
1.107 deraadt 1011:
1012: /* Parse data out of the expanded keyword */
1.98 niallo 1013: switch (checkin_keywordtype(keystring)) {
1014: case KW_TYPE_ID:
1.101 niallo 1015: for ((p = strtok(keystring, " ")); p;
1.107 deraadt 1016: (p = strtok(NULL, " "))) {
1.99 niallo 1017: if (i < KW_NUMTOKS_ID - 1)
1018: tokens[i++] = p;
1.107 deraadt 1019: }
1.99 niallo 1020: tokens[i] = NULL;
1.98 niallo 1021: if (*author != NULL)
1022: xfree(*author);
1023: if (*state != NULL)
1024: xfree(*state);
1025: /* only parse revision if one is not already set */
1026: if (*rev == NULL) {
1027: if ((*rev = rcsnum_parse(tokens[2])) == NULL)
1028: fatal("could not parse rcsnum");
1029: }
1.135 ray 1030: *author = xstrdup(tokens[5]);
1031: *author = xstrdup(tokens[6]);
1.98 niallo 1032: len = strlen(tokens[3]) + strlen(tokens[4]) + 2;
1033: datestring = xmalloc(len);
1034: strlcpy(datestring, tokens[3], len);
1035: strlcat(datestring, " ", len);
1036: strlcat(datestring, tokens[4], len);
1037: if ((*date = cvs_date_parse(datestring)) <= 0)
1038: fatal("could not parse date\n");
1039: xfree(datestring);
1040: break;
1041: case KW_TYPE_AUTHOR:
1.101 niallo 1042: for ((p = strtok(keystring, " ")); p;
1.107 deraadt 1043: (p = strtok(NULL, " "))) {
1.99 niallo 1044: if (i < KW_NUMTOKS_AUTHOR - 1)
1045: tokens[i++] = p;
1.107 deraadt 1046: }
1.98 niallo 1047: if (*author != NULL)
1048: xfree(*author);
1.135 ray 1049: *author = xstrdup(tokens[1]);
1.98 niallo 1050: break;
1051: case KW_TYPE_DATE:
1.101 niallo 1052: for ((p = strtok(keystring, " ")); p;
1.107 deraadt 1053: (p = strtok(NULL, " "))) {
1.99 niallo 1054: if (i < KW_NUMTOKS_DATE - 1)
1055: tokens[i++] = p;
1.98 niallo 1056: }
1057: len = strlen(tokens[1]) + strlen(tokens[2]) + 2;
1058: datestring = xmalloc(len);
1059: strlcpy(datestring, tokens[1], len);
1060: strlcat(datestring, " ", len);
1061: strlcat(datestring, tokens[2], len);
1062: if ((*date = cvs_date_parse(datestring)) <= 0)
1063: fatal("could not parse date\n");
1064: xfree(datestring);
1065: break;
1066: case KW_TYPE_STATE:
1.101 niallo 1067: for ((p = strtok(keystring, " ")); p;
1.107 deraadt 1068: (p = strtok(NULL, " "))) {
1.99 niallo 1069: if (i < KW_NUMTOKS_STATE - 1)
1070: tokens[i++] = p;
1.107 deraadt 1071: }
1.98 niallo 1072: if (*state != NULL)
1073: xfree(*state);
1.135 ray 1074: *state = xstrdup(tokens[1]);
1.98 niallo 1075: break;
1076: case KW_TYPE_REVISION:
1077: /* only parse revision if one is not already set */
1078: if (*rev != NULL)
1079: break;
1.102 niallo 1080: for ((p = strtok(keystring, " ")); p;
1.107 deraadt 1081: (p = strtok(NULL, " "))) {
1.99 niallo 1082: if (i < KW_NUMTOKS_REVISION - 1)
1083: tokens[i++] = p;
1.107 deraadt 1084: }
1.98 niallo 1085: if ((*rev = rcsnum_parse(tokens[1])) == NULL)
1086: fatal("could not parse rcsnum");
1087: break;
1088: }
1.1 niallo 1089: }