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