Annotation of src/usr.bin/rcs/ci.c, Revision 1.50
1.50 ! xsa 1: /* $OpenBSD: ci.c,v 1.49 2005/10/25 17:48:56 xsa Exp $ */
1.1 niallo 2: /*
3: * Copyright (c) 2005 Niall O'Higgins <niallo@openbsd.org>
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:
27: #include <sys/param.h>
28: #include <sys/types.h>
29: #include <sys/stat.h>
30: #include <sys/wait.h>
31:
32: #include <err.h>
33: #include <pwd.h>
34: #include <errno.h>
35: #include <stdio.h>
36: #include <ctype.h>
37: #include <stdlib.h>
38: #include <unistd.h>
39: #include <string.h>
1.20 niallo 40: #include <time.h>
1.1 niallo 41:
42: #include "log.h"
43: #include "rcs.h"
1.4 niallo 44: #include "diff.h"
1.1 niallo 45: #include "rcsprog.h"
46:
1.9 niallo 47: #define LOCK_LOCK 1
48: #define LOCK_UNLOCK 2
49:
1.25 niallo 50: #define DATE_NOW -1
51: #define DATE_MTIME -2
52:
1.4 niallo 53: static char * checkin_diff_file(RCSFILE *, RCSNUM *, const char *);
1.34 niallo 54: static char * checkin_getlogmsg(RCSNUM *, RCSNUM *);
1.4 niallo 55:
1.1 niallo 56: void
57: checkin_usage(void)
58: {
59: fprintf(stderr,
1.29 niallo 60: "usage: ci [-jMNqV] [-d[date]] [-f[rev]] [-kmode] [-l[rev]]\n"
1.43 niallo 61: " [-mmsg] [-Nsymbol] [-nsymbol] [-r[rev]] [-u[rev]]\n"
62: " [-wusername] file ...\n");
1.1 niallo 63: }
64:
65: /*
66: * checkin_main()
67: *
68: * Handler for the `ci' program.
69: * Returns 0 on success, or >0 on error.
70: */
71: int
72: checkin_main(int argc, char **argv)
73: {
1.46 joris 74: int found, notlocked, ret;
1.43 niallo 75: int i, ch, force, lkmode, interactive, rflag, status, symforce;
1.1 niallo 76: mode_t fmode;
1.27 niallo 77: time_t date;
1.1 niallo 78: RCSFILE *file;
1.12 niallo 79: RCSNUM *frev, *newrev;
1.1 niallo 80: char fpath[MAXPATHLEN];
1.46 joris 81: char *rcs_msg, *filec, *deltatext, *username, rbuf[16];
1.38 niallo 82: const char *symbol = NULL;
1.16 niallo 83: struct rcs_lock *lkp;
1.4 niallo 84: BUF *bp;
1.1 niallo 85:
1.27 niallo 86: date = DATE_NOW;
1.1 niallo 87: file = NULL;
1.31 niallo 88: rcs_msg = username = NULL;
1.12 niallo 89: newrev = NULL;
1.47 niallo 90: fmode = force = lkmode = rflag = status = symforce = 0;
1.6 niallo 91: interactive = 1;
1.1 niallo 92:
1.9 niallo 93:
1.38 niallo 94: while ((ch = rcs_getopt(argc, argv, "d::f::j:k:l::m:M:N:n:qr::u::Vw:")) != -1) {
1.1 niallo 95: switch (ch) {
1.20 niallo 96: case 'd':
1.25 niallo 97: if (rcs_optarg == NULL)
98: date = DATE_MTIME;
99: else if ((date = cvs_date_parse(rcs_optarg)) <= 0) {
1.20 niallo 100: cvs_log(LP_ERR, "invalide date");
101: exit(1);
102: }
103: break;
1.29 niallo 104: case 'f':
1.45 joris 105: rcs_set_rev(rcs_optarg, &newrev);
1.29 niallo 106: force = 1;
107: break;
1.1 niallo 108: case 'h':
109: (usage)();
110: exit(0);
1.30 niallo 111: case 'l':
1.45 joris 112: rcs_set_rev(rcs_optarg, &newrev);
1.30 niallo 113: lkmode = LOCK_LOCK;
114: break;
1.1 niallo 115: case 'm':
1.24 joris 116: rcs_msg = rcs_optarg;
1.6 niallo 117: interactive = 0;
1.3 joris 118: break;
1.42 niallo 119: case 'N':
120: if ((symbol = strdup(rcs_optarg)) == NULL) {
121: cvs_log(LP_ERRNO, "out of memory");
122: exit(1);
123: }
124: if (rcs_sym_check(symbol) != 1) {
125: cvs_log(LP_ERR, "invalid symbol `%s'", symbol);
126: exit(1);
127: }
1.43 niallo 128: symforce = 1;
1.42 niallo 129: break;
1.38 niallo 130: case 'n':
131: if ((symbol = strdup(rcs_optarg)) == NULL) {
132: cvs_log(LP_ERRNO, "out of memory");
133: exit(1);
134: }
135: if (rcs_sym_check(symbol) != 1) {
136: cvs_log(LP_ERR, "invalid symbol `%s'", symbol);
137: exit(1);
138: }
139: break;
1.3 joris 140: case 'q':
141: verbose = 0;
1.1 niallo 142: break;
1.30 niallo 143: case 'r':
1.45 joris 144: rcs_set_rev(rcs_optarg, &newrev);
1.30 niallo 145: rflag = 1;
1.9 niallo 146: break;
147: case 'u':
1.45 joris 148: rcs_set_rev(rcs_optarg, &newrev);
1.9 niallo 149: lkmode = LOCK_UNLOCK;
150: break;
1.30 niallo 151: case 'V':
152: printf("%s\n", rcs_version);
153: exit(0);
1.31 niallo 154: case 'w':
155: username = rcs_optarg;
156: break;
1.1 niallo 157: default:
158: (usage)();
159: exit(1);
160: }
161: }
162:
1.24 joris 163: argc -= rcs_optind;
164: argv += rcs_optind;
165:
1.1 niallo 166: if (argc == 0) {
167: cvs_log(LP_ERR, "no input file");
168: (usage)();
169: exit(1);
170: }
171:
1.31 niallo 172: if ((username == NULL) && (username = getlogin()) == NULL) {
173: cvs_log(LP_ERRNO, "failed to get username");
174: exit(1);
175: }
176:
177:
1.1 niallo 178: for (i = 0; i < argc; i++) {
179: if (rcs_statfile(argv[i], fpath, sizeof(fpath)) < 0)
180: continue;
181:
1.4 niallo 182: file = rcs_open(fpath, RCS_RDWR, fmode);
1.1 niallo 183: if (file == NULL) {
1.4 niallo 184: cvs_log(LP_ERR, "failed to open rcsfile '%s'", fpath);
1.1 niallo 185: exit(1);
186: }
1.29 niallo 187:
1.17 niallo 188: frev = file->rf_head;
1.36 niallo 189:
1.46 joris 190: if (verbose == 1)
191: printf("%s <-- %s\n", fpath, argv[i]);
1.29 niallo 192:
1.17 niallo 193: /*
194: * If revision passed on command line is less than HEAD, bail.
195: */
196: if ((newrev != NULL) && (rcsnum_cmp(newrev, frev, 0) > 0)) {
197: cvs_log(LP_ERR, "revision is too low!");
198: status = 1;
199: rcs_close(file);
200: continue;
201: }
1.4 niallo 202:
203: /*
204: * Load file contents
205: */
206: if ((bp = cvs_buf_load(argv[i], BUF_AUTOEXT)) == NULL) {
207: cvs_log(LP_ERR, "failed to load '%s'", argv[i]);
208: exit(1);
209: }
210:
1.12 niallo 211: if (cvs_buf_putc(bp, '\0') < 0)
212: exit(1);
213:
1.23 niallo 214: filec = (char *)cvs_buf_release(bp);
1.13 joris 215:
1.6 niallo 216: /*
1.29 niallo 217: * Get RCS patch
218: */
219: if ((deltatext = checkin_diff_file(file, frev, argv[i])) == NULL) {
220: cvs_log(LP_ERR, "failed to get diff");
221: exit(1);
222: }
223:
224: /*
225: * If -f is not specified and there are no differences, tell the
226: * user and revert to latest version.
227: */
1.49 xsa 228: if ((force == 0) && (strlen(deltatext) < 1)) {
1.46 joris 229: rcsnum_tostr(frev, rbuf, sizeof(rbuf));
1.48 xsa 230: cvs_log(LP_WARN,
1.29 niallo 231: "file is unchanged; reverting to previous revision %s",
1.46 joris 232: rbuf);
1.29 niallo 233: (void)unlink(argv[i]);
234: if (lkmode != 0)
1.33 niallo 235: checkout_rev(file, frev, argv[i], lkmode,
1.41 joris 236: username, 0);
1.40 niallo 237: rcs_lock_remove(file, frev);
1.29 niallo 238: rcs_close(file);
1.46 joris 239: if (verbose == 1)
240: printf("done\n");
1.29 niallo 241: continue;
242: }
243:
244: /*
1.16 niallo 245: * Check for a lock belonging to this user. If none,
246: * abort check-in.
247: */
1.44 joris 248: found = 0;
249: notlocked = 1;
250: if (!TAILQ_EMPTY(&(file->rf_locks))) {
251: TAILQ_FOREACH(lkp, &(file->rf_locks), rl_list) {
252: if (!strcmp(lkp->rl_name, username))
253: notlocked = 0;
254:
255: if (!strcmp(lkp->rl_name, username) &&
256: !rcsnum_cmp(lkp->rl_num, frev, 0)) {
257: found = 1;
258: break;
259: }
260: }
261: }
262:
1.49 xsa 263: if ((found == 0) && (notlocked == 0)) {
1.44 joris 264: cvs_log(LP_ERR, "no locks set for '%s'", username);
1.16 niallo 265: status = 1;
1.29 niallo 266: rcs_close(file);
1.16 niallo 267: continue;
268: }
269:
270: /*
1.6 niallo 271: * If no log message specified, get it interactively.
272: */
1.14 joris 273: if (rcs_msg == NULL)
1.34 niallo 274: rcs_msg = checkin_getlogmsg(frev, newrev);
1.4 niallo 275:
276: /*
277: * Remove the lock
278: */
279: if (rcs_lock_remove(file, frev) < 0) {
280: if (rcs_errno != RCS_ERR_NOENT)
281: cvs_log(LP_WARN, "failed to remove lock");
282: }
283:
284: /*
285: * Current head revision gets the RCS patch as rd_text
286: */
1.12 niallo 287: if (rcs_deltatext_set(file, frev, deltatext) == -1) {
1.7 niallo 288: cvs_log(LP_ERR,
289: "failed to set new rd_text for head rev");
1.4 niallo 290: exit (1);
1.25 niallo 291: }
1.32 joris 292:
1.25 niallo 293: /*
294: * Set the date of the revision to be the last modification time
295: * of the working file if -d is specified without an argument.
296: */
297: if (date == DATE_MTIME) {
298: struct stat sb;
299: if (stat(argv[i], &sb) != 0) {
1.33 niallo 300: cvs_log(LP_ERRNO, "failed to stat: `%s'",
301: argv[i]);
1.25 niallo 302: rcs_close(file);
303: continue;
304: }
305: date = (time_t)sb.st_mtimespec.tv_sec;
1.4 niallo 306: }
1.32 joris 307:
1.4 niallo 308: /*
309: * Now add our new revision
310: */
1.12 niallo 311: if (rcs_rev_add(file, (newrev == NULL ? RCS_HEAD_REV : newrev),
1.31 niallo 312: rcs_msg, date, username) != 0) {
1.4 niallo 313: cvs_log(LP_ERR, "failed to add new revision");
314: exit(1);
315: }
316:
317: /*
1.12 niallo 318: * If we are checking in to a non-default (ie user-specified)
319: * revision, set head to this revision.
320: */
321: if (newrev != NULL)
322: rcs_head_set(file, newrev);
1.15 niallo 323: else
324: newrev = file->rf_head;
1.32 joris 325:
1.12 niallo 326: /*
1.4 niallo 327: * New head revision has to contain entire file;
328: */
329: if (rcs_deltatext_set(file, frev, filec) == -1) {
330: cvs_log(LP_ERR, "failed to set new head revision");
331: exit(1);
1.38 niallo 332: }
333:
334: /*
335: * Attach a symbolic name to this revision if specified.
336: */
337: if (symbol != NULL) {
1.46 joris 338: if (verbose == 1)
339: printf("symbol: %s\n", symbol);
1.49 xsa 340: if (symforce == 1)
1.43 niallo 341: rcs_sym_remove(file, symbol);
1.38 niallo 342: if ((ret = rcs_sym_add(file, symbol, newrev) == -1)
343: && (rcs_errno == RCS_ERR_DUPENT)) {
1.42 niallo 344: rcsnum_tostr(rcs_sym_getrev(file, symbol),
1.46 joris 345: rbuf, sizeof(rbuf));
1.42 niallo 346: cvs_log(LP_ERR,
347: "symbolic name %s already bound to %s",
1.46 joris 348: symbol, rbuf);
1.38 niallo 349: status = 1;
350: rcs_close(file);
351: continue;
352: } else if (ret == -1) {
1.46 joris 353: cvs_log(LP_ERR, "problem adding symbol: %s",
1.42 niallo 354: symbol);
1.38 niallo 355: status = 1;
356: rcs_close(file);
357: continue;
358: }
1.4 niallo 359: }
360:
361: free(deltatext);
362: free(filec);
1.9 niallo 363: (void)unlink(argv[i]);
1.4 niallo 364:
1.9 niallo 365: /*
366: * Do checkout if -u or -l are specified.
367: */
1.49 xsa 368: if ((lkmode != 0) && (rflag == 0))
1.41 joris 369: checkout_rev(file, newrev, argv[i], lkmode, username, 0);
1.28 niallo 370:
1.4 niallo 371: /* File will NOW be synced */
1.1 niallo 372: rcs_close(file);
1.4 niallo 373:
1.49 xsa 374: if (interactive == 1) {
1.6 niallo 375: free(rcs_msg);
376: rcs_msg = NULL;
377: }
1.4 niallo 378: }
379:
1.16 niallo 380: return (status);
1.4 niallo 381: }
382:
383: static char *
384: checkin_diff_file(RCSFILE *rfp, RCSNUM *rev, const char *filename)
385: {
386: char path1[MAXPATHLEN], path2[MAXPATHLEN];
387: BUF *b1, *b2, *b3;
388: char rbuf[64], *deltatext;
389:
390: rcsnum_tostr(rev, rbuf, sizeof(rbuf));
391:
392: if ((b1 = cvs_buf_load(filename, BUF_AUTOEXT)) == NULL) {
393: cvs_log(LP_ERR, "failed to load file: '%s'", filename);
394: return (NULL);
1.1 niallo 395: }
396:
1.4 niallo 397: if ((b2 = rcs_getrev(rfp, rev)) == NULL) {
398: cvs_log(LP_ERR, "failed to load revision");
399: cvs_buf_free(b1);
400: return (NULL);
401: }
402:
403: if ((b3 = cvs_buf_alloc(128, BUF_AUTOEXT)) == NULL) {
404: cvs_log(LP_ERR, "failed to allocated buffer for diff");
405: cvs_buf_free(b1);
406: cvs_buf_free(b2);
407: return (NULL);
408: }
409:
1.50 ! xsa 410: strlcpy(path1, rcs_tmpdir, sizeof(path1));
! 411: strlcat(path1, "/diff1.XXXXXXXXXX", sizeof(path1));
1.4 niallo 412: if (cvs_buf_write_stmp(b1, path1, 0600) == -1) {
413: cvs_log(LP_ERRNO, "could not write temporary file");
414: cvs_buf_free(b1);
415: cvs_buf_free(b2);
416: return (NULL);
417: }
418: cvs_buf_free(b1);
419:
1.50 ! xsa 420: strlcpy(path2, rcs_tmpdir, sizeof(path2));
! 421: strlcat(path2, "/diff2.XXXXXXXXXX", sizeof(path2));
1.4 niallo 422: if (cvs_buf_write_stmp(b2, path2, 0600) == -1) {
423: cvs_buf_free(b2);
424: (void)unlink(path1);
425: return (NULL);
426: }
427: cvs_buf_free(b2);
428:
1.5 niallo 429: diff_format = D_RCSDIFF;
1.4 niallo 430: cvs_diffreg(path1, path2, b3);
431: (void)unlink(path1);
432: (void)unlink(path2);
433:
434: cvs_buf_putc(b3, '\0');
435: deltatext = (char *)cvs_buf_release(b3);
436:
437: return (deltatext);
1.6 niallo 438: }
439:
440: /*
441: * Get log message from user interactively.
442: */
443: static char *
1.34 niallo 444: checkin_getlogmsg(RCSNUM *rev, RCSNUM *rev2)
1.6 niallo 445: {
446: char *rcs_msg, buf[128], nrev[16], prev[16];
447: BUF *logbuf;
448: RCSNUM *tmprev;
449:
1.7 niallo 450: rcs_msg = NULL;
1.6 niallo 451: tmprev = rcsnum_alloc();
452: rcsnum_cpy(rev, tmprev, 16);
1.9 niallo 453: rcsnum_tostr(tmprev, prev, sizeof(prev));
1.12 niallo 454: if (rev2 == NULL)
455: rcsnum_tostr(rcsnum_inc(tmprev), nrev, sizeof(nrev));
456: else
457: rcsnum_tostr(rev2, nrev, sizeof(nrev));
1.6 niallo 458: rcsnum_free(tmprev);
459:
460: if ((logbuf = cvs_buf_alloc(64, BUF_AUTOEXT)) == NULL) {
1.7 niallo 461: cvs_log(LP_ERR, "failed to allocate log buffer");
1.6 niallo 462: return (NULL);
463: }
1.32 joris 464:
1.47 niallo 465: if (verbose == 1)
466: printf("new revision: %s; previous revision: %s\n", nrev,
467: prev);
1.46 joris 468: printf("enter log message, terminated with single "
1.6 niallo 469: "'.' or end of file:\n");
1.46 joris 470: printf(">> ");
1.32 joris 471:
1.6 niallo 472: for (;;) {
473: fgets(buf, (int)sizeof(buf), stdin);
1.9 niallo 474: if (feof(stdin) || ferror(stdin) || buf[0] == '.')
1.6 niallo 475: break;
476: cvs_buf_append(logbuf, buf, strlen(buf));
1.46 joris 477: printf(">> ");
1.6 niallo 478: }
1.32 joris 479:
1.8 niallo 480: cvs_buf_putc(logbuf, '\0');
1.6 niallo 481: rcs_msg = (char *)cvs_buf_release(logbuf);
1.32 joris 482:
1.6 niallo 483: return (rcs_msg);
1.1 niallo 484: }