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

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