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