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

1.16    ! niallo      1: /*     $OpenBSD: ci.c,v 1.15 2005/10/09 21:17:59 niallo Exp $  */
1.1       niallo      2: /*
                      3:  * Copyright (c) 2005 Niall O'Higgins <niallo@openbsd.org>
                      4:  * All rights reserved.
                      5:  *
                      6:  * Redistribution and use in source and binary forms, with or without
                      7:  * modification, are permitted provided that the following cinditions
                      8:  * are met:
                      9:  *
                     10:  * 1. Redistributions of source cide must retain the above cipyright
                     11:  *    notice, this list of cinditions and the following disclaimer.
                     12:  * 2. The name of the author may not be used to endorse or promote products
                     13:  *    derived from this software without specific prior written permission.
                     14:  *
                     15:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
                     16:  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
                     17:  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
                     18:  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
                     19:  * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
                     20:  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
                     21:  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
                     22:  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
                     23:  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
                     24:  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     25:  */
                     26:
                     27: #include <sys/param.h>
                     28: #include <sys/types.h>
                     29: #include <sys/stat.h>
                     30: #include <sys/wait.h>
                     31:
                     32: #include <err.h>
                     33: #include <pwd.h>
                     34: #include <errno.h>
                     35: #include <stdio.h>
                     36: #include <ctype.h>
                     37: #include <stdlib.h>
                     38: #include <unistd.h>
                     39: #include <signal.h>
                     40: #include <string.h>
                     41:
                     42: #include "log.h"
                     43: #include "rcs.h"
1.4       niallo     44: #include "diff.h"
1.1       niallo     45: #include "rcsprog.h"
                     46:
                     47: extern char *__progname;
                     48:
1.9       niallo     49: #define LOCK_LOCK      1
                     50: #define LOCK_UNLOCK    2
                     51:
1.4       niallo     52: static char * checkin_diff_file(RCSFILE *, RCSNUM *, const char *);
1.12      niallo     53: static char * checkin_getlogmsg(char *, char *, RCSNUM *, RCSNUM *);
1.4       niallo     54:
1.1       niallo     55: void
                     56: checkin_usage(void)
                     57: {
                     58:        fprintf(stderr,
1.12      niallo     59:            "usage: %s [-jlMNquV] [-d date] [-r [rev]] [-m msg] [-k mode] "
1.1       niallo     60:            "file ...\n", __progname);
                     61: }
                     62:
                     63: /*
                     64:  * checkin_main()
                     65:  *
                     66:  * Handler for the `ci' program.
                     67:  * Returns 0 on success, or >0 on error.
                     68:  */
                     69: int
                     70: checkin_main(int argc, char **argv)
                     71: {
1.16    ! niallo     72:        int i, ch, dflag, flags, lkmode, interactive, rflag, status;
1.1       niallo     73:        mode_t fmode;
                     74:        RCSFILE *file;
1.12      niallo     75:        RCSNUM *frev, *newrev;
1.1       niallo     76:        char fpath[MAXPATHLEN];
1.12      niallo     77:        char *rcs_msg, *filec, *deltatext, *username;
1.16    ! niallo     78:        struct rcs_lock *lkp;
1.4       niallo     79:        BUF *bp;
1.1       niallo     80:
                     81:        flags = RCS_RDWR;
                     82:        file = NULL;
1.12      niallo     83:        rcs_msg = NULL;
                     84:        newrev =  NULL;
1.16    ! niallo     85:        fmode = lkmode = dflag = verbose = rflag = status = 0;
1.6       niallo     86:        interactive = 1;
1.1       niallo     87:
1.9       niallo     88:        if ((username = getlogin()) == NULL) {
                     89:                cvs_log(LP_ERR, "failed to get username");
                     90:                exit(1);
                     91:        }
                     92:
                     93:        while ((ch = getopt(argc, argv, "j:lM:N:qud:r::m:k:V")) != -1) {
1.1       niallo     94:                switch (ch) {
                     95:                case 'h':
                     96:                        (usage)();
                     97:                        exit(0);
                     98:                case 'm':
                     99:                        rcs_msg = optarg;
1.6       niallo    100:                        interactive = 0;
1.3       joris     101:                        break;
                    102:                case 'q':
                    103:                        verbose = 0;
1.1       niallo    104:                        break;
                    105:                case 'V':
                    106:                        printf("%s\n", rcs_version);
                    107:                        exit(0);
1.9       niallo    108:                case 'l':
                    109:                        lkmode = LOCK_LOCK;
                    110:                        break;
                    111:                case 'u':
                    112:                        lkmode = LOCK_UNLOCK;
                    113:                        break;
1.12      niallo    114:                case 'r':
                    115:                        rflag = 1;
                    116:                        if (optarg != NULL) {
                    117:                                if ((newrev = rcsnum_parse(optarg)) == NULL) {
                    118:                                        cvs_log(LP_ERR, "bad revision number");
                    119:                                        exit(1);
                    120:                                }
                    121:                        }
                    122:                        break;
1.1       niallo    123:                default:
                    124:                        (usage)();
                    125:                        exit(1);
                    126:                }
                    127:        }
                    128:
                    129:        argc -= optind;
                    130:        argv += optind;
                    131:        if (argc == 0) {
                    132:                cvs_log(LP_ERR, "no input file");
                    133:                (usage)();
                    134:                exit(1);
                    135:        }
                    136:
                    137:        for (i = 0; i < argc; i++) {
                    138:                if (rcs_statfile(argv[i], fpath, sizeof(fpath)) < 0)
                    139:                        continue;
                    140:
1.4       niallo    141:                file = rcs_open(fpath, RCS_RDWR, fmode);
1.1       niallo    142:                if (file == NULL) {
1.4       niallo    143:                        cvs_log(LP_ERR, "failed to open rcsfile '%s'", fpath);
1.1       niallo    144:                        exit(1);
                    145:                }
1.4       niallo    146:
                    147:                if (dflag) {
1.6       niallo    148:                        /* XXX */
1.1       niallo    149:                }
1.4       niallo    150:
                    151:                /*
                    152:                 * Load file contents
                    153:                 */
                    154:                if ((bp = cvs_buf_load(argv[i], BUF_AUTOEXT)) == NULL) {
                    155:                        cvs_log(LP_ERR, "failed to load '%s'", argv[i]);
                    156:                        exit(1);
                    157:                }
                    158:
1.12      niallo    159:                if (cvs_buf_putc(bp, '\0') < 0)
                    160:                        exit(1);
                    161:
                    162:                filec = cvs_buf_release(bp);
                    163:
                    164:                /*
                    165:                 * If rev is not specified on the command line,
                    166:                 * assume HEAD.
                    167:                 */
                    168:                frev = file->rf_head;
1.13      joris     169:
1.6       niallo    170:                /*
1.16    ! niallo    171:                 * Check for a lock belonging to this user. If none,
        !           172:                 * abort check-in.
        !           173:                 */
        !           174:                if (TAILQ_EMPTY(&(file->rf_locks))) {
        !           175:                        cvs_log(LP_ERR, "%s: no lock set by %s", fpath,
        !           176:                            username);
        !           177:                        status = 1;
        !           178:                        continue;
        !           179:                } else {
        !           180:                        TAILQ_FOREACH(lkp, &(file->rf_locks), rl_list) {
        !           181:                                if ((strcmp(lkp->rl_name, username) != 0)
        !           182:                                    && (rcsnum_cmp(lkp->rl_num, frev, 0))) {
        !           183:                                        cvs_log(LP_ERR,
        !           184:                                            "%s: no lock set by %s", fpath,
        !           185:                                            username);
        !           186:                                        status = 1;
        !           187:                                        continue;
        !           188:                                }
        !           189:                        }
        !           190:                }
        !           191:
        !           192:                /*
1.6       niallo    193:                 * If no log message specified, get it interactively.
                    194:                 */
1.14      joris     195:                if (rcs_msg == NULL)
1.12      niallo    196:                        rcs_msg = checkin_getlogmsg(fpath, argv[i], frev, newrev);
1.4       niallo    197:
                    198:                /*
                    199:                 * Remove the lock
                    200:                 */
                    201:                if (rcs_lock_remove(file, frev) < 0) {
                    202:                        if (rcs_errno != RCS_ERR_NOENT)
                    203:                            cvs_log(LP_WARN, "failed to remove lock");
                    204:                 }
                    205:
                    206:                /*
                    207:                 * Get RCS patch
                    208:                 */
                    209:                if ((deltatext = checkin_diff_file(file, frev, argv[i])) == NULL) {
                    210:                        cvs_log(LP_ERR, "failed to get diff");
                    211:                        exit(1);
                    212:                }
                    213:
                    214:                /*
                    215:                 * Current head revision gets the RCS patch as rd_text
                    216:                 */
1.12      niallo    217:                if (rcs_deltatext_set(file, frev, deltatext) == -1) {
1.7       niallo    218:                        cvs_log(LP_ERR,
                    219:                            "failed to set new rd_text for head rev");
1.4       niallo    220:                        exit (1);
                    221:                }
                    222:                /*
                    223:                 * Now add our new revision
                    224:                 */
1.12      niallo    225:                if (rcs_rev_add(file, (newrev == NULL ? RCS_HEAD_REV : newrev),
                    226:                    rcs_msg, -1) != 0) {
1.4       niallo    227:                        cvs_log(LP_ERR, "failed to add new revision");
                    228:                        exit(1);
                    229:                }
                    230:
                    231:                /*
1.12      niallo    232:                 * If we are checking in to a non-default (ie user-specified)
                    233:                 * revision, set head to this revision.
                    234:                 */
                    235:                if (newrev != NULL)
                    236:                        rcs_head_set(file, newrev);
1.15      niallo    237:                else
                    238:                        newrev = file->rf_head;
1.12      niallo    239:                /*
1.4       niallo    240:                 * New head revision has to contain entire file;
                    241:                 */
                    242:                 if (rcs_deltatext_set(file, frev, filec) == -1) {
                    243:                        cvs_log(LP_ERR, "failed to set new head revision");
                    244:                        exit(1);
                    245:                }
                    246:
                    247:                free(deltatext);
                    248:                free(filec);
1.9       niallo    249:                (void)unlink(argv[i]);
1.4       niallo    250:
1.9       niallo    251:                /*
                    252:                 * Do checkout if -u or -l are specified.
                    253:                 */
1.12      niallo    254:                if (lkmode != 0 && !rflag) {
1.9       niallo    255:                        mode_t mode = 0;
1.12      niallo    256:                        if ((bp = rcs_getrev(file, newrev)) == NULL) {
1.9       niallo    257:                                cvs_log(LP_ERR, "cannot get revision");
                    258:                                goto err;
                    259:                        }
                    260:                        if (lkmode == LOCK_LOCK) {
                    261:                                mode = 0644;
1.12      niallo    262:                                if (rcs_lock_add(file, username, newrev) < 0) {
1.9       niallo    263:                                        if (rcs_errno != RCS_ERR_DUPENT)
                    264:                                                cvs_log(LP_ERR,
                    265:                                                    "failed to lock revision");
                    266:                                        else
                    267:                                                cvs_log(LP_ERR,
                    268:                                                    "you already have a lock");
                    269:                                }
                    270:                        } else if (lkmode == LOCK_UNLOCK) {
                    271:                                mode = 0444;
                    272:                        }
                    273:                        if (cvs_buf_write(bp, argv[i], mode) < 0) {
                    274:                                cvs_log(LP_ERR,
                    275:                                    "failed to write revision to file");
                    276:                        }
                    277:                        cvs_buf_free(bp);
                    278:                }
                    279: err:
1.4       niallo    280:                /* File will NOW be synced */
1.1       niallo    281:                rcs_close(file);
1.4       niallo    282:
1.6       niallo    283:                if (interactive) {
                    284:                        free(rcs_msg);
                    285:                        rcs_msg = NULL;
                    286:                }
1.4       niallo    287:        }
                    288:
1.16    ! niallo    289:        return (status);
1.4       niallo    290: }
                    291:
                    292: static char *
                    293: checkin_diff_file(RCSFILE *rfp, RCSNUM *rev, const char *filename)
                    294: {
                    295:        char path1[MAXPATHLEN], path2[MAXPATHLEN];
                    296:        BUF *b1, *b2, *b3;
                    297:        char rbuf[64], *deltatext;
                    298:
                    299:        rcsnum_tostr(rev, rbuf, sizeof(rbuf));
                    300:
                    301:        if ((b1 = cvs_buf_load(filename, BUF_AUTOEXT)) == NULL) {
                    302:                cvs_log(LP_ERR, "failed to load file: '%s'", filename);
                    303:                return (NULL);
1.1       niallo    304:        }
                    305:
1.4       niallo    306:        if ((b2 = rcs_getrev(rfp, rev)) == NULL) {
                    307:                cvs_log(LP_ERR, "failed to load revision");
                    308:                cvs_buf_free(b1);
                    309:                return (NULL);
                    310:        }
                    311:
                    312:        if ((b3 = cvs_buf_alloc(128, BUF_AUTOEXT)) == NULL) {
                    313:                cvs_log(LP_ERR, "failed to allocated buffer for diff");
                    314:                cvs_buf_free(b1);
                    315:                cvs_buf_free(b2);
                    316:                return (NULL);
                    317:        }
                    318:
                    319:        strlcpy(path1, "/tmp/diff1.XXXXXXXXXX", sizeof(path1));
                    320:        if (cvs_buf_write_stmp(b1, path1, 0600) == -1) {
                    321:                cvs_log(LP_ERRNO, "could not write temporary file");
                    322:                cvs_buf_free(b1);
                    323:                cvs_buf_free(b2);
                    324:                return (NULL);
                    325:        }
                    326:        cvs_buf_free(b1);
                    327:
                    328:        strlcpy(path2, "/tmp/diff2.XXXXXXXXXX", sizeof(path2));
                    329:        if (cvs_buf_write_stmp(b2, path2, 0600) == -1) {
                    330:                cvs_buf_free(b2);
                    331:                (void)unlink(path1);
                    332:                return (NULL);
                    333:        }
                    334:        cvs_buf_free(b2);
                    335:
1.5       niallo    336:        diff_format = D_RCSDIFF;
1.4       niallo    337:        cvs_diffreg(path1, path2, b3);
                    338:        (void)unlink(path1);
                    339:        (void)unlink(path2);
                    340:
                    341:        cvs_buf_putc(b3, '\0');
                    342:        deltatext = (char *)cvs_buf_release(b3);
                    343:
                    344:        return (deltatext);
1.6       niallo    345: }
                    346:
                    347: /*
                    348:  * Get log message from user interactively.
                    349:  */
                    350: static char *
1.12      niallo    351: checkin_getlogmsg(char *rcsfile, char *workingfile, RCSNUM *rev, RCSNUM *rev2)
1.6       niallo    352: {
                    353:        char   *rcs_msg, buf[128], nrev[16], prev[16];
                    354:        BUF    *logbuf;
                    355:        RCSNUM *tmprev;
                    356:
1.7       niallo    357:        rcs_msg = NULL;
1.6       niallo    358:        tmprev = rcsnum_alloc();
                    359:        rcsnum_cpy(rev, tmprev, 16);
1.9       niallo    360:        rcsnum_tostr(tmprev, prev, sizeof(prev));
1.12      niallo    361:        if (rev2 == NULL)
                    362:                rcsnum_tostr(rcsnum_inc(tmprev), nrev, sizeof(nrev));
                    363:        else
                    364:                rcsnum_tostr(rev2, nrev, sizeof(nrev));
1.6       niallo    365:        rcsnum_free(tmprev);
                    366:
                    367:        if ((logbuf = cvs_buf_alloc(64, BUF_AUTOEXT)) == NULL) {
1.7       niallo    368:                cvs_log(LP_ERR, "failed to allocate log buffer");
1.6       niallo    369:                return (NULL);
                    370:        }
                    371:        cvs_printf("%s  <--  %s\n", rcsfile, workingfile);
1.7       niallo    372:        cvs_printf("new revision: %s; previous revision: %s\n", nrev, prev);
1.6       niallo    373:        cvs_printf("enter log message, terminated with single "
                    374:            "'.' or end of file:\n");
                    375:        cvs_printf(">> ");
                    376:        for (;;) {
                    377:                fgets(buf, (int)sizeof(buf), stdin);
1.9       niallo    378:                if (feof(stdin) || ferror(stdin) || buf[0] == '.')
1.6       niallo    379:                        break;
                    380:                cvs_buf_append(logbuf, buf, strlen(buf));
                    381:                cvs_printf(">> ");
                    382:        }
1.8       niallo    383:        cvs_buf_putc(logbuf, '\0');
1.6       niallo    384:        rcs_msg = (char *)cvs_buf_release(logbuf);
                    385:        return (rcs_msg);
1.1       niallo    386: }