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