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

1.109   ! joris       1: /*     $OpenBSD: commit.c,v 1.108 2007/06/28 17:45:49 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.105     otto       19: #include <sys/stat.h>
                     20:
                     21: #include <errno.h>
                     22: #include <fcntl.h>
                     23: #include <string.h>
                     24: #include <unistd.h>
1.1       jfb        25:
                     26: #include "cvs.h"
1.55      joris      27: #include "diff.h"
1.80      joris      28: #include "remote.h"
1.1       jfb        29:
1.55      joris      30: void   cvs_commit_local(struct cvs_file *);
1.86      joris      31: void   cvs_commit_check_files(struct cvs_file *);
1.55      joris      32:
1.91      joris      33: static BUF *commit_diff_file(struct cvs_file *);
1.71      xsa        34: static void commit_desc_set(struct cvs_file *);
1.55      joris      35:
                     36: struct cvs_flisthead files_affected;
1.86      joris      37: struct cvs_flisthead files_added;
                     38: struct cvs_flisthead files_removed;
                     39: struct cvs_flisthead files_modified;
                     40:
1.55      joris      41: int    conflicts_found;
1.86      joris      42: char   *logmsg = NULL;
1.34      jfb        43:
                     44: struct cvs_cmd cvs_cmd_commit = {
1.74      joris      45:        CVS_OP_COMMIT, 0, "commit",
1.55      joris      46:        { "ci", "com" },
1.34      jfb        47:        "Check files into the repository",
                     48:        "[-flR] [-F logfile | -m msg] [-r rev] ...",
                     49:        "F:flm:Rr:",
1.18      joris      50:        NULL,
1.55      joris      51:        cvs_commit
1.18      joris      52: };
1.1       jfb        53:
1.55      joris      54: int
                     55: cvs_commit(int argc, char **argv)
1.1       jfb        56: {
1.18      joris      57:        int ch;
1.55      joris      58:        char *arg = ".";
1.58      joris      59:        int flags;
1.55      joris      60:        struct cvs_recursion cr;
1.3       krapht     61:
1.58      joris      62:        flags = CR_RECURSE_DIRS;
                     63:
1.55      joris      64:        while ((ch = getopt(argc, argv, cvs_cmd_commit.cmd_opts)) != -1) {
1.1       jfb        65:                switch (ch) {
                     66:                case 'F':
1.88      joris      67:                        logmsg = cvs_logmsg_read(optarg);
1.1       jfb        68:                        break;
1.82      xsa        69:                case 'f':
                     70:                        break;
1.1       jfb        71:                case 'l':
1.58      joris      72:                        flags &= ~CR_RECURSE_DIRS;
1.1       jfb        73:                        break;
                     74:                case 'm':
1.55      joris      75:                        logmsg = xstrdup(optarg);
                     76:                        break;
1.82      xsa        77:                case 'R':
                     78:                        break;
1.55      joris      79:                case 'r':
1.1       jfb        80:                        break;
                     81:                default:
1.55      joris      82:                        fatal("%s", cvs_cmd_commit.cmd_synopsis);
1.1       jfb        83:                }
                     84:        }
                     85:
1.55      joris      86:        argc -= optind;
                     87:        argv += optind;
1.1       jfb        88:
1.86      joris      89:        TAILQ_INIT(&files_affected);
                     90:        TAILQ_INIT(&files_added);
                     91:        TAILQ_INIT(&files_removed);
                     92:        TAILQ_INIT(&files_modified);
                     93:        conflicts_found = 0;
                     94:
                     95:        cr.enterdir = NULL;
                     96:        cr.leavedir = NULL;
                     97:        cr.fileproc = cvs_commit_check_files;
                     98:        cr.flags = flags;
                     99:
                    100:        if (argc > 0)
                    101:                cvs_file_run(argc, argv, &cr);
                    102:        else
                    103:                cvs_file_run(1, &arg, &cr);
                    104:
                    105:        if (conflicts_found != 0)
                    106:                fatal("%d conflicts found, please correct these first",
                    107:                    conflicts_found);
1.92      joris     108:
                    109:        if (TAILQ_EMPTY(&files_affected))
                    110:                return (0);
1.86      joris     111:
                    112:        if (logmsg == NULL && cvs_server_active == 0) {
                    113:                logmsg = cvs_logmsg_create(&files_added, &files_removed,
                    114:                    &files_modified);
                    115:        }
                    116:
1.55      joris     117:        if (logmsg == NULL)
1.86      joris     118:                fatal("This shouldnt happen, honestly!");
1.87      joris     119:
                    120:        cvs_file_freelist(&files_modified);
                    121:        cvs_file_freelist(&files_removed);
                    122:        cvs_file_freelist(&files_added);
1.80      joris     123:
                    124:        if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
1.89      joris     125:                cvs_client_connect_to_server();
1.80      joris     126:                cr.fileproc = cvs_client_sendfile;
                    127:
                    128:                if (argc > 0)
                    129:                        cvs_file_run(argc, argv, &cr);
                    130:                else
                    131:                        cvs_file_run(1, &arg, &cr);
1.99      xsa       132:
                    133:                if (!(flags & CR_RECURSE_DIRS))
                    134:                        cvs_client_send_request("Argument -l");
1.80      joris     135:
1.109   ! joris     136:                if (logmsg != NULL)
        !           137:                        cvs_client_send_logmsg(logmsg);
1.80      joris     138:
                    139:                cvs_client_send_files(argv, argc);
                    140:                cvs_client_senddir(".");
                    141:                cvs_client_send_request("ci");
                    142:                cvs_client_get_responses();
1.86      joris     143:        } else {
                    144:                cr.fileproc = cvs_commit_local;
                    145:                cvs_file_walklist(&files_affected, &cr);
                    146:                cvs_file_freelist(&files_affected);
1.80      joris     147:        }
1.1       jfb       148:
1.18      joris     149:        return (0);
                    150: }
1.1       jfb       151:
1.55      joris     152: void
1.86      joris     153: cvs_commit_check_files(struct cvs_file *cf)
1.18      joris     154: {
1.86      joris     155:        cvs_log(LP_TRACE, "cvs_commit_check_files(%s)", cf->file_path);
1.44      joris     156:
1.108     joris     157:        if (current_cvsroot->cr_method != CVS_METHOD_LOCAL)
                    158:                cvs_remote_classify_file(cf);
                    159:        else
                    160:                cvs_file_classify(cf, NULL);
1.59      joris     161:
                    162:        if (cf->file_type == CVS_DIR) {
                    163:                if (verbosity > 1)
                    164:                        cvs_log(LP_NOTICE, "Examining %s", cf->file_path);
                    165:                return;
                    166:        }
1.32      joris     167:
1.55      joris     168:        if (cf->file_status == FILE_CONFLICT ||
1.79      joris     169:            cf->file_status == FILE_UNLINK) {
1.55      joris     170:                conflicts_found++;
1.79      joris     171:                return;
                    172:        }
1.57      joris     173:
1.63      joris     174:        if (cf->file_status != FILE_REMOVED &&
                    175:            update_has_conflict_markers(cf)) {
1.60      joris     176:                cvs_log(LP_ERR, "conflict: unresolved conflicts in %s from "
                    177:                    "merging, please fix these first", cf->file_path);
                    178:                conflicts_found++;
1.79      joris     179:                return;
1.60      joris     180:        }
                    181:
1.57      joris     182:        if (cf->file_status == FILE_MERGE ||
1.72      joris     183:            cf->file_status == FILE_PATCH ||
1.79      joris     184:            cf->file_status == FILE_CHECKOUT ||
                    185:            cf->file_status == FILE_LOST) {
1.57      joris     186:                cvs_log(LP_ERR, "conflict: %s is not up-to-date",
                    187:                    cf->file_path);
                    188:                conflicts_found++;
1.79      joris     189:                return;
1.57      joris     190:        }
1.55      joris     191:
                    192:        if (cf->file_status == FILE_ADDED ||
                    193:            cf->file_status == FILE_REMOVED ||
                    194:            cf->file_status == FILE_MODIFIED)
                    195:                cvs_file_get(cf->file_path, &files_affected);
1.86      joris     196:
                    197:        switch (cf->file_status) {
                    198:        case FILE_ADDED:
                    199:                cvs_file_get(cf->file_path, &files_added);
                    200:                break;
                    201:        case FILE_REMOVED:
                    202:                cvs_file_get(cf->file_path, &files_removed);
                    203:                break;
                    204:        case FILE_MODIFIED:
                    205:                cvs_file_get(cf->file_path, &files_modified);
                    206:                break;
                    207:        }
1.55      joris     208: }
1.1       jfb       209:
1.55      joris     210: void
                    211: cvs_commit_local(struct cvs_file *cf)
                    212: {
1.91      joris     213:        BUF *b, *d;
1.107     joris     214:        int isnew, histtype;
1.93      joris     215:        RCSNUM *head;
1.102     xsa       216:        int openflags, rcsflags;
1.91      joris     217:        char rbuf[24], nbuf[24];
1.56      joris     218:        CVSENTRIES *entlist;
1.100     otto      219:        char attic[MAXPATHLEN], repo[MAXPATHLEN], rcsfile[MAXPATHLEN];
1.17      joris     220:
1.55      joris     221:        cvs_log(LP_TRACE, "cvs_commit_local(%s)", cf->file_path);
1.104     joris     222:        cvs_file_classify(cf, NULL);
1.69      joris     223:
                    224:        if (cvs_noexec == 1)
                    225:                return;
1.35      xsa       226:
1.64      joris     227:        if (cf->file_type != CVS_FILE)
                    228:                fatal("cvs_commit_local: '%s' is not a file", cf->file_path);
                    229:
1.107     joris     230:        if (cf->file_status != FILE_MODIFIED &&
                    231:            cf->file_status != FILE_ADDED &&
                    232:            cf->file_status != FILE_REMOVED) {
                    233:                cvs_log(LP_ERR, "skipping bogus file `%s'", cf->file_path);
                    234:                return;
                    235:        }
                    236:
1.56      joris     237:        if (cf->file_status == FILE_MODIFIED ||
1.66      joris     238:            cf->file_status == FILE_REMOVED || (cf->file_status == FILE_ADDED
1.93      joris     239:            && cf->file_rcs != NULL && cf->file_rcs->rf_dead == 1)) {
                    240:                head = rcs_head_get(cf->file_rcs);
                    241:                rcsnum_tostr(head, rbuf, sizeof(rbuf));
                    242:                rcsnum_free(head);
                    243:        } else {
1.61      joris     244:                strlcpy(rbuf, "Non-existent", sizeof(rbuf));
1.93      joris     245:        }
1.61      joris     246:
1.67      joris     247:        isnew = 0;
1.66      joris     248:        if (cf->file_status == FILE_ADDED) {
1.67      joris     249:                isnew = 1;
1.66      joris     250:                rcsflags = RCS_CREATE;
                    251:                openflags = O_CREAT | O_TRUNC | O_WRONLY;
                    252:                if (cf->file_rcs != NULL) {
1.106     niallo    253:                        if (cf->in_attic == 0)
1.66      joris     254:                                cvs_log(LP_ERR, "warning: expected %s "
                    255:                                    "to be in the Attic", cf->file_path);
                    256:
                    257:                        if (cf->file_rcs->rf_dead == 0)
                    258:                                cvs_log(LP_ERR, "warning: expected %s "
                    259:                                    "to be dead", cf->file_path);
                    260:
                    261:                        cvs_get_repository_path(cf->file_wd, repo, MAXPATHLEN);
1.102     xsa       262:                        (void)xsnprintf(rcsfile, MAXPATHLEN, "%s/%s%s",
1.66      joris     263:                            repo, cf->file_name, RCS_FILE_EXT);
                    264:
                    265:                        if (rename(cf->file_rpath, rcsfile) == -1)
                    266:                                fatal("cvs_commit_local: failed to move %s "
                    267:                                    "outside the Attic: %s", cf->file_path,
                    268:                                    strerror(errno));
                    269:
                    270:                        xfree(cf->file_rpath);
                    271:                        cf->file_rpath = xstrdup(rcsfile);
                    272:
                    273:                        rcsflags = RCS_READ | RCS_PARSE_FULLY;
                    274:                        openflags = O_RDONLY;
                    275:                        rcs_close(cf->file_rcs);
1.67      joris     276:                        isnew = 0;
1.66      joris     277:                }
                    278:
                    279:                cf->repo_fd = open(cf->file_rpath, openflags);
1.61      joris     280:                if (cf->repo_fd < 0)
                    281:                        fatal("cvs_commit_local: %s", strerror(errno));
                    282:
                    283:                cf->file_rcs = rcs_open(cf->file_rpath, cf->repo_fd,
1.103     otto      284:                    rcsflags, 0444);
1.61      joris     285:                if (cf->file_rcs == NULL)
                    286:                        fatal("cvs_commit_local: failed to create RCS file "
                    287:                            "for %s", cf->file_path);
1.71      xsa       288:
                    289:                commit_desc_set(cf);
1.61      joris     290:        }
1.36      joris     291:
1.77      reyk      292:        if (verbosity > 1) {
                    293:                cvs_printf("Checking in %s:\n", cf->file_path);
                    294:                cvs_printf("%s <- %s\n", cf->file_rpath, cf->file_path);
                    295:                cvs_printf("old revision: %s; ", rbuf);
                    296:        }
1.7       jfb       297:
1.67      joris     298:        if (isnew == 0)
1.61      joris     299:                d = commit_diff_file(cf);
1.7       jfb       300:
1.56      joris     301:        if (cf->file_status == FILE_REMOVED) {
1.98      joris     302:                b = rcs_rev_getbuf(cf->file_rcs, cf->file_rcs->rf_head, 0);
1.56      joris     303:                if (b == NULL)
                    304:                        fatal("cvs_commit_local: failed to get HEAD");
1.61      joris     305:        } else {
1.73      joris     306:                if ((b = cvs_buf_load_fd(cf->fd, BUF_AUTOEXT)) == NULL)
1.56      joris     307:                        fatal("cvs_commit_local: failed to load file");
                    308:        }
1.7       jfb       309:
1.67      joris     310:        if (isnew == 0) {
1.61      joris     311:                if (rcs_deltatext_set(cf->file_rcs,
                    312:                    cf->file_rcs->rf_head, d) == -1)
                    313:                        fatal("cvs_commit_local: failed to set delta");
                    314:        }
1.3       krapht    315:
1.55      joris     316:        if (rcs_rev_add(cf->file_rcs, RCS_HEAD_REV, logmsg, -1, NULL) == -1)
                    317:                fatal("cvs_commit_local: failed to add new revision");
1.3       krapht    318:
1.91      joris     319:        if (rcs_deltatext_set(cf->file_rcs, cf->file_rcs->rf_head, b) == -1)
1.55      joris     320:                fatal("cvs_commit_local: failed to set new HEAD delta");
1.3       krapht    321:
1.56      joris     322:        if (cf->file_status == FILE_REMOVED) {
                    323:                if (rcs_state_set(cf->file_rcs,
                    324:                    cf->file_rcs->rf_head, RCS_STATE_DEAD) == -1)
                    325:                        fatal("cvs_commit_local: failed to set state");
1.68      joris     326:        }
                    327:
                    328:        if (cf->file_rcs->rf_branch != NULL) {
                    329:                rcsnum_free(cf->file_rcs->rf_branch);
                    330:                cf->file_rcs->rf_branch = NULL;
1.101     xsa       331:        }
                    332:
                    333:        if (cf->file_status == FILE_ADDED && cf->file_ent->ce_opts != NULL) {
                    334:                int kflag;
                    335:
                    336:                kflag = rcs_kflag_get(cf->file_ent->ce_opts + 2);
                    337:                rcs_kwexp_set(cf->file_rcs, kflag);
1.56      joris     338:        }
                    339:
1.55      joris     340:        rcs_write(cf->file_rcs);
1.6       jfb       341:
1.56      joris     342:        if (cf->file_status == FILE_REMOVED) {
1.77      reyk      343:                strlcpy(nbuf, "Removed", sizeof(nbuf));
1.61      joris     344:        } else if (cf->file_status == FILE_ADDED) {
1.66      joris     345:                if (cf->file_rcs->rf_dead == 1)
1.77      reyk      346:                        strlcpy(nbuf, "Initial Revision", sizeof(nbuf));
1.63      joris     347:                else
                    348:                        rcsnum_tostr(cf->file_rcs->rf_head,
1.77      reyk      349:                            nbuf, sizeof(nbuf));
1.56      joris     350:        } else if (cf->file_status == FILE_MODIFIED) {
1.77      reyk      351:                rcsnum_tostr(cf->file_rcs->rf_head, nbuf, sizeof(nbuf));
1.56      joris     352:        }
                    353:
1.77      reyk      354:        if (verbosity > 1)
                    355:                cvs_printf("new revision: %s\n", nbuf);
1.3       krapht    356:
1.55      joris     357:        (void)unlink(cf->file_path);
                    358:        (void)close(cf->fd);
                    359:        cf->fd = -1;
1.56      joris     360:
                    361:        if (cf->file_status != FILE_REMOVED) {
1.95      joris     362:                cvs_checkout_file(cf, cf->file_rcs->rf_head, CO_COMMIT);
1.56      joris     363:        } else {
                    364:                entlist = cvs_ent_open(cf->file_wd);
                    365:                cvs_ent_remove(entlist, cf->file_name);
                    366:                cvs_ent_close(entlist, ENT_SYNC);
1.64      joris     367:
                    368:                cvs_get_repository_path(cf->file_wd, repo, MAXPATHLEN);
                    369:
1.102     xsa       370:                (void)xsnprintf(attic, MAXPATHLEN, "%s/%s",
                    371:                    repo, CVS_PATH_ATTIC);
1.64      joris     372:
                    373:                if (mkdir(attic, 0755) == -1 && errno != EEXIST)
                    374:                        fatal("cvs_commit_local: failed to create Attic");
                    375:
1.102     xsa       376:                (void)xsnprintf(attic, MAXPATHLEN, "%s/%s/%s%s", repo,
1.64      joris     377:                    CVS_PATH_ATTIC, cf->file_name, RCS_FILE_EXT);
                    378:
                    379:                if (rename(cf->file_rpath, attic) == -1)
                    380:                        fatal("cvs_commit_local: failed to move %s to Attic",
                    381:                            cf->file_path);
                    382:
1.85      joris     383:                if (cvs_server_active == 1)
                    384:                        cvs_server_update_entry("Remove-entry", cf);
1.56      joris     385:        }
1.3       krapht    386:
1.77      reyk      387:        if (verbosity > 1)
                    388:                cvs_printf("done\n");
                    389:        else {
                    390:                cvs_log(LP_NOTICE, "checking in '%s'; revision %s -> %s",
                    391:                    cf->file_path, rbuf, nbuf);
                    392:        }
1.107     joris     393:
                    394:        switch (cf->file_status) {
                    395:        case FILE_MODIFIED:
                    396:                histtype = CVS_HISTORY_COMMIT_MODIFIED;
                    397:                break;
                    398:        case FILE_ADDED:
                    399:                histtype = CVS_HISTORY_COMMIT_ADDED;
                    400:                break;
                    401:        case FILE_REMOVED:
                    402:                histtype = CVS_HISTORY_COMMIT_REMOVED;
                    403:                break;
                    404:        }
                    405:
                    406:        cvs_history_add(histtype, cf, NULL);
1.39      xsa       407: }
                    408:
1.91      joris     409: static BUF *
1.55      joris     410: commit_diff_file(struct cvs_file *cf)
1.39      xsa       411: {
1.91      joris     412:        char *p1, *p2;
1.97      joris     413:        BUF *b1, *b2;
                    414:
                    415:        (void)xasprintf(&p1, "%s/diff1.XXXXXXXXXX", cvs_tmpdir);
1.39      xsa       416:
1.63      joris     417:        if (cf->file_status == FILE_MODIFIED ||
                    418:            cf->file_status == FILE_ADDED) {
1.73      joris     419:                if ((b1 = cvs_buf_load_fd(cf->fd, BUF_AUTOEXT)) == NULL)
1.56      joris     420:                        fatal("commit_diff_file: failed to load '%s'",
                    421:                            cf->file_path);
1.97      joris     422:                cvs_buf_write_stmp(b1, p1, NULL);
                    423:                cvs_buf_free(b1);
1.63      joris     424:        } else {
1.97      joris     425:                rcs_rev_write_stmp(cf->file_rcs, cf->file_rcs->rf_head, p1, 0);
1.56      joris     426:        }
1.3       krapht    427:
1.97      joris     428:        (void)xasprintf(&p2, "%s/diff2.XXXXXXXXXX", cvs_tmpdir);
                    429:        rcs_rev_write_stmp(cf->file_rcs, cf->file_rcs->rf_head, p2, 0);
1.55      joris     430:
1.97      joris     431:        if ((b2 = cvs_buf_alloc(128, BUF_AUTOEXT)) == NULL)
1.55      joris     432:                fatal("commit_diff_file: failed to create diff buf");
                    433:
                    434:        diff_format = D_RCSDIFF;
1.97      joris     435:        if (cvs_diffreg(p1, p2, b2) == D_ERROR)
1.55      joris     436:                fatal("commit_diff_file: failed to get RCS patch");
1.96      joris     437:
                    438:        xfree(p1);
                    439:        xfree(p2);
1.55      joris     440:
1.97      joris     441:        return (b2);
1.71      xsa       442: }
                    443:
                    444: static void
                    445: commit_desc_set(struct cvs_file *cf)
                    446: {
                    447:        BUF *bp;
1.102     xsa       448:        int fd;
1.100     otto      449:        char desc_path[MAXPATHLEN], *desc;
1.71      xsa       450:
1.102     xsa       451:        (void)xsnprintf(desc_path, MAXPATHLEN, "%s/%s%s",
1.71      xsa       452:            CVS_PATH_CVSDIR, cf->file_name, CVS_DESCR_FILE_EXT);
                    453:
1.100     otto      454:        if ((fd = open(desc_path, O_RDONLY)) == -1)
1.71      xsa       455:                return;
                    456:
1.78      joris     457:        bp = cvs_buf_load_fd(fd, BUF_AUTOEXT);
1.71      xsa       458:        cvs_buf_putc(bp, '\0');
                    459:        desc = cvs_buf_release(bp);
                    460:
                    461:        rcs_desc_set(cf->file_rcs, desc);
                    462:
1.76      joris     463:        (void)close(fd);
1.71      xsa       464:        (void)cvs_unlink(desc_path);
                    465:
                    466:        xfree(desc);
1.1       jfb       467: }