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