Annotation of src/usr.bin/rcs/ci.c, Revision 1.82
1.82 ! joris 1: /* $OpenBSD: ci.c,v 1.81 2005/12/03 17:11:58 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:
1.68 xsa 32: #include <ctype.h>
1.1 niallo 33: #include <pwd.h>
34: #include <stdio.h>
35: #include <stdlib.h>
1.68 xsa 36: #include <string.h>
1.1 niallo 37: #include <unistd.h>
38:
39: #include "log.h"
40: #include "rcs.h"
1.4 niallo 41: #include "diff.h"
1.1 niallo 42: #include "rcsprog.h"
1.9 niallo 43:
1.80 niallo 44: #define CI_OPTSTRING "d::f::i::j::k:l::m:M::N:n:qr::s:Tt:u::Vw:x:"
1.25 niallo 45: #define DATE_NOW -1
46: #define DATE_MTIME -2
47:
1.58 niallo 48: #define LOG_INIT "Initial revision"
1.63 niallo 49: #define LOG_PROMPT "enter log message, terminated with a single '.' " \
1.58 niallo 50: "or end of file:\n>> "
1.63 niallo 51: #define DESC_PROMPT "enter description, terminated with single '.' " \
52: "or end of file:\nNOTE: This is NOT the log message!" \
53: "\n>> "
1.58 niallo 54:
55: struct checkin_params {
56: int flags, openflags;
57: mode_t fmode;
58: time_t date;
59: RCSFILE *file;
60: RCSNUM *frev, *newrev;
61: char fpath[MAXPATHLEN], *rcs_msg, *username, *deltatext, *filename;
1.82 ! joris 62: char *author;
1.80 niallo 63: const char *symbol, *state, *description;
1.58 niallo 64: };
65:
1.68 xsa 66: static int checkin_attach_symbol(struct checkin_params *pb);
67: static int checkin_checklock(struct checkin_params *pb);
68: static char *checkin_choose_rcsfile(const char *);
69: static char *checkin_diff_file(struct checkin_params *);
70: static char *checkin_getdesc(void);
71: static char *checkin_getinput(const char *);
72: static char *checkin_getlogmsg(RCSNUM *, RCSNUM *);
73: static int checkin_init(struct checkin_params *);
74: static int checkin_mtimedate(struct checkin_params *pb);
75: static int checkin_update(struct checkin_params *pb);
76: static void checkin_revert(struct checkin_params *pb);
1.4 niallo 77:
1.1 niallo 78: void
79: checkin_usage(void)
80: {
81: fprintf(stderr,
1.67 xsa 82: "usage: ci [-MNqTV] [-d[date]] [-f[rev]] [-i[rev]] [-j[rev]]\n"
1.74 xsa 83: " [-kmode] [-l[rev]] [-M[rev]] [-mmsg] [-Nsymbol]\n"
1.81 niallo 84: " [-nsymbol] [-r[rev]] [-sstate] [-tfile] [-u[rev]]\n"
85: " [-wusername] [-xsuffixes] file ...\n");
1.58 niallo 86: }
87:
1.55 niallo 88:
1.1 niallo 89:
90: /*
91: * checkin_main()
92: *
93: * Handler for the `ci' program.
94: * Returns 0 on success, or >0 on error.
95: */
96: int
97: checkin_main(int argc, char **argv)
98: {
1.58 niallo 99: int i, ch, status;
100: struct checkin_params pb;
1.1 niallo 101:
1.58 niallo 102: pb.date = DATE_NOW;
103: pb.file = NULL;
1.82 ! joris 104: pb.rcs_msg = pb.username = pb.author = NULL;
1.80 niallo 105: pb.state = pb.symbol = pb.description = NULL;
1.58 niallo 106: pb.newrev = NULL;
107: pb.fmode = pb.flags = status = 0;
1.1 niallo 108:
1.58 niallo 109: pb.flags = INTERACTIVE;
110: pb.openflags = RCS_RDWR|RCS_CREATE;
1.9 niallo 111:
1.58 niallo 112: while ((ch = rcs_getopt(argc, argv, CI_OPTSTRING)) != -1) {
1.1 niallo 113: switch (ch) {
1.20 niallo 114: case 'd':
1.25 niallo 115: if (rcs_optarg == NULL)
1.58 niallo 116: pb.date = DATE_MTIME;
117: else if ((pb.date = cvs_date_parse(rcs_optarg)) <= 0) {
1.20 niallo 118: cvs_log(LP_ERR, "invalide date");
119: exit(1);
120: }
121: break;
1.29 niallo 122: case 'f':
1.58 niallo 123: rcs_set_rev(rcs_optarg, &pb.newrev);
124: pb.flags |= FORCE;
1.29 niallo 125: break;
1.1 niallo 126: case 'h':
127: (usage)();
128: exit(0);
1.58 niallo 129: case 'i':
130: rcs_set_rev(rcs_optarg, &pb.newrev);
131: pb.openflags |= RCS_CREATE;
1.66 niallo 132: pb.flags |= CI_INIT;
1.58 niallo 133: break;
134: case 'j':
135: rcs_set_rev(rcs_optarg, &pb.newrev);
136: pb.openflags &= ~RCS_CREATE;
1.66 niallo 137: pb.flags &= ~CI_INIT;
1.58 niallo 138: break;
1.30 niallo 139: case 'l':
1.58 niallo 140: rcs_set_rev(rcs_optarg, &pb.newrev);
141: pb.flags |= CO_LOCK;
1.54 niallo 142: break;
143: case 'M':
1.58 niallo 144: rcs_set_rev(rcs_optarg, &pb.newrev);
145: pb.flags |= CO_REVDATE;
1.30 niallo 146: break;
1.1 niallo 147: case 'm':
1.58 niallo 148: pb.rcs_msg = rcs_optarg;
149: if (pb.rcs_msg == NULL) {
150: cvs_log(LP_ERR,
151: "missing message for -m option");
152: exit(1);
153: }
154: pb.flags &= ~INTERACTIVE;
1.3 joris 155: break;
1.42 niallo 156: case 'N':
1.58 niallo 157: if ((pb.symbol = strdup(rcs_optarg)) == NULL) {
1.42 niallo 158: cvs_log(LP_ERRNO, "out of memory");
159: exit(1);
160: }
1.58 niallo 161: if (rcs_sym_check(pb.symbol) != 1) {
162: cvs_log(LP_ERR, "invalid symbol `%s'",
163: pb.symbol);
1.42 niallo 164: exit(1);
165: }
1.58 niallo 166: pb.flags |= CI_SYMFORCE;
1.42 niallo 167: break;
1.38 niallo 168: case 'n':
1.58 niallo 169: if ((pb.symbol = strdup(rcs_optarg)) == NULL) {
1.38 niallo 170: cvs_log(LP_ERRNO, "out of memory");
171: exit(1);
172: }
1.58 niallo 173: if (rcs_sym_check(pb.symbol) != 1) {
174: cvs_log(LP_ERR, "invalid symbol `%s'",
175: pb.symbol);
1.38 niallo 176: exit(1);
177: }
178: break;
1.3 joris 179: case 'q':
180: verbose = 0;
1.1 niallo 181: break;
1.30 niallo 182: case 'r':
1.58 niallo 183: rcs_set_rev(rcs_optarg, &pb.newrev);
184: pb.flags |= CI_DEFAULT;
1.9 niallo 185: break;
1.51 niallo 186: case 's':
1.58 niallo 187: pb.state = rcs_optarg;
188: if (rcs_state_check(pb.state) < 0) {
189: cvs_log(LP_ERR, "invalid state `%s'",
190: pb.state);
1.51 niallo 191: exit(1);
192: }
1.67 xsa 193: break;
194: case 'T':
195: pb.flags |= PRESERVETIME;
1.51 niallo 196: break;
1.80 niallo 197: case 't':
198: if ((pb.description = strdup(rcs_optarg)) == NULL) {
199: cvs_log(LP_ERRNO, "out of memory");
200: exit(1);
201: }
202: break;
1.9 niallo 203: case 'u':
1.58 niallo 204: rcs_set_rev(rcs_optarg, &pb.newrev);
205: pb.flags |= CO_UNLOCK;
1.9 niallo 206: break;
1.30 niallo 207: case 'V':
208: printf("%s\n", rcs_version);
209: exit(0);
1.31 niallo 210: case 'w':
1.82 ! joris 211: if ((pb.author = strdup(rcs_optarg)) == NULL) {
! 212: cvs_log(LP_ERRNO, "out of memory");
! 213: exit(1);
! 214: }
1.64 xsa 215: break;
216: case 'x':
217: rcs_suffixes = rcs_optarg;
1.31 niallo 218: break;
1.1 niallo 219: default:
220: (usage)();
221: exit(1);
222: }
223: }
224:
1.24 joris 225: argc -= rcs_optind;
226: argv += rcs_optind;
227:
1.1 niallo 228: if (argc == 0) {
229: cvs_log(LP_ERR, "no input file");
230: (usage)();
231: exit(1);
232: }
233:
1.82 ! joris 234: if ((pb.username = getlogin()) == NULL) {
1.31 niallo 235: cvs_log(LP_ERRNO, "failed to get username");
236: exit(1);
237: }
238:
239:
1.1 niallo 240: for (i = 0; i < argc; i++) {
1.58 niallo 241: pb.filename = argv[i];
1.1 niallo 242:
1.58 niallo 243: /*
244: * Test for existence of ,v file. If we are expected to
245: * create one, set NEWFILE flag.
246: */
1.71 niallo 247: if (rcs_statfile(pb.filename, pb.fpath, sizeof(pb.fpath)) < 0) {
248: if (pb.openflags & RCS_CREATE)
249: pb.flags |= NEWFILE;
250: else {
251: cvs_log(LP_ERR, "No existing RCS file");
252: status = 1;
253: continue;
254: }
1.66 niallo 255: } else {
256: if (pb.flags & CI_INIT) {
257: cvs_log(LP_ERR, "%s already exists", pb.fpath);
258: status = 1;
259: continue;
260: }
1.58 niallo 261: pb.openflags &= ~RCS_CREATE;
1.66 niallo 262: }
1.63 niallo 263: /*
264: * If we are to create a new ,v file, we must decide where it
265: * should go.
266: */
267: if (pb.flags & NEWFILE) {
268: char *fpath = checkin_choose_rcsfile(pb.filename);
269: if (fpath == NULL) {
270: status = 1;
271: continue;
272: }
273: strlcpy(pb.fpath, fpath, sizeof(pb.fpath));
274: free(fpath);
275: }
276:
1.58 niallo 277: pb.file = rcs_open(pb.fpath, pb.openflags, pb.fmode);
278:
279: if (pb.file == NULL) {
1.60 niallo 280: cvs_log(LP_ERR, "failed to open rcsfile '%s'",
281: pb.fpath);
1.1 niallo 282: exit(1);
283: }
1.29 niallo 284:
1.58 niallo 285: if (verbose == 1)
286: printf("%s <-- %s\n", pb.fpath, pb.filename);
1.61 niallo 287:
1.63 niallo 288: if (pb.flags & NEWFILE)
289: status = checkin_init(&pb);
1.15 niallo 290: else
1.63 niallo 291: status = checkin_update(&pb);
1.4 niallo 292: }
293:
1.16 niallo 294: return (status);
1.4 niallo 295: }
296:
1.59 niallo 297: /*
298: * checkin_diff_file()
299: *
1.65 xsa 300: * Generate the diff between the working file and a revision.
1.59 niallo 301: * Returns pointer to a char array on success, NULL on failure.
302: */
1.4 niallo 303: static char *
1.58 niallo 304: checkin_diff_file(struct checkin_params *pb)
1.4 niallo 305: {
306: char path1[MAXPATHLEN], path2[MAXPATHLEN];
307: BUF *b1, *b2, *b3;
308: char rbuf[64], *deltatext;
309:
1.58 niallo 310: rcsnum_tostr(pb->frev, rbuf, sizeof(rbuf));
1.4 niallo 311:
1.58 niallo 312: if ((b1 = cvs_buf_load(pb->filename, BUF_AUTOEXT)) == NULL) {
313: cvs_log(LP_ERR, "failed to load file: '%s'", pb->filename);
1.4 niallo 314: return (NULL);
1.1 niallo 315: }
316:
1.58 niallo 317: if ((b2 = rcs_getrev(pb->file, pb->frev)) == NULL) {
1.4 niallo 318: cvs_log(LP_ERR, "failed to load revision");
319: cvs_buf_free(b1);
320: return (NULL);
321: }
322:
1.57 xsa 323: if ((b3 = cvs_buf_alloc((size_t)128, BUF_AUTOEXT)) == NULL) {
1.4 niallo 324: cvs_log(LP_ERR, "failed to allocated buffer for diff");
325: cvs_buf_free(b1);
326: cvs_buf_free(b2);
327: return (NULL);
328: }
329:
1.50 xsa 330: strlcpy(path1, rcs_tmpdir, sizeof(path1));
331: strlcat(path1, "/diff1.XXXXXXXXXX", sizeof(path1));
1.4 niallo 332: if (cvs_buf_write_stmp(b1, path1, 0600) == -1) {
333: cvs_log(LP_ERRNO, "could not write temporary file");
334: cvs_buf_free(b1);
335: cvs_buf_free(b2);
336: return (NULL);
337: }
338: cvs_buf_free(b1);
339:
1.50 xsa 340: strlcpy(path2, rcs_tmpdir, sizeof(path2));
341: strlcat(path2, "/diff2.XXXXXXXXXX", sizeof(path2));
1.4 niallo 342: if (cvs_buf_write_stmp(b2, path2, 0600) == -1) {
343: cvs_buf_free(b2);
344: (void)unlink(path1);
345: return (NULL);
346: }
347: cvs_buf_free(b2);
348:
1.5 niallo 349: diff_format = D_RCSDIFF;
1.4 niallo 350: cvs_diffreg(path1, path2, b3);
351: (void)unlink(path1);
352: (void)unlink(path2);
353:
354: cvs_buf_putc(b3, '\0');
355: deltatext = (char *)cvs_buf_release(b3);
356:
357: return (deltatext);
1.6 niallo 358: }
359:
360: /*
1.65 xsa 361: * checkin_getlogmsg()
1.59 niallo 362: *
1.6 niallo 363: * Get log message from user interactively.
1.59 niallo 364: * Returns pointer to a char array on success, NULL on failure.
1.6 niallo 365: */
366: static char *
1.34 niallo 367: checkin_getlogmsg(RCSNUM *rev, RCSNUM *rev2)
1.6 niallo 368: {
1.58 niallo 369: char *rcs_msg, nrev[16], prev[16];
1.6 niallo 370: RCSNUM *tmprev;
371:
1.7 niallo 372: rcs_msg = NULL;
1.6 niallo 373: tmprev = rcsnum_alloc();
374: rcsnum_cpy(rev, tmprev, 16);
1.9 niallo 375: rcsnum_tostr(tmprev, prev, sizeof(prev));
1.12 niallo 376: if (rev2 == NULL)
377: rcsnum_tostr(rcsnum_inc(tmprev), nrev, sizeof(nrev));
378: else
379: rcsnum_tostr(rev2, nrev, sizeof(nrev));
1.6 niallo 380: rcsnum_free(tmprev);
381:
1.47 niallo 382: if (verbose == 1)
383: printf("new revision: %s; previous revision: %s\n", nrev,
384: prev);
1.32 joris 385:
1.58 niallo 386: rcs_msg = checkin_getinput(LOG_PROMPT);
387: return (rcs_msg);
388: }
389:
390:
391: /*
392: * checkin_getdesc()
393: *
394: * Get file description interactively.
1.59 niallo 395: * Returns pointer to a char array on success, NULL on failure.
1.58 niallo 396: */
397: static char *
398: checkin_getdesc()
399: {
400: char *description;
401:
402: description = checkin_getinput(DESC_PROMPT);
403: return (description);
404: }
405:
406: /*
407: * checkin_getinput()
408: *
1.59 niallo 409: * Get some input from the user, in RCS style, prompting with message <prompt>.
410: * Returns pointer to a char array on success, NULL on failure.
1.58 niallo 411: */
412: static char *
413: checkin_getinput(const char *prompt)
414: {
415: BUF *inputbuf;
416: char *input, buf[128];
417:
418: if ((inputbuf = cvs_buf_alloc((size_t)64, BUF_AUTOEXT)) == NULL) {
419: cvs_log(LP_ERR, "failed to allocate input buffer");
420: return (NULL);
421: }
422:
423: printf(prompt);
1.6 niallo 424: for (;;) {
425: fgets(buf, (int)sizeof(buf), stdin);
1.9 niallo 426: if (feof(stdin) || ferror(stdin) || buf[0] == '.')
1.6 niallo 427: break;
1.58 niallo 428: cvs_buf_append(inputbuf, buf, strlen(buf));
1.46 joris 429: printf(">> ");
1.6 niallo 430: }
1.32 joris 431:
1.58 niallo 432: cvs_buf_putc(inputbuf, '\0');
433: input = (char *)cvs_buf_release(inputbuf);
434:
435: return (input);
436: }
437:
438: /*
1.63 niallo 439: * checkin_update()
440: *
441: * Do a checkin to an existing RCS file.
442: *
443: * On success, return 0. On error return -1.
444: */
445: static int
446: checkin_update(struct checkin_params *pb)
447: {
1.77 xsa 448: char *filec, numb1[64], numb2[64];
1.63 niallo 449: BUF *bp;
450:
1.82 ! joris 451: /*
! 452: * XXX this is wrong, we need to get the revision the user
! 453: * has the lock for. So we can decide if we want to create a
! 454: * branch or not. (if it's not current HEAD we need to branch).
! 455: */
1.63 niallo 456: pb->frev = pb->file->rf_head;
457:
1.82 ! joris 458: if (checkin_checklock(pb) < 0)
! 459: return (-1);
! 460:
! 461: /* If revision passed on command line is less than HEAD, bail.
! 462: * XXX only applies to ci -r1.2 foo for example if HEAD is > 1.2 and
! 463: * there is no lock set for the user.
! 464: */
1.63 niallo 465: if ((pb->newrev != NULL)
466: && (rcsnum_cmp(pb->newrev, pb->frev, 0) > 0)) {
1.77 xsa 467: cvs_log(LP_ERR,
468: "%s: revision %s too low; must be higher than %s",
469: pb->file->rf_path,
470: rcsnum_tostr(pb->newrev, numb1, sizeof(numb1)),
471: rcsnum_tostr(pb->frev, numb2, sizeof(numb2)));
1.63 niallo 472: rcs_close(pb->file);
473: return (-1);
474: }
475:
1.73 xsa 476: /* Load file contents */
1.63 niallo 477: if ((bp = cvs_buf_load(pb->filename, BUF_AUTOEXT)) == NULL) {
478: cvs_log(LP_ERR, "failed to load '%s'", pb->filename);
479: return (-1);
480: }
481:
482: if (cvs_buf_putc(bp, '\0') < 0)
483: return (-1);
484:
485: filec = (char *)cvs_buf_release(bp);
486:
1.73 xsa 487: /* Get RCS patch */
1.63 niallo 488: if ((pb->deltatext = checkin_diff_file(pb)) == NULL) {
489: cvs_log(LP_ERR, "failed to get diff");
490: return (-1);
491: }
492:
493: /*
494: * If -f is not specified and there are no differences, tell
495: * the user and revert to latest version.
496: */
497: if (!(pb->flags & FORCE) && (strlen(pb->deltatext) < 1)) {
498: checkin_revert(pb);
499: return (0);
500: }
501:
1.73 xsa 502: /* If no log message specified, get it interactively. */
1.63 niallo 503: if (pb->flags & INTERACTIVE)
504: pb->rcs_msg = checkin_getlogmsg(pb->frev, pb->newrev);
505:
1.82 ! joris 506: if (rcs_lock_remove(pb->file, pb->username, pb->frev) < 0) {
1.63 niallo 507: if (rcs_errno != RCS_ERR_NOENT)
1.82 ! joris 508: cvs_log(LP_WARN, "failed to remove lock");
! 509: else if (!(pb->flags & CO_LOCK))
! 510: cvs_log(LP_WARN, "previous revision was not locked; "
! 511: "ignoring -l option");
1.63 niallo 512: }
513:
1.73 xsa 514: /* Current head revision gets the RCS patch as rd_text */
1.63 niallo 515: if (rcs_deltatext_set(pb->file, pb->frev, pb->deltatext) == -1) {
516: cvs_log(LP_ERR,
517: "failed to set new rd_text for head rev");
518: exit (1);
519: }
520:
521: /*
522: * Set the date of the revision to be the last modification
523: * time of the working file if -d has no argument.
524: */
525: if (pb->date == DATE_MTIME
526: && (checkin_mtimedate(pb) < 0))
527: return (-1);
528:
1.73 xsa 529: /* Now add our new revision */
1.63 niallo 530: if (rcs_rev_add(pb->file,
531: (pb->newrev == NULL ? RCS_HEAD_REV : pb->newrev),
1.82 ! joris 532: pb->rcs_msg, pb->date, pb->author) != 0) {
1.63 niallo 533: cvs_log(LP_ERR, "failed to add new revision");
534: return (-1);
535: }
536:
537: /*
538: * If we are checking in to a non-default (ie user-specified)
539: * revision, set head to this revision.
540: */
541: if (pb->newrev != NULL)
542: rcs_head_set(pb->file, pb->newrev);
543: else
544: pb->newrev = pb->file->rf_head;
545:
1.73 xsa 546: /* New head revision has to contain entire file; */
1.63 niallo 547: if (rcs_deltatext_set(pb->file, pb->frev, filec) == -1) {
548: cvs_log(LP_ERR, "failed to set new head revision");
549: exit(1);
550: }
551:
1.73 xsa 552: /* Attach a symbolic name to this revision if specified. */
1.63 niallo 553: if (pb->symbol != NULL
554: && (checkin_attach_symbol(pb) < 0))
555: return (-1);
556:
1.73 xsa 557: /* Set the state of this revision if specified. */
1.63 niallo 558: if (pb->state != NULL)
559: (void)rcs_state_set(pb->file, pb->newrev, pb->state);
560:
561: free(pb->deltatext);
562: free(filec);
563: (void)unlink(pb->filename);
564:
1.73 xsa 565: /* Do checkout if -u or -l are specified. */
1.63 niallo 566: if (((pb->flags & CO_LOCK) || (pb->flags & CO_UNLOCK))
567: && !(pb->flags & CI_DEFAULT))
568: checkout_rev(pb->file, pb->newrev, pb->filename, pb->flags,
1.82 ! joris 569: pb->username, pb->author, NULL);
1.63 niallo 570:
571: /* File will NOW be synced */
572: rcs_close(pb->file);
573:
574: if (pb->flags & INTERACTIVE) {
575: free(pb->rcs_msg);
576: pb->rcs_msg = NULL;
577: }
578: return (0);
579: }
580:
581: /*
1.58 niallo 582: * checkin_init()
1.65 xsa 583: *
1.58 niallo 584: * Does an initial check in, just enough to create the new ,v file
1.63 niallo 585: * On success, return 0. On error return -1.
1.58 niallo 586: */
1.63 niallo 587: static int
1.58 niallo 588: checkin_init(struct checkin_params *pb)
589: {
1.80 niallo 590: BUF *bp, *dp;
591: char *filec;
592: const char *rcs_desc;
1.58 niallo 593:
1.73 xsa 594: /* Load file contents */
1.58 niallo 595: if ((bp = cvs_buf_load(pb->filename, BUF_AUTOEXT)) == NULL) {
596: cvs_log(LP_ERR, "failed to load '%s'", pb->filename);
1.63 niallo 597: return (-1);
1.58 niallo 598: }
599:
600: if (cvs_buf_putc(bp, '\0') < 0)
1.63 niallo 601: return (-1);
1.58 niallo 602:
603: filec = (char *)cvs_buf_release(bp);
604:
1.73 xsa 605: /* Get description from user */
1.80 niallo 606: if (pb->description == NULL)
607: rcs_desc = (const char *)checkin_getdesc();
608: else {
609: if (*pb->description == '-') {
610: pb->description++;
1.81 niallo 611: rcs_desc = (const char *)pb->description;
612: } else {
1.80 niallo 613: dp = cvs_buf_load(pb->description, BUF_AUTOEXT);
614: if (dp == NULL) {
615: cvs_log(LP_ERR,
616: "failed to load description file '%s'",
617: pb->description);
618: free(filec);
619: return (-1);
620: }
621: if (cvs_buf_putc(dp, '\0') < 0) {
622: free(filec);
623: return (-1);
624: }
625: rcs_desc = (const char *)cvs_buf_release(dp);
626: }
627: }
1.58 niallo 628: rcs_desc_set(pb->file, rcs_desc);
629:
1.73 xsa 630: /* Now add our new revision */
1.82 ! joris 631: if (rcs_rev_add(pb->file, RCS_HEAD_REV, LOG_INIT, -1, pb->author) != 0) {
1.58 niallo 632: cvs_log(LP_ERR, "failed to add new revision");
1.63 niallo 633: return (-1);
634: }
635: /*
636: * If we are checking in to a non-default (ie user-specified)
637: * revision, set head to this revision.
638: */
639: if (pb->newrev != NULL)
640: rcs_head_set(pb->file, pb->newrev);
641: else
642: pb->newrev = pb->file->rf_head;
643:
1.73 xsa 644: /* New head revision has to contain entire file; */
1.66 niallo 645: if (rcs_deltatext_set(pb->file, pb->file->rf_head, filec) == -1) {
1.63 niallo 646: cvs_log(LP_ERR, "failed to set new head revision");
647: return (-1);
1.58 niallo 648: }
1.73 xsa 649: /* Attach a symbolic name to this revision if specified. */
1.66 niallo 650: if (pb->symbol != NULL
651: && (checkin_attach_symbol(pb) < 0))
652: return (-1);
653:
1.73 xsa 654: /* Set the state of this revision if specified. */
1.66 niallo 655: if (pb->state != NULL)
656: (void)rcs_state_set(pb->file, pb->newrev, pb->state);
657:
658: free(filec);
659: (void)unlink(pb->filename);
660:
1.73 xsa 661: /* Do checkout if -u or -l are specified. */
1.66 niallo 662: if (((pb->flags & CO_LOCK) || (pb->flags & CO_UNLOCK))
663: && !(pb->flags & CI_DEFAULT))
664: checkout_rev(pb->file, pb->newrev, pb->filename, pb->flags,
1.82 ! joris 665: pb->username, pb->author, NULL);
1.63 niallo 666:
1.66 niallo 667: /* File will NOW be synced */
668: rcs_close(pb->file);
1.63 niallo 669: return (0);
1.58 niallo 670: }
671:
1.59 niallo 672: /*
673: * checkin_attach_symbol()
674: *
675: * Attempt to attach the specified symbol to the revision.
676: * On success, return 0. On error return -1.
677: */
1.58 niallo 678: static int
679: checkin_attach_symbol(struct checkin_params *pb)
680: {
681: char rbuf[16];
682: int ret;
683: if (verbose == 1)
684: printf("symbol: %s\n", pb->symbol);
685: if (pb->flags & CI_SYMFORCE)
686: rcs_sym_remove(pb->file, pb->symbol);
687: if ((ret = rcs_sym_add(pb->file, pb->symbol, pb->newrev) == -1)
688: && (rcs_errno == RCS_ERR_DUPENT)) {
689: rcsnum_tostr(rcs_sym_getrev(pb->file, pb->symbol),
690: rbuf, sizeof(rbuf));
691: cvs_log(LP_ERR,
692: "symbolic name %s already bound to %s",
693: pb->symbol, rbuf);
694: rcs_close(pb->file);
695: return (-1);
696: } else if (ret == -1) {
697: cvs_log(LP_ERR, "problem adding symbol: %s",
698: pb->symbol);
699: rcs_close(pb->file);
700: return (-1);
701: }
702: return (0);
703: }
704:
1.59 niallo 705: /*
706: * checkin_revert()
707: *
708: * If there are no differences between the working file and the latest revision
709: * and the -f flag is not specified, simply revert to the latest version and
710: * warn the user.
711: *
712: */
1.58 niallo 713: static void
714: checkin_revert(struct checkin_params *pb)
715: {
716: char rbuf[16];
717:
718: rcsnum_tostr(pb->frev, rbuf, sizeof(rbuf));
719: cvs_log(LP_WARN,
720: "file is unchanged; reverting to previous revision %s",
721: rbuf);
722: (void)unlink(pb->filename);
723: if ((pb->flags & CO_LOCK) || (pb->flags & CO_UNLOCK))
724: checkout_rev(pb->file, pb->frev, pb->filename,
1.82 ! joris 725: pb->flags, pb->username, pb->author, NULL);
! 726: rcs_lock_remove(pb->file, pb->username, pb->frev);
1.58 niallo 727: rcs_close(pb->file);
728: if (verbose == 1)
729: printf("done\n");
730: }
731:
1.59 niallo 732: /*
733: * checkin_checklock()
734: *
735: * Check for the existence of a lock on the file. If there are no locks, or it
736: * is not locked by the correct user, return -1. Otherwise, return 0.
737: */
1.58 niallo 738: static int
739: checkin_checklock(struct checkin_params *pb)
740: {
1.82 ! joris 741: int notlocked = 1;
1.58 niallo 742: struct rcs_lock *lkp;
743:
1.82 ! joris 744: TAILQ_FOREACH(lkp, &(pb->file->rf_locks), rl_list) {
! 745: if ((!strcmp(lkp->rl_name, pb->username)) &&
! 746: (!rcsnum_cmp(lkp->rl_num, pb->frev, 0)))
! 747: return (0);
1.58 niallo 748: }
1.32 joris 749:
1.82 ! joris 750: cvs_log(LP_ERR,
! 751: "%s: no lock set by %s", pb->file->rf_path, pb->username);
! 752: rcs_close(pb->file);
! 753: return (-1);
1.61 niallo 754: }
755:
756: /*
1.62 niallo 757: * checkin_mtimedate()
1.61 niallo 758: *
759: * Set the date of the revision to be the last modification
1.62 niallo 760: * time of the working file.
1.61 niallo 761: *
762: * On success, return 0. On error return -1.
763: */
764: static int
1.62 niallo 765: checkin_mtimedate(struct checkin_params *pb)
1.61 niallo 766: {
767: struct stat sb;
768: if (stat(pb->filename, &sb) != 0) {
1.72 xsa 769: cvs_log(LP_ERRNO, "failed to stat `%s'", pb->filename);
1.61 niallo 770: rcs_close(pb->file);
771: return (-1);
772: }
773: pb->date = (time_t)sb.st_mtimespec.tv_sec;
1.58 niallo 774: return (0);
1.63 niallo 775: }
776:
777: /*
1.75 niallo 778: * checkin_choose_rcsfile()
1.63 niallo 779: *
780: * Given a relative filename, decide where the corresponding ,v file
1.65 xsa 781: * should be.
1.63 niallo 782: *
783: * Returns pointer to a char array on success, NULL on failure.
784: */
785: static char *
786: checkin_choose_rcsfile(const char *filename)
787: {
1.76 niallo 788: char name[MAXPATHLEN], *basepath;
789: const char *ptr;
1.63 niallo 790: size_t len;
791: struct stat sb;
792:
1.76 niallo 793: if ((basepath = malloc(MAXPATHLEN)) == NULL) {
794: cvs_log(LP_ERRNO, "could not allocate memory");
1.63 niallo 795: return (NULL);
796: }
1.76 niallo 797: if (strchr(filename, '/') == NULL) {
798: strlcat(basepath, RCSDIR"/", MAXPATHLEN);
799: if ((stat(basepath, &sb) == 0) && (sb.st_mode & S_IFDIR)) {
800: /* <path>/RCS/<filename>,v */
801: strlcat(basepath, filename, MAXPATHLEN);
802: strlcat(basepath, RCS_FILE_EXT, MAXPATHLEN);
803: } else {
804: /* <path>/<filename>,v */
805: memset(basepath, '\0', MAXPATHLEN);
806: strlcpy(basepath, filename, MAXPATHLEN);
807: strlcat(basepath, RCS_FILE_EXT, MAXPATHLEN);
808: }
809: } else {
810: ptr = filename;
811: /* Walk backwards till we find the base directory */
812: len = strlen(filename);
813: ptr += len + 1;
814: while (filename[len] != '/') {
815: len--;
816: ptr--;
817: }
1.63 niallo 818: /*
819: * Need two bytes extra for trailing slash and
820: * NUL-termination.
821: */
822: len += 2;
1.76 niallo 823: if (len > MAXPATHLEN) {
824: free(basepath);
1.63 niallo 825: return (NULL);
826: }
1.76 niallo 827: strlcpy(basepath, filename, len);
828: strlcpy(name, ptr, MAXPATHLEN);
1.63 niallo 829: strlcat(basepath, RCSDIR"/", MAXPATHLEN);
830: if ((stat(basepath, &sb) == 0) && (sb.st_mode & S_IFDIR)) {
831: /* <path>/RCS/<filename>,v */
1.76 niallo 832: strlcat(basepath, name, MAXPATHLEN);
1.63 niallo 833: strlcat(basepath, RCS_FILE_EXT, MAXPATHLEN);
834: } else {
835: /* <path>/<filename>,v */
1.69 xsa 836: memset(basepath, '\0', MAXPATHLEN);
1.76 niallo 837: strlcpy(basepath, filename, MAXPATHLEN);
1.63 niallo 838: strlcat(basepath, RCS_FILE_EXT, MAXPATHLEN);
839: }
1.76 niallo 840: }
841: return (basepath);
1.1 niallo 842: }