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

Annotation of src/usr.bin/cvs/commit.c, Revision 1.76

1.76    ! joris       1: /*     $OpenBSD: commit.c,v 1.75 2006/06/19 05:05:17 joris Exp $       */
1.1       jfb         2: /*
1.55      joris       3:  * Copyright (c) 2006 Joris Vink <joris@openbsd.org>
1.71      xsa         4:  * Copyright (c) 2006 Xavier Santolaria <xsa@openbsd.org>
1.1       jfb         5:  *
1.55      joris       6:  * Permission to use, copy, modify, and distribute this software for any
                      7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
1.1       jfb         9:  *
1.55      joris      10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     13:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     14:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     15:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     16:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1       jfb        17:  */
                     18:
1.51      xsa        19: #include "includes.h"
1.1       jfb        20:
                     21: #include "cvs.h"
1.55      joris      22: #include "diff.h"
1.1       jfb        23: #include "log.h"
                     24:
1.55      joris      25: int    cvs_commit(int, char **);
                     26: void   cvs_commit_local(struct cvs_file *);
                     27: void   cvs_commit_check_conflicts(struct cvs_file *);
                     28:
                     29: static char *commit_diff_file(struct cvs_file *);
1.71      xsa        30: static void commit_desc_set(struct cvs_file *);
1.55      joris      31:
                     32: struct cvs_flisthead files_affected;
                     33: int    conflicts_found;
                     34: char   *logmsg;
1.34      jfb        35:
                     36: struct cvs_cmd cvs_cmd_commit = {
1.74      joris      37:        CVS_OP_COMMIT, 0, "commit",
1.55      joris      38:        { "ci", "com" },
1.34      jfb        39:        "Check files into the repository",
                     40:        "[-flR] [-F logfile | -m msg] [-r rev] ...",
                     41:        "F:flm:Rr:",
1.18      joris      42:        NULL,
1.55      joris      43:        cvs_commit
1.18      joris      44: };
1.1       jfb        45:
1.55      joris      46: int
                     47: cvs_commit(int argc, char **argv)
1.1       jfb        48: {
1.18      joris      49:        int ch;
1.70      joris      50:        BUF *bp;
1.55      joris      51:        char *arg = ".";
1.58      joris      52:        int flags;
1.55      joris      53:        struct cvs_recursion cr;
1.3       krapht     54:
1.58      joris      55:        flags = CR_RECURSE_DIRS;
                     56:
1.55      joris      57:        while ((ch = getopt(argc, argv, cvs_cmd_commit.cmd_opts)) != -1) {
1.1       jfb        58:                switch (ch) {
1.55      joris      59:                case 'f':
                     60:                        break;
1.1       jfb        61:                case 'F':
1.70      joris      62:                        bp = cvs_buf_load(optarg, BUF_AUTOEXT);
                     63:                        if (bp == NULL)
                     64:                                fatal("failed to load commit message %s",
                     65:                                    optarg);
                     66:                        cvs_buf_putc(bp, '\0');
                     67:                        logmsg = cvs_buf_release(bp);
1.1       jfb        68:                        break;
                     69:                case 'l':
1.58      joris      70:                        flags &= ~CR_RECURSE_DIRS;
1.1       jfb        71:                        break;
                     72:                case 'm':
1.55      joris      73:                        logmsg = xstrdup(optarg);
                     74:                        break;
                     75:                case 'r':
1.1       jfb        76:                        break;
                     77:                case 'R':
1.35      xsa        78:                        break;
1.1       jfb        79:                default:
1.55      joris      80:                        fatal("%s", cvs_cmd_commit.cmd_synopsis);
1.1       jfb        81:                }
                     82:        }
                     83:
1.55      joris      84:        argc -= optind;
                     85:        argv += optind;
1.1       jfb        86:
1.55      joris      87:        if (logmsg == NULL)
1.72      joris      88:                fatal("please use -m or -F to specify a log message for now");
1.1       jfb        89:
1.55      joris      90:        TAILQ_INIT(&files_affected);
                     91:        conflicts_found = 0;
1.32      joris      92:
1.55      joris      93:        cr.enterdir = NULL;
                     94:        cr.leavedir = NULL;
1.75      joris      95:        cr.fileproc = cvs_commit_check_conflicts;
1.58      joris      96:        cr.flags = flags;
1.55      joris      97:
                     98:        if (argc > 0)
                     99:                cvs_file_run(argc, argv, &cr);
                    100:        else
                    101:                cvs_file_run(1, &arg, &cr);
                    102:
                    103:        if (conflicts_found != 0)
                    104:                fatal("%d conflicts found, please correct these first",
                    105:                    conflicts_found);
                    106:
1.75      joris     107:        cr.fileproc = cvs_commit_local;
1.55      joris     108:        cvs_file_walklist(&files_affected, &cr);
                    109:        cvs_file_freelist(&files_affected);
1.32      joris     110:
1.18      joris     111:        return (0);
                    112: }
1.1       jfb       113:
1.55      joris     114: void
                    115: cvs_commit_check_conflicts(struct cvs_file *cf)
1.18      joris     116: {
1.55      joris     117:        cvs_log(LP_TRACE, "cvs_commit_check_conflicts(%s)", cf->file_path);
1.44      joris     118:
1.43      joris     119:        /*
1.55      joris     120:         * cvs_file_classify makes the noise for us
                    121:         * XXX - we want that?
1.43      joris     122:         */
1.65      joris     123:        cvs_file_classify(cf, NULL, 1);
1.59      joris     124:
                    125:        if (cf->file_type == CVS_DIR) {
                    126:                if (verbosity > 1)
                    127:                        cvs_log(LP_NOTICE, "Examining %s", cf->file_path);
                    128:                return;
                    129:        }
1.32      joris     130:
1.55      joris     131:        if (cf->file_status == FILE_CONFLICT ||
                    132:            cf->file_status == FILE_LOST ||
                    133:            cf->file_status == FILE_UNLINK)
                    134:                conflicts_found++;
1.57      joris     135:
1.63      joris     136:        if (cf->file_status != FILE_REMOVED &&
                    137:            update_has_conflict_markers(cf)) {
1.60      joris     138:                cvs_log(LP_ERR, "conflict: unresolved conflicts in %s from "
                    139:                    "merging, please fix these first", cf->file_path);
                    140:                conflicts_found++;
                    141:        }
                    142:
1.57      joris     143:        if (cf->file_status == FILE_MERGE ||
1.72      joris     144:            cf->file_status == FILE_PATCH ||
                    145:            cf->file_status == FILE_CHECKOUT) {
1.57      joris     146:                cvs_log(LP_ERR, "conflict: %s is not up-to-date",
                    147:                    cf->file_path);
                    148:                conflicts_found++;
                    149:        }
1.55      joris     150:
                    151:        if (cf->file_status == FILE_ADDED ||
                    152:            cf->file_status == FILE_REMOVED ||
                    153:            cf->file_status == FILE_MODIFIED)
                    154:                cvs_file_get(cf->file_path, &files_affected);
                    155: }
1.1       jfb       156:
1.55      joris     157: void
                    158: cvs_commit_local(struct cvs_file *cf)
                    159: {
                    160:        BUF *b;
1.67      joris     161:        int isnew;
1.66      joris     162:        int l, openflags, rcsflags;
1.61      joris     163:        char *d, *f, rbuf[24];
1.56      joris     164:        CVSENTRIES *entlist;
1.66      joris     165:        char *attic, *repo, *rcsfile;
1.17      joris     166:
1.55      joris     167:        cvs_log(LP_TRACE, "cvs_commit_local(%s)", cf->file_path);
1.65      joris     168:        cvs_file_classify(cf, NULL, 0);
1.69      joris     169:
                    170:        if (cvs_noexec == 1)
                    171:                return;
1.35      xsa       172:
1.64      joris     173:        if (cf->file_type != CVS_FILE)
                    174:                fatal("cvs_commit_local: '%s' is not a file", cf->file_path);
                    175:
1.56      joris     176:        if (cf->file_status == FILE_MODIFIED ||
1.66      joris     177:            cf->file_status == FILE_REMOVED || (cf->file_status == FILE_ADDED
                    178:            && cf->file_rcs != NULL && cf->file_rcs->rf_dead == 1))
1.68      joris     179:                rcsnum_tostr(rcs_head_get(cf->file_rcs), rbuf, sizeof(rbuf));
1.61      joris     180:        else
                    181:                strlcpy(rbuf, "Non-existent", sizeof(rbuf));
                    182:
1.67      joris     183:        isnew = 0;
1.66      joris     184:        if (cf->file_status == FILE_ADDED) {
1.67      joris     185:                isnew = 1;
1.66      joris     186:                rcsflags = RCS_CREATE;
                    187:                openflags = O_CREAT | O_TRUNC | O_WRONLY;
                    188:                if (cf->file_rcs != NULL) {
                    189:                        if (cf->file_rcs->rf_inattic == 0)
                    190:                                cvs_log(LP_ERR, "warning: expected %s "
                    191:                                    "to be in the Attic", cf->file_path);
                    192:
                    193:                        if (cf->file_rcs->rf_dead == 0)
                    194:                                cvs_log(LP_ERR, "warning: expected %s "
                    195:                                    "to be dead", cf->file_path);
                    196:
                    197:                        rcsfile = xmalloc(MAXPATHLEN);
                    198:                        repo = xmalloc(MAXPATHLEN);
                    199:                        cvs_get_repository_path(cf->file_wd, repo, MAXPATHLEN);
                    200:                        l = snprintf(rcsfile, MAXPATHLEN, "%s/%s%s",
                    201:                            repo, cf->file_name, RCS_FILE_EXT);
                    202:                        if (l == -1 || l >= MAXPATHLEN)
                    203:                                fatal("cvs_commit_local: overflow");
                    204:
                    205:                        if (rename(cf->file_rpath, rcsfile) == -1)
                    206:                                fatal("cvs_commit_local: failed to move %s "
                    207:                                    "outside the Attic: %s", cf->file_path,
                    208:                                    strerror(errno));
                    209:
                    210:                        xfree(cf->file_rpath);
                    211:                        cf->file_rpath = xstrdup(rcsfile);
                    212:                        xfree(rcsfile);
                    213:                        xfree(repo);
                    214:
                    215:                        rcsflags = RCS_READ | RCS_PARSE_FULLY;
                    216:                        openflags = O_RDONLY;
                    217:                        rcs_close(cf->file_rcs);
1.67      joris     218:                        isnew = 0;
1.66      joris     219:                }
                    220:
                    221:                cf->repo_fd = open(cf->file_rpath, openflags);
1.61      joris     222:                if (cf->repo_fd < 0)
                    223:                        fatal("cvs_commit_local: %s", strerror(errno));
                    224:
                    225:                cf->file_rcs = rcs_open(cf->file_rpath, cf->repo_fd,
1.66      joris     226:                    rcsflags, 0600);
1.61      joris     227:                if (cf->file_rcs == NULL)
                    228:                        fatal("cvs_commit_local: failed to create RCS file "
                    229:                            "for %s", cf->file_path);
1.71      xsa       230:
                    231:                commit_desc_set(cf);
1.61      joris     232:        }
1.36      joris     233:
1.55      joris     234:        cvs_printf("Checking in %s:\n", cf->file_path);
                    235:        cvs_printf("%s <- %s\n", cf->file_rpath, cf->file_path);
                    236:        cvs_printf("old revision: %s; ", rbuf);
1.7       jfb       237:
1.67      joris     238:        if (isnew == 0)
1.61      joris     239:                d = commit_diff_file(cf);
1.7       jfb       240:
1.56      joris     241:        if (cf->file_status == FILE_REMOVED) {
                    242:                b = rcs_getrev(cf->file_rcs, cf->file_rcs->rf_head);
                    243:                if (b == NULL)
                    244:                        fatal("cvs_commit_local: failed to get HEAD");
1.61      joris     245:        } else {
1.73      joris     246:                if ((b = cvs_buf_load_fd(cf->fd, BUF_AUTOEXT)) == NULL)
1.56      joris     247:                        fatal("cvs_commit_local: failed to load file");
                    248:        }
1.7       jfb       249:
1.55      joris     250:        cvs_buf_putc(b, '\0');
                    251:        f = cvs_buf_release(b);
1.7       jfb       252:
1.67      joris     253:        if (isnew == 0) {
1.61      joris     254:                if (rcs_deltatext_set(cf->file_rcs,
                    255:                    cf->file_rcs->rf_head, d) == -1)
                    256:                        fatal("cvs_commit_local: failed to set delta");
                    257:        }
1.3       krapht    258:
1.55      joris     259:        if (rcs_rev_add(cf->file_rcs, RCS_HEAD_REV, logmsg, -1, NULL) == -1)
                    260:                fatal("cvs_commit_local: failed to add new revision");
1.3       krapht    261:
1.55      joris     262:        if (rcs_deltatext_set(cf->file_rcs, cf->file_rcs->rf_head, f) == -1)
                    263:                fatal("cvs_commit_local: failed to set new HEAD delta");
1.3       krapht    264:
1.55      joris     265:        xfree(f);
1.61      joris     266:
1.67      joris     267:        if (isnew == 0)
1.61      joris     268:                xfree(d);
1.6       jfb       269:
1.56      joris     270:        if (cf->file_status == FILE_REMOVED) {
                    271:                if (rcs_state_set(cf->file_rcs,
                    272:                    cf->file_rcs->rf_head, RCS_STATE_DEAD) == -1)
                    273:                        fatal("cvs_commit_local: failed to set state");
1.68      joris     274:        }
                    275:
                    276:        if (cf->file_rcs->rf_branch != NULL) {
                    277:                rcsnum_free(cf->file_rcs->rf_branch);
                    278:                cf->file_rcs->rf_branch = NULL;
1.56      joris     279:        }
                    280:
1.55      joris     281:        rcs_write(cf->file_rcs);
1.6       jfb       282:
1.56      joris     283:        if (cf->file_status == FILE_REMOVED) {
                    284:                strlcpy(rbuf, "Removed", sizeof(rbuf));
1.61      joris     285:        } else if (cf->file_status == FILE_ADDED) {
1.66      joris     286:                if (cf->file_rcs->rf_dead == 1)
1.63      joris     287:                        strlcpy(rbuf, "Initial Revision", sizeof(rbuf));
                    288:                else
                    289:                        rcsnum_tostr(cf->file_rcs->rf_head,
                    290:                            rbuf, sizeof(rbuf));
1.56      joris     291:        } else if (cf->file_status == FILE_MODIFIED) {
                    292:                rcsnum_tostr(cf->file_rcs->rf_head, rbuf, sizeof(rbuf));
                    293:        }
                    294:
1.55      joris     295:        cvs_printf("new revision: %s\n", rbuf);
1.3       krapht    296:
1.55      joris     297:        (void)unlink(cf->file_path);
                    298:        (void)close(cf->fd);
                    299:        cf->fd = -1;
1.56      joris     300:
                    301:        if (cf->file_status != FILE_REMOVED) {
1.60      joris     302:                b = rcs_getrev(cf->file_rcs, cf->file_rcs->rf_head);
                    303:                if (b == NULL)
                    304:                        fatal("cvs_commit_local: failed to get HEAD");
                    305:
                    306:                cvs_checkout_file(cf, cf->file_rcs->rf_head, b, 0);
1.56      joris     307:        } else {
                    308:                entlist = cvs_ent_open(cf->file_wd);
                    309:                cvs_ent_remove(entlist, cf->file_name);
                    310:                cvs_ent_close(entlist, ENT_SYNC);
1.64      joris     311:
                    312:                repo = xmalloc(MAXPATHLEN);
                    313:                attic = xmalloc(MAXPATHLEN);
                    314:                cvs_get_repository_path(cf->file_wd, repo, MAXPATHLEN);
                    315:
                    316:                l = snprintf(attic, MAXPATHLEN, "%s/%s", repo, CVS_PATH_ATTIC);
                    317:                if (l == -1 || l >= MAXPATHLEN)
                    318:                        fatal("cvs_commit_local: overflow");
                    319:
                    320:                if (mkdir(attic, 0755) == -1 && errno != EEXIST)
                    321:                        fatal("cvs_commit_local: failed to create Attic");
                    322:
                    323:                l = snprintf(attic, MAXPATHLEN, "%s/%s/%s%s", repo,
                    324:                    CVS_PATH_ATTIC, cf->file_name, RCS_FILE_EXT);
                    325:                if (l == -1 || l >= MAXPATHLEN)
                    326:                        fatal("cvs_commit_local: overflow");
                    327:
                    328:                if (rename(cf->file_rpath, attic) == -1)
                    329:                        fatal("cvs_commit_local: failed to move %s to Attic",
                    330:                            cf->file_path);
                    331:
                    332:                xfree(repo);
                    333:                xfree(attic);
1.56      joris     334:        }
1.3       krapht    335:
1.55      joris     336:        cvs_printf("done\n");
1.16      joris     337:
1.39      xsa       338: }
                    339:
1.55      joris     340: static char *
                    341: commit_diff_file(struct cvs_file *cf)
1.39      xsa       342: {
1.55      joris     343:        char*delta,  *p1, *p2;
                    344:        BUF *b1, *b2, *b3;
1.39      xsa       345:
1.63      joris     346:        if (cf->file_status == FILE_MODIFIED ||
                    347:            cf->file_status == FILE_ADDED) {
1.73      joris     348:                if ((b1 = cvs_buf_load_fd(cf->fd, BUF_AUTOEXT)) == NULL)
1.56      joris     349:                        fatal("commit_diff_file: failed to load '%s'",
                    350:                            cf->file_path);
1.63      joris     351:        } else {
1.56      joris     352:                b1 = rcs_getrev(cf->file_rcs, cf->file_rcs->rf_head);
                    353:                if (b1 == NULL)
                    354:                        fatal("commit_diff_file: failed to load HEAD");
                    355:                b1 = rcs_kwexp_buf(b1, cf->file_rcs, cf->file_rcs->rf_head);
                    356:        }
1.3       krapht    357:
1.55      joris     358:        if ((b2 = rcs_getrev(cf->file_rcs, cf->file_rcs->rf_head)) == NULL)
                    359:                fatal("commit_diff_file: failed to load HEAD for '%s'",
                    360:                    cf->file_path);
                    361:
                    362:        if ((b3 = cvs_buf_alloc(128, BUF_AUTOEXT)) == NULL)
                    363:                fatal("commit_diff_file: failed to create diff buf");
                    364:
                    365:        (void)xasprintf(&p1, "%s/diff1.XXXXXXXXXX", cvs_tmpdir);
                    366:        cvs_buf_write_stmp(b1, p1, 0600, NULL);
                    367:        cvs_buf_free(b1);
                    368:
                    369:        (void)xasprintf(&p2, "%s/diff2.XXXXXXXXXX", cvs_tmpdir);
                    370:        cvs_buf_write_stmp(b2, p2, 0600, NULL);
                    371:        cvs_buf_free(b2);
                    372:
                    373:        diff_format = D_RCSDIFF;
                    374:        if (cvs_diffreg(p1, p2, b3) == D_ERROR)
                    375:                fatal("commit_diff_file: failed to get RCS patch");
                    376:
                    377:        cvs_buf_putc(b3, '\0');
                    378:        delta = cvs_buf_release(b3);
                    379:        return (delta);
1.71      xsa       380: }
                    381:
                    382: static void
                    383: commit_desc_set(struct cvs_file *cf)
                    384: {
                    385:        BUF *bp;
1.76    ! joris     386:        int l, fd;
1.71      xsa       387:        char *desc_path, *desc;
                    388:
                    389:        desc_path = xmalloc(MAXPATHLEN);
                    390:        l = snprintf(desc_path, MAXPATHLEN, "%s/%s%s",
                    391:            CVS_PATH_CVSDIR, cf->file_name, CVS_DESCR_FILE_EXT);
                    392:        if (l == -1 || l >= MAXPATHLEN)
                    393:                fatal("commit_desc_set: overflow");
                    394:
1.76    ! joris     395:        if ((fd = open(desc_path, O_RDONLY)) == -1) {
1.71      xsa       396:                xfree(desc_path);
                    397:                return;
                    398:        }
                    399:
1.76    ! joris     400:        cvs_buf_load_fd(fd, BUF_AUTOEXT);
1.71      xsa       401:        cvs_buf_putc(bp, '\0');
                    402:        desc = cvs_buf_release(bp);
                    403:
                    404:        rcs_desc_set(cf->file_rcs, desc);
                    405:
1.76    ! joris     406:        (void)close(fd);
1.71      xsa       407:        (void)cvs_unlink(desc_path);
                    408:
                    409:        xfree(desc);
                    410:        xfree(desc_path);
1.1       jfb       411: }