[BACK]Return to ci.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / rcs

Annotation of src/usr.bin/rcs/ci.c, Revision 1.30

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