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