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