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