Annotation of src/usr.bin/rcs/ci.c, Revision 1.45
1.45 ! joris 1: /* $OpenBSD: ci.c,v 1.44 2005/10/18 01:10:28 joris 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.44 joris 74: int found, notlocked;
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.12 niallo 81: char *rcs_msg, *filec, *deltatext, *username;
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.43 niallo 90: fmode = force = lkmode = verbose = 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.29 niallo 190: cvs_printf("%s <-- %s\n", fpath, argv[i]);
191:
1.17 niallo 192: /*
193: * If revision passed on command line is less than HEAD, bail.
194: */
195: if ((newrev != NULL) && (rcsnum_cmp(newrev, frev, 0) > 0)) {
196: cvs_log(LP_ERR, "revision is too low!");
197: status = 1;
198: rcs_close(file);
199: continue;
200: }
1.4 niallo 201:
202: /*
203: * Load file contents
204: */
205: if ((bp = cvs_buf_load(argv[i], BUF_AUTOEXT)) == NULL) {
206: cvs_log(LP_ERR, "failed to load '%s'", argv[i]);
207: exit(1);
208: }
209:
1.12 niallo 210: if (cvs_buf_putc(bp, '\0') < 0)
211: exit(1);
212:
1.23 niallo 213: filec = (char *)cvs_buf_release(bp);
1.13 joris 214:
1.6 niallo 215: /*
1.29 niallo 216: * Get RCS patch
217: */
218: if ((deltatext = checkin_diff_file(file, frev, argv[i])) == NULL) {
219: cvs_log(LP_ERR, "failed to get diff");
220: exit(1);
221: }
222:
223: /*
224: * If -f is not specified and there are no differences, tell the
225: * user and revert to latest version.
226: */
227: if ((!force) && (strlen(deltatext) < 1)) {
228: char buf[16];
229: rcsnum_tostr(frev, buf, sizeof(buf));
230: cvs_log(LP_WARN,
231: "file is unchanged; reverting to previous revision %s",
232: buf);
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);
239: cvs_printf("done\n");
240: continue;
241: }
242:
243: /*
1.16 niallo 244: * Check for a lock belonging to this user. If none,
245: * abort check-in.
246: */
1.44 joris 247: found = 0;
248: notlocked = 1;
249: if (!TAILQ_EMPTY(&(file->rf_locks))) {
250: TAILQ_FOREACH(lkp, &(file->rf_locks), rl_list) {
251: if (!strcmp(lkp->rl_name, username))
252: notlocked = 0;
253:
254: if (!strcmp(lkp->rl_name, username) &&
255: !rcsnum_cmp(lkp->rl_num, frev, 0)) {
256: found = 1;
257: break;
258: }
259: }
260: }
261:
262: if (found == 0 && notlocked == 0) {
263: cvs_log(LP_ERR, "no locks set for '%s'", username);
1.16 niallo 264: status = 1;
1.29 niallo 265: rcs_close(file);
1.16 niallo 266: continue;
267: }
268:
269: /*
1.6 niallo 270: * If no log message specified, get it interactively.
271: */
1.14 joris 272: if (rcs_msg == NULL)
1.34 niallo 273: rcs_msg = checkin_getlogmsg(frev, newrev);
1.4 niallo 274:
275: /*
276: * Remove the lock
277: */
278: if (rcs_lock_remove(file, frev) < 0) {
279: if (rcs_errno != RCS_ERR_NOENT)
280: cvs_log(LP_WARN, "failed to remove lock");
281: }
282:
283: /*
284: * Current head revision gets the RCS patch as rd_text
285: */
1.12 niallo 286: if (rcs_deltatext_set(file, frev, deltatext) == -1) {
1.7 niallo 287: cvs_log(LP_ERR,
288: "failed to set new rd_text for head rev");
1.4 niallo 289: exit (1);
1.25 niallo 290: }
1.32 joris 291:
1.25 niallo 292: /*
293: * Set the date of the revision to be the last modification time
294: * of the working file if -d is specified without an argument.
295: */
296: if (date == DATE_MTIME) {
297: struct stat sb;
298: if (stat(argv[i], &sb) != 0) {
1.33 niallo 299: cvs_log(LP_ERRNO, "failed to stat: `%s'",
300: argv[i]);
1.25 niallo 301: rcs_close(file);
302: continue;
303: }
304: date = (time_t)sb.st_mtimespec.tv_sec;
1.4 niallo 305: }
1.32 joris 306:
1.4 niallo 307: /*
308: * Now add our new revision
309: */
1.12 niallo 310: if (rcs_rev_add(file, (newrev == NULL ? RCS_HEAD_REV : newrev),
1.31 niallo 311: rcs_msg, date, username) != 0) {
1.4 niallo 312: cvs_log(LP_ERR, "failed to add new revision");
313: exit(1);
314: }
315:
316: /*
1.12 niallo 317: * If we are checking in to a non-default (ie user-specified)
318: * revision, set head to this revision.
319: */
320: if (newrev != NULL)
321: rcs_head_set(file, newrev);
1.15 niallo 322: else
323: newrev = file->rf_head;
1.32 joris 324:
1.12 niallo 325: /*
1.4 niallo 326: * New head revision has to contain entire file;
327: */
328: if (rcs_deltatext_set(file, frev, filec) == -1) {
329: cvs_log(LP_ERR, "failed to set new head revision");
330: exit(1);
1.38 niallo 331: }
332:
333: /*
334: * Attach a symbolic name to this revision if specified.
335: */
336: if (symbol != NULL) {
337: cvs_printf("symbol: %s\n", symbol);
338: int ret = 0;
1.43 niallo 339: if (symforce)
340: rcs_sym_remove(file, symbol);
1.38 niallo 341: if ((ret = rcs_sym_add(file, symbol, newrev) == -1)
342: && (rcs_errno == RCS_ERR_DUPENT)) {
343: char tmp[16];
1.42 niallo 344: rcsnum_tostr(rcs_sym_getrev(file, symbol),
345: tmp, sizeof(tmp));
346: cvs_log(LP_ERR,
347: "symbolic name %s already bound to %s",
1.38 niallo 348: symbol, tmp);
349: status = 1;
350: rcs_close(file);
351: continue;
352: } else if (ret == -1) {
1.42 niallo 353: cvs_printf("problem adding symbol: %s\n",
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.28 niallo 368: if (lkmode != 0 && !rflag)
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.6 niallo 374: if (interactive) {
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:
410: strlcpy(path1, "/tmp/diff1.XXXXXXXXXX", sizeof(path1));
411: if (cvs_buf_write_stmp(b1, path1, 0600) == -1) {
412: cvs_log(LP_ERRNO, "could not write temporary file");
413: cvs_buf_free(b1);
414: cvs_buf_free(b2);
415: return (NULL);
416: }
417: cvs_buf_free(b1);
418:
419: strlcpy(path2, "/tmp/diff2.XXXXXXXXXX", sizeof(path2));
420: if (cvs_buf_write_stmp(b2, path2, 0600) == -1) {
421: cvs_buf_free(b2);
422: (void)unlink(path1);
423: return (NULL);
424: }
425: cvs_buf_free(b2);
426:
1.5 niallo 427: diff_format = D_RCSDIFF;
1.4 niallo 428: cvs_diffreg(path1, path2, b3);
429: (void)unlink(path1);
430: (void)unlink(path2);
431:
432: cvs_buf_putc(b3, '\0');
433: deltatext = (char *)cvs_buf_release(b3);
434:
435: return (deltatext);
1.6 niallo 436: }
437:
438: /*
439: * Get log message from user interactively.
440: */
441: static char *
1.34 niallo 442: checkin_getlogmsg(RCSNUM *rev, RCSNUM *rev2)
1.6 niallo 443: {
444: char *rcs_msg, buf[128], nrev[16], prev[16];
445: BUF *logbuf;
446: RCSNUM *tmprev;
447:
1.7 niallo 448: rcs_msg = NULL;
1.6 niallo 449: tmprev = rcsnum_alloc();
450: rcsnum_cpy(rev, tmprev, 16);
1.9 niallo 451: rcsnum_tostr(tmprev, prev, sizeof(prev));
1.12 niallo 452: if (rev2 == NULL)
453: rcsnum_tostr(rcsnum_inc(tmprev), nrev, sizeof(nrev));
454: else
455: rcsnum_tostr(rev2, nrev, sizeof(nrev));
1.6 niallo 456: rcsnum_free(tmprev);
457:
458: if ((logbuf = cvs_buf_alloc(64, BUF_AUTOEXT)) == NULL) {
1.7 niallo 459: cvs_log(LP_ERR, "failed to allocate log buffer");
1.6 niallo 460: return (NULL);
461: }
1.32 joris 462:
1.7 niallo 463: cvs_printf("new revision: %s; previous revision: %s\n", nrev, prev);
1.6 niallo 464: cvs_printf("enter log message, terminated with single "
465: "'.' or end of file:\n");
466: cvs_printf(">> ");
1.32 joris 467:
1.6 niallo 468: for (;;) {
469: fgets(buf, (int)sizeof(buf), stdin);
1.9 niallo 470: if (feof(stdin) || ferror(stdin) || buf[0] == '.')
1.6 niallo 471: break;
472: cvs_buf_append(logbuf, buf, strlen(buf));
473: cvs_printf(">> ");
474: }
1.32 joris 475:
1.8 niallo 476: cvs_buf_putc(logbuf, '\0');
1.6 niallo 477: rcs_msg = (char *)cvs_buf_release(logbuf);
1.32 joris 478:
1.6 niallo 479: return (rcs_msg);
1.1 niallo 480: }