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

1.57    ! joris       1: /*     $OpenBSD: commit.c,v 1.56 2006/05/27 06:16:14 joris Exp $       */
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++;
1.57    ! joris     117:
        !           118:        if (cf->file_status == FILE_MERGE ||
        !           119:            cf->file_status == FILE_PATCH) {
        !           120:                cvs_log(LP_ERR, "conflict: %s is not up-to-date",
        !           121:                    cf->file_path);
        !           122:                conflicts_found++;
        !           123:        }
1.55      joris     124:
                    125:        if (cf->file_status == FILE_ADDED ||
                    126:            cf->file_status == FILE_REMOVED ||
                    127:            cf->file_status == FILE_MODIFIED)
                    128:                cvs_file_get(cf->file_path, &files_affected);
                    129: }
1.1       jfb       130:
1.55      joris     131: void
                    132: cvs_commit_local(struct cvs_file *cf)
                    133: {
                    134:        BUF *b;
                    135:        char *d, *f, rbuf[16];
1.56      joris     136:        CVSENTRIES *entlist;
1.17      joris     137:
1.55      joris     138:        cvs_log(LP_TRACE, "cvs_commit_local(%s)", cf->file_path);
                    139:        cvs_file_classify(cf);
1.35      xsa       140:
1.56      joris     141:        if (cf->file_status == FILE_MODIFIED ||
                    142:            cf->file_status == FILE_REMOVED)
                    143:                rcsnum_tostr(cf->file_rcs->rf_head, rbuf, sizeof(rbuf));
1.36      joris     144:
1.55      joris     145:        cvs_printf("Checking in %s:\n", cf->file_path);
                    146:        cvs_printf("%s <- %s\n", cf->file_rpath, cf->file_path);
                    147:        cvs_printf("old revision: %s; ", rbuf);
1.7       jfb       148:
1.55      joris     149:        d = commit_diff_file(cf);
1.7       jfb       150:
1.56      joris     151:        if (cf->file_status == FILE_REMOVED) {
                    152:                b = rcs_getrev(cf->file_rcs, cf->file_rcs->rf_head);
                    153:                if (b == NULL)
                    154:                        fatal("cvs_commit_local: failed to get HEAD");
                    155:        } else if (cf->file_status == FILE_MODIFIED) {
                    156:                if ((b = cvs_buf_load(cf->file_path, BUF_AUTOEXT)) == NULL)
                    157:                        fatal("cvs_commit_local: failed to load file");
                    158:        }
1.7       jfb       159:
1.55      joris     160:        cvs_buf_putc(b, '\0');
                    161:        f = cvs_buf_release(b);
1.7       jfb       162:
1.55      joris     163:        if (rcs_deltatext_set(cf->file_rcs, cf->file_rcs->rf_head, d) == -1)
                    164:                fatal("cvs_commit_local: failed to set delta");
1.3       krapht    165:
1.55      joris     166:        if (rcs_rev_add(cf->file_rcs, RCS_HEAD_REV, logmsg, -1, NULL) == -1)
                    167:                fatal("cvs_commit_local: failed to add new revision");
1.3       krapht    168:
1.55      joris     169:        if (rcs_deltatext_set(cf->file_rcs, cf->file_rcs->rf_head, f) == -1)
                    170:                fatal("cvs_commit_local: failed to set new HEAD delta");
1.3       krapht    171:
1.55      joris     172:        xfree(f);
                    173:        xfree(d);
1.6       jfb       174:
1.56      joris     175:        if (cf->file_status == FILE_REMOVED) {
                    176:                if (rcs_state_set(cf->file_rcs,
                    177:                    cf->file_rcs->rf_head, RCS_STATE_DEAD) == -1)
                    178:                        fatal("cvs_commit_local: failed to set state");
                    179:        }
                    180:
1.55      joris     181:        rcs_write(cf->file_rcs);
1.6       jfb       182:
1.56      joris     183:        if (cf->file_status == FILE_REMOVED) {
                    184:                strlcpy(rbuf, "Removed", sizeof(rbuf));
                    185:        } else if (cf->file_status == FILE_MODIFIED) {
                    186:                rcsnum_tostr(cf->file_rcs->rf_head, rbuf, sizeof(rbuf));
                    187:        }
                    188:
1.55      joris     189:        cvs_printf("new revision: %s\n", rbuf);
1.3       krapht    190:
1.55      joris     191:        (void)unlink(cf->file_path);
                    192:        (void)close(cf->fd);
                    193:        cf->fd = -1;
1.56      joris     194:
                    195:        if (cf->file_status != FILE_REMOVED) {
                    196:                cvs_checkout_file(cf, cf->file_rcs->rf_head, 0);
                    197:        } else {
                    198:                entlist = cvs_ent_open(cf->file_wd);
                    199:                cvs_ent_remove(entlist, cf->file_name);
                    200:                cvs_ent_close(entlist, ENT_SYNC);
                    201:        }
1.3       krapht    202:
1.55      joris     203:        cvs_printf("done\n");
1.16      joris     204:
1.39      xsa       205: }
                    206:
1.55      joris     207: static char *
                    208: commit_diff_file(struct cvs_file *cf)
1.39      xsa       209: {
1.55      joris     210:        char*delta,  *p1, *p2;
                    211:        BUF *b1, *b2, *b3;
1.39      xsa       212:
1.56      joris     213:        if (cf->file_status == FILE_MODIFIED) {
                    214:                if ((b1 = cvs_buf_load(cf->file_path, BUF_AUTOEXT)) == NULL)
                    215:                        fatal("commit_diff_file: failed to load '%s'",
                    216:                            cf->file_path);
                    217:        } else if (cf->file_status == FILE_REMOVED) {
                    218:                b1 = rcs_getrev(cf->file_rcs, cf->file_rcs->rf_head);
                    219:                if (b1 == NULL)
                    220:                        fatal("commit_diff_file: failed to load HEAD");
                    221:                b1 = rcs_kwexp_buf(b1, cf->file_rcs, cf->file_rcs->rf_head);
                    222:        }
1.3       krapht    223:
1.55      joris     224:        if ((b2 = rcs_getrev(cf->file_rcs, cf->file_rcs->rf_head)) == NULL)
                    225:                fatal("commit_diff_file: failed to load HEAD for '%s'",
                    226:                    cf->file_path);
                    227:
                    228:        if ((b3 = cvs_buf_alloc(128, BUF_AUTOEXT)) == NULL)
                    229:                fatal("commit_diff_file: failed to create diff buf");
                    230:
                    231:        (void)xasprintf(&p1, "%s/diff1.XXXXXXXXXX", cvs_tmpdir);
                    232:        cvs_buf_write_stmp(b1, p1, 0600, NULL);
                    233:        cvs_buf_free(b1);
                    234:
                    235:        (void)xasprintf(&p2, "%s/diff2.XXXXXXXXXX", cvs_tmpdir);
                    236:        cvs_buf_write_stmp(b2, p2, 0600, NULL);
                    237:        cvs_buf_free(b2);
                    238:
                    239:        diff_format = D_RCSDIFF;
                    240:        if (cvs_diffreg(p1, p2, b3) == D_ERROR)
                    241:                fatal("commit_diff_file: failed to get RCS patch");
                    242:
                    243:        cvs_buf_putc(b3, '\0');
                    244:        delta = cvs_buf_release(b3);
                    245:        return (delta);
1.1       jfb       246: }