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

1.11    ! niallo      1: /*     $OpenBSD: ci.c,v 1.10 2005/10/08 16:19:40 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.6       niallo     53: static char * checkin_getlogmsg(char *, char *, RCSNUM *);
1.4       niallo     54:
1.1       niallo     55: void
                     56: checkin_usage(void)
                     57: {
                     58:        fprintf(stderr,
1.10      niallo     59:            "usage: %s [-jlMNqruV] [-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.6       niallo     72:        int i, ch, dflag, flags, lkmode, interactive;
1.1       niallo     73:        mode_t fmode;
                     74:        RCSFILE *file;
1.4       niallo     75:        RCSNUM *frev;
1.1       niallo     76:        char fpath[MAXPATHLEN];
1.9       niallo     77:        char *rcs_msg, *rev, *filec, *deltatext, *username;
1.4       niallo     78:        BUF *bp;
1.1       niallo     79:
                     80:        flags = RCS_RDWR;
                     81:        file = NULL;
1.4       niallo     82:        rcs_msg = rev = NULL;
1.9       niallo     83:        fmode = lkmode = dflag = verbose = 0;
1.6       niallo     84:        interactive = 1;
1.1       niallo     85:
1.9       niallo     86:        if ((username = getlogin()) == NULL) {
                     87:                cvs_log(LP_ERR, "failed to get username");
                     88:                exit(1);
                     89:        }
                     90:
                     91:        while ((ch = getopt(argc, argv, "j:lM:N:qud:r::m:k:V")) != -1) {
1.1       niallo     92:                switch (ch) {
                     93:                case 'h':
                     94:                        (usage)();
                     95:                        exit(0);
                     96:                case 'm':
                     97:                        rcs_msg = optarg;
1.6       niallo     98:                        interactive = 0;
1.3       joris      99:                        break;
                    100:                case 'q':
                    101:                        verbose = 0;
1.1       niallo    102:                        break;
                    103:                case 'V':
                    104:                        printf("%s\n", rcs_version);
                    105:                        exit(0);
1.9       niallo    106:                case 'l':
                    107:                        lkmode = LOCK_LOCK;
                    108:                        break;
                    109:                case 'u':
                    110:                        lkmode = LOCK_UNLOCK;
                    111:                        break;
1.1       niallo    112:                default:
                    113:                        (usage)();
                    114:                        exit(1);
                    115:                }
                    116:        }
                    117:
                    118:        argc -= optind;
                    119:        argv += optind;
                    120:        if (argc == 0) {
                    121:                cvs_log(LP_ERR, "no input file");
                    122:                (usage)();
                    123:                exit(1);
                    124:        }
                    125:
                    126:        for (i = 0; i < argc; i++) {
                    127:                if (rcs_statfile(argv[i], fpath, sizeof(fpath)) < 0)
                    128:                        continue;
                    129:
1.4       niallo    130:                file = rcs_open(fpath, RCS_RDWR, fmode);
1.1       niallo    131:                if (file == NULL) {
1.4       niallo    132:                        cvs_log(LP_ERR, "failed to open rcsfile '%s'", fpath);
1.1       niallo    133:                        exit(1);
                    134:                }
1.4       niallo    135:
                    136:                if (dflag) {
1.6       niallo    137:                        /* XXX */
1.1       niallo    138:                }
1.4       niallo    139:
                    140:                /*
                    141:                 * Load file contents
                    142:                 */
                    143:                if ((bp = cvs_buf_load(argv[i], BUF_AUTOEXT)) == NULL) {
                    144:                        cvs_log(LP_ERR, "failed to load '%s'", argv[i]);
                    145:                        exit(1);
                    146:                }
                    147:
1.6       niallo    148:                if (rev == NULL)
                    149:                        frev = file->rf_head;
                    150:                /*
                    151:                 * If no log message specified, get it interactively.
                    152:                 */
                    153:                if (rcs_msg == NULL)
                    154:                        rcs_msg = checkin_getlogmsg(fpath, argv[i], frev);
                    155:
1.4       niallo    156:                if (cvs_buf_putc(bp, '\0') < 0)
                    157:                        exit(1);
                    158:
                    159:                filec = cvs_buf_release(bp);
                    160:
                    161:                /*
                    162:                 * Remove the lock
                    163:                 */
                    164:                if (rcs_lock_remove(file, frev) < 0) {
                    165:                        if (rcs_errno != RCS_ERR_NOENT)
                    166:                            cvs_log(LP_WARN, "failed to remove lock");
                    167:                 }
                    168:
                    169:                /*
                    170:                 * Get RCS patch
                    171:                 */
                    172:                if ((deltatext = checkin_diff_file(file, frev, argv[i])) == NULL) {
                    173:                        cvs_log(LP_ERR, "failed to get diff");
                    174:                        exit(1);
                    175:                }
                    176:
                    177:                /*
                    178:                 * Current head revision gets the RCS patch as rd_text
                    179:                 */
                    180:                if (rcs_deltatext_set(file, file->rf_head, deltatext) == -1) {
1.7       niallo    181:                        cvs_log(LP_ERR,
                    182:                            "failed to set new rd_text for head rev");
1.4       niallo    183:                        exit (1);
                    184:                }
                    185:                /*
                    186:                 * Now add our new revision
                    187:                 */
                    188:                if (rcs_rev_add(file, RCS_HEAD_REV, rcs_msg, -1) != 0) {
                    189:                        cvs_log(LP_ERR, "failed to add new revision");
                    190:                        exit(1);
                    191:                }
                    192:
                    193:                /*
                    194:                 * New head revision has to contain entire file;
                    195:                 */
                    196:                 if (rcs_deltatext_set(file, frev, filec) == -1) {
                    197:                        cvs_log(LP_ERR, "failed to set new head revision");
                    198:                        exit(1);
                    199:                }
                    200:
                    201:                free(deltatext);
                    202:                free(filec);
1.9       niallo    203:                (void)unlink(argv[i]);
1.4       niallo    204:
1.9       niallo    205:                /*
                    206:                 * Do checkout if -u or -l are specified.
                    207:                 */
                    208:                if (lkmode != 0) {
                    209:                        mode_t mode = 0;
                    210:                        if ((bp = rcs_getrev(file, frev)) == NULL) {
                    211:                                cvs_log(LP_ERR, "cannot get revision");
                    212:                                goto err;
                    213:                        }
                    214:                        if (lkmode == LOCK_LOCK) {
                    215:                                mode = 0644;
                    216:                                if (rcs_lock_add(file, username, frev) < 0) {
                    217:                                        if (rcs_errno != RCS_ERR_DUPENT)
                    218:                                                cvs_log(LP_ERR,
                    219:                                                    "failed to lock revision");
                    220:                                        else
                    221:                                                cvs_log(LP_ERR,
                    222:                                                    "you already have a lock");
                    223:                                }
                    224:                        } else if (lkmode == LOCK_UNLOCK) {
                    225:                                mode = 0444;
                    226:                        }
                    227:                        if (cvs_buf_write(bp, argv[i], mode) < 0) {
                    228:                                cvs_log(LP_ERR,
                    229:                                    "failed to write revision to file");
                    230:                        }
                    231:                        cvs_buf_free(bp);
                    232:                }
                    233: err:
1.4       niallo    234:                /* File will NOW be synced */
1.1       niallo    235:                rcs_close(file);
1.4       niallo    236:
1.6       niallo    237:                if (interactive) {
                    238:                        free(rcs_msg);
                    239:                        rcs_msg = NULL;
                    240:                }
1.4       niallo    241:        }
                    242:
                    243:        return (0);
                    244: }
                    245:
                    246: static char *
                    247: checkin_diff_file(RCSFILE *rfp, RCSNUM *rev, const char *filename)
                    248: {
                    249:        char path1[MAXPATHLEN], path2[MAXPATHLEN];
                    250:        BUF *b1, *b2, *b3;
                    251:        char rbuf[64], *deltatext;
                    252:
                    253:        rcsnum_tostr(rev, rbuf, sizeof(rbuf));
                    254:
                    255:        if ((b1 = cvs_buf_load(filename, BUF_AUTOEXT)) == NULL) {
                    256:                cvs_log(LP_ERR, "failed to load file: '%s'", filename);
                    257:                return (NULL);
1.1       niallo    258:        }
                    259:
1.4       niallo    260:        if ((b2 = rcs_getrev(rfp, rev)) == NULL) {
                    261:                cvs_log(LP_ERR, "failed to load revision");
                    262:                cvs_buf_free(b1);
                    263:                return (NULL);
                    264:        }
                    265:
                    266:        if ((b3 = cvs_buf_alloc(128, BUF_AUTOEXT)) == NULL) {
                    267:                cvs_log(LP_ERR, "failed to allocated buffer for diff");
                    268:                cvs_buf_free(b1);
                    269:                cvs_buf_free(b2);
                    270:                return (NULL);
                    271:        }
                    272:
                    273:        strlcpy(path1, "/tmp/diff1.XXXXXXXXXX", sizeof(path1));
                    274:        if (cvs_buf_write_stmp(b1, path1, 0600) == -1) {
                    275:                cvs_log(LP_ERRNO, "could not write temporary file");
                    276:                cvs_buf_free(b1);
                    277:                cvs_buf_free(b2);
                    278:                return (NULL);
                    279:        }
                    280:        cvs_buf_free(b1);
                    281:
                    282:        strlcpy(path2, "/tmp/diff2.XXXXXXXXXX", sizeof(path2));
                    283:        if (cvs_buf_write_stmp(b2, path2, 0600) == -1) {
                    284:                cvs_buf_free(b2);
                    285:                (void)unlink(path1);
                    286:                return (NULL);
                    287:        }
                    288:        cvs_buf_free(b2);
                    289:
1.5       niallo    290:        diff_format = D_RCSDIFF;
1.4       niallo    291:        cvs_diffreg(path1, path2, b3);
                    292:        (void)unlink(path1);
                    293:        (void)unlink(path2);
                    294:
                    295:        cvs_buf_putc(b3, '\0');
                    296:        deltatext = (char *)cvs_buf_release(b3);
                    297:
                    298:        return (deltatext);
1.6       niallo    299: }
                    300:
                    301: /*
                    302:  * Get log message from user interactively.
                    303:  */
                    304: static char *
                    305: checkin_getlogmsg(char *rcsfile, char *workingfile, RCSNUM *rev)
                    306: {
                    307:        char   *rcs_msg, buf[128], nrev[16], prev[16];
                    308:        BUF    *logbuf;
                    309:        RCSNUM *tmprev;
                    310:
1.7       niallo    311:        rcs_msg = NULL;
1.6       niallo    312:        tmprev = rcsnum_alloc();
                    313:        rcsnum_cpy(rev, tmprev, 16);
1.9       niallo    314:        rcsnum_tostr(tmprev, prev, sizeof(prev));
1.6       niallo    315:        rcsnum_tostr(rcsnum_inc(tmprev), nrev, sizeof(nrev));
                    316:        rcsnum_free(tmprev);
                    317:
                    318:        if ((logbuf = cvs_buf_alloc(64, BUF_AUTOEXT)) == NULL) {
1.7       niallo    319:                cvs_log(LP_ERR, "failed to allocate log buffer");
1.6       niallo    320:                return (NULL);
                    321:        }
                    322:        cvs_printf("%s  <--  %s\n", rcsfile, workingfile);
1.7       niallo    323:        cvs_printf("new revision: %s; previous revision: %s\n", nrev, prev);
1.6       niallo    324:        cvs_printf("enter log message, terminated with single "
                    325:            "'.' or end of file:\n");
                    326:        cvs_printf(">> ");
                    327:        for (;;) {
                    328:                fgets(buf, (int)sizeof(buf), stdin);
1.9       niallo    329:                if (feof(stdin) || ferror(stdin) || buf[0] == '.')
1.6       niallo    330:                        break;
                    331:                cvs_buf_append(logbuf, buf, strlen(buf));
                    332:                cvs_printf(">> ");
                    333:        }
1.8       niallo    334:        cvs_buf_putc(logbuf, '\0');
1.6       niallo    335:        rcs_msg = (char *)cvs_buf_release(logbuf);
                    336:        return (rcs_msg);
1.1       niallo    337: }