[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.31

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