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

1.55    ! joris       1: /*     $OpenBSD$       */
1.1       jfb         2: /*
1.55    ! joris       3:  * Copyright (c) 2006 Joris Vink <joris@openbsd.org>
1.1       jfb         4:  *
1.55    ! joris       5:  * Permission to use, copy, modify, and distribute this software for any
        !             6:  * purpose with or without fee is hereby granted, provided that the above
        !             7:  * copyright notice and this permission notice appear in all copies.
1.1       jfb         8:  *
1.55    ! joris       9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
        !            10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
        !            11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
        !            12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
        !            13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
        !            14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
        !            15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1       jfb        16:  */
                     17:
1.51      xsa        18: #include "includes.h"
1.1       jfb        19:
                     20: #include "cvs.h"
1.55    ! joris      21: #include "diff.h"
1.1       jfb        22: #include "log.h"
1.2       jfb        23: #include "proto.h"
1.1       jfb        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 *);
        !            30:
        !            31: struct cvs_flisthead files_affected;
        !            32: int    conflicts_found;
        !            33: char   *logmsg;
1.34      jfb        34:
                     35: struct cvs_cmd cvs_cmd_commit = {
                     36:        CVS_OP_COMMIT, CVS_REQ_CI, "commit",
1.55    ! joris      37:        { "ci", "com" },
1.34      jfb        38:        "Check files into the repository",
                     39:        "[-flR] [-F logfile | -m msg] [-r rev] ...",
                     40:        "F:flm:Rr:",
1.18      joris      41:        NULL,
1.55    ! joris      42:        cvs_commit
1.18      joris      43: };
1.1       jfb        44:
1.55    ! joris      45: int
        !            46: cvs_commit(int argc, char **argv)
1.1       jfb        47: {
1.18      joris      48:        int ch;
1.55    ! joris      49:        char *arg = ".";
        !            50:        struct cvs_recursion cr;
1.3       krapht     51:
1.55    ! joris      52:        while ((ch = getopt(argc, argv, cvs_cmd_commit.cmd_opts)) != -1) {
1.1       jfb        53:                switch (ch) {
1.55    ! joris      54:                case 'f':
        !            55:                        break;
1.1       jfb        56:                case 'F':
                     57:                        break;
                     58:                case 'l':
                     59:                        break;
                     60:                case 'm':
1.55    ! joris      61:                        logmsg = xstrdup(optarg);
        !            62:                        break;
        !            63:                case 'r':
1.1       jfb        64:                        break;
                     65:                case 'R':
1.35      xsa        66:                        break;
1.1       jfb        67:                default:
1.55    ! joris      68:                        fatal("%s", cvs_cmd_commit.cmd_synopsis);
1.1       jfb        69:                }
                     70:        }
                     71:
1.55    ! joris      72:        argc -= optind;
        !            73:        argv += optind;
1.1       jfb        74:
1.55    ! joris      75:        if (logmsg == NULL)
        !            76:                fatal("please use -m to specify a log message for now");
1.1       jfb        77:
1.55    ! joris      78:        TAILQ_INIT(&files_affected);
        !            79:        conflicts_found = 0;
1.32      joris      80:
1.55    ! joris      81:        cr.enterdir = NULL;
        !            82:        cr.leavedir = NULL;
        !            83:        cr.local = cvs_commit_check_conflicts;
        !            84:        cr.remote = NULL;
        !            85:
        !            86:        if (argc > 0)
        !            87:                cvs_file_run(argc, argv, &cr);
        !            88:        else
        !            89:                cvs_file_run(1, &arg, &cr);
        !            90:
        !            91:        if (conflicts_found != 0)
        !            92:                fatal("%d conflicts found, please correct these first",
        !            93:                    conflicts_found);
        !            94:
        !            95:        cr.local = cvs_commit_local;
        !            96:        cvs_file_walklist(&files_affected, &cr);
        !            97:        cvs_file_freelist(&files_affected);
1.32      joris      98:
1.18      joris      99:        return (0);
                    100: }
1.1       jfb       101:
1.55    ! joris     102: void
        !           103: cvs_commit_check_conflicts(struct cvs_file *cf)
1.18      joris     104: {
1.55    ! joris     105:        cvs_log(LP_TRACE, "cvs_commit_check_conflicts(%s)", cf->file_path);
1.44      joris     106:
1.43      joris     107:        /*
1.55    ! joris     108:         * cvs_file_classify makes the noise for us
        !           109:         * XXX - we want that?
1.43      joris     110:         */
1.55    ! joris     111:        cvs_file_classify(cf);
1.32      joris     112:
1.55    ! joris     113:        if (cf->file_status == FILE_CONFLICT ||
        !           114:            cf->file_status == FILE_LOST ||
        !           115:            cf->file_status == FILE_UNLINK)
        !           116:                conflicts_found++;
        !           117:
        !           118:        if (cf->file_status == FILE_ADDED ||
        !           119:            cf->file_status == FILE_REMOVED ||
        !           120:            cf->file_status == FILE_MODIFIED)
        !           121:                cvs_file_get(cf->file_path, &files_affected);
        !           122: }
1.1       jfb       123:
1.55    ! joris     124: void
        !           125: cvs_commit_local(struct cvs_file *cf)
        !           126: {
        !           127:        BUF *b;
        !           128:        char *d, *f, rbuf[16];
1.17      joris     129:
1.55    ! joris     130:        cvs_log(LP_TRACE, "cvs_commit_local(%s)", cf->file_path);
        !           131:        cvs_file_classify(cf);
1.35      xsa       132:
1.55    ! joris     133:        rcsnum_tostr(cf->file_rcs->rf_head, rbuf, sizeof(rbuf));
1.36      joris     134:
1.55    ! joris     135:        cvs_printf("Checking in %s:\n", cf->file_path);
        !           136:        cvs_printf("%s <- %s\n", cf->file_rpath, cf->file_path);
        !           137:        cvs_printf("old revision: %s; ", rbuf);
1.7       jfb       138:
1.55    ! joris     139:        d = commit_diff_file(cf);
1.7       jfb       140:
1.55    ! joris     141:        if ((b = cvs_buf_load(cf->file_path, BUF_AUTOEXT)) == NULL)
        !           142:                fatal("cvs_commit_local: failed to load file");
1.7       jfb       143:
1.55    ! joris     144:        cvs_buf_putc(b, '\0');
        !           145:        f = cvs_buf_release(b);
1.7       jfb       146:
1.55    ! joris     147:        if (rcs_deltatext_set(cf->file_rcs, cf->file_rcs->rf_head, d) == -1)
        !           148:                fatal("cvs_commit_local: failed to set delta");
1.3       krapht    149:
1.55    ! joris     150:        if (rcs_rev_add(cf->file_rcs, RCS_HEAD_REV, logmsg, -1, NULL) == -1)
        !           151:                fatal("cvs_commit_local: failed to add new revision");
1.3       krapht    152:
1.55    ! joris     153:        if (rcs_deltatext_set(cf->file_rcs, cf->file_rcs->rf_head, f) == -1)
        !           154:                fatal("cvs_commit_local: failed to set new HEAD delta");
1.3       krapht    155:
1.55    ! joris     156:        xfree(f);
        !           157:        xfree(d);
1.6       jfb       158:
1.55    ! joris     159:        rcs_write(cf->file_rcs);
1.6       jfb       160:
1.55    ! joris     161:        rcsnum_tostr(cf->file_rcs->rf_head, rbuf, sizeof(rbuf));
        !           162:        cvs_printf("new revision: %s\n", rbuf);
1.3       krapht    163:
1.55    ! joris     164:        (void)unlink(cf->file_path);
        !           165:        (void)close(cf->fd);
        !           166:        cf->fd = -1;
        !           167:        cvs_checkout_file(cf, cf->file_rcs->rf_head, 0);
1.3       krapht    168:
1.55    ! joris     169:        cvs_printf("done\n");
1.16      joris     170:
1.39      xsa       171: }
                    172:
1.55    ! joris     173: static char *
        !           174: commit_diff_file(struct cvs_file *cf)
1.39      xsa       175: {
1.55    ! joris     176:        char*delta,  *p1, *p2;
        !           177:        BUF *b1, *b2, *b3;
1.39      xsa       178:
1.55    ! joris     179:        if ((b1 = cvs_buf_load(cf->file_path, BUF_AUTOEXT)) == NULL)
        !           180:                fatal("commit_diff_file: failed to load '%s'", cf->file_path);
1.3       krapht    181:
1.55    ! joris     182:        if ((b2 = rcs_getrev(cf->file_rcs, cf->file_rcs->rf_head)) == NULL)
        !           183:                fatal("commit_diff_file: failed to load HEAD for '%s'",
        !           184:                    cf->file_path);
        !           185:
        !           186:        if ((b3 = cvs_buf_alloc(128, BUF_AUTOEXT)) == NULL)
        !           187:                fatal("commit_diff_file: failed to create diff buf");
        !           188:
        !           189:        (void)xasprintf(&p1, "%s/diff1.XXXXXXXXXX", cvs_tmpdir);
        !           190:        cvs_buf_write_stmp(b1, p1, 0600, NULL);
        !           191:        cvs_buf_free(b1);
        !           192:
        !           193:        (void)xasprintf(&p2, "%s/diff2.XXXXXXXXXX", cvs_tmpdir);
        !           194:        cvs_buf_write_stmp(b2, p2, 0600, NULL);
        !           195:        cvs_buf_free(b2);
        !           196:
        !           197:        diff_format = D_RCSDIFF;
        !           198:        if (cvs_diffreg(p1, p2, b3) == D_ERROR)
        !           199:                fatal("commit_diff_file: failed to get RCS patch");
        !           200:
        !           201:        cvs_buf_putc(b3, '\0');
        !           202:        delta = cvs_buf_release(b3);
        !           203:        return (delta);
1.1       jfb       204: }