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

1.34    ! jfb         1: /*     $OpenBSD: commit.c,v 1.33 2005/05/20 20:00:53 joris Exp $       */
1.1       jfb         2: /*
                      3:  * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
1.9       tedu        4:  * All rights reserved.
1.1       jfb         5:  *
1.9       tedu        6:  * Redistribution and use in source and binary forms, with or without
                      7:  * modification, are permitted provided that the following conditions
                      8:  * are met:
1.1       jfb         9:  *
1.9       tedu       10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
1.1       jfb        12:  * 2. The name of the author may not be used to endorse or promote products
1.9       tedu       13:  *    derived from this software without specific prior written permission.
1.1       jfb        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
1.9       tedu       24:  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.1       jfb        25:  */
                     26:
                     27: #include <sys/types.h>
1.6       jfb        28: #include <sys/queue.h>
1.1       jfb        29: #include <sys/stat.h>
                     30:
                     31: #include <errno.h>
                     32: #include <stdio.h>
                     33: #include <fcntl.h>
                     34: #include <stdlib.h>
                     35: #include <unistd.h>
                     36: #include <string.h>
                     37:
                     38: #include "cvs.h"
                     39: #include "log.h"
1.5       krapht     40: #include "buf.h"
1.2       jfb        41: #include "proto.h"
1.1       jfb        42:
                     43:
1.34    ! jfb        44: static int cvs_commit_init    (struct cvs_cmd *, int, char **, int *);
        !            45: static int cvs_commit_prepare (CVSFILE *, void *);
        !            46: static int cvs_commit_file    (CVSFILE *, void *);
        !            47: static int cvs_commit_pre_exec(struct cvsroot *);
        !            48:
        !            49: struct cvs_cmd cvs_cmd_commit = {
        !            50:        CVS_OP_COMMIT, CVS_REQ_CI, "commit",
        !            51:        { "ci",  "com" },
        !            52:        "Check files into the repository",
        !            53:        "[-flR] [-F logfile | -m msg] [-r rev] ...",
        !            54:        "F:flm:Rr:",
1.18      joris      55:        NULL,
1.34    ! jfb        56:        CF_RECURSE | CF_IGNORE | CF_SORT,
        !            57:        cvs_commit_init,
        !            58:        cvs_commit_pre_exec,
1.18      joris      59:        cvs_commit_file,
                     60:        NULL,
1.34    ! jfb        61:        NULL,
        !            62:        NULL,
        !            63:        CVS_CMD_ALLOWSPEC | CVS_CMD_SENDARGS2
1.18      joris      64: };
1.1       jfb        65:
1.18      joris      66: static char *mfile = NULL;
1.32      joris      67: static char **commit_files = NULL;
                     68: static int commit_fcount = 0;
1.6       jfb        69:
1.34    ! jfb        70: static int
        !            71: cvs_commit_init(struct cvs_cmd *cmd, int argc, char **argv, int *arg)
1.1       jfb        72: {
1.18      joris      73:        int ch;
1.3       krapht     74:
1.34    ! jfb        75:        while ((ch = getopt(argc, argv, cmd->cmd_opts)) != -1) {
1.1       jfb        76:                switch (ch) {
                     77:                case 'F':
                     78:                        mfile = optarg;
                     79:                        break;
                     80:                case 'f':
1.10      jfb        81:                        /* XXX half-implemented */
1.34    ! jfb        82:                        cmd->file_flags &= ~CF_RECURSE;
1.1       jfb        83:                        break;
                     84:                case 'l':
1.34    ! jfb        85:                        cmd->file_flags &= ~CF_RECURSE;
1.1       jfb        86:                        break;
                     87:                case 'm':
1.14      jfb        88:                        cvs_msg = strdup(optarg);
                     89:                        if (cvs_msg == NULL) {
                     90:                                cvs_log(LP_ERRNO, "failed to copy message");
1.23      joris      91:                                return (CVS_EX_USAGE);
1.14      jfb        92:                        }
1.1       jfb        93:                        break;
                     94:                case 'R':
1.34    ! jfb        95:                        cmd->file_flags |= CF_RECURSE;
1.1       jfb        96:                        break;
                     97:                default:
1.23      joris      98:                        return (CVS_EX_USAGE);
1.1       jfb        99:                }
                    100:        }
                    101:
1.14      jfb       102:        if ((cvs_msg != NULL) && (mfile != NULL)) {
1.1       jfb       103:                cvs_log(LP_ERR, "the -F and -m flags are mutually exclusive");
1.23      joris     104:                return (CVS_EX_USAGE);
1.1       jfb       105:        }
                    106:
1.14      jfb       107:        if ((mfile != NULL) && (cvs_msg = cvs_logmsg_open(mfile)) == NULL)
1.23      joris     108:                return (CVS_EX_DATA);
1.1       jfb       109:
1.18      joris     110:        *arg = optind;
1.32      joris     111:
                    112:        commit_files = (argv + optind);
                    113:        commit_fcount = (argc - optind);
                    114:
1.18      joris     115:        return (0);
                    116: }
1.1       jfb       117:
1.18      joris     118: int
1.34    ! jfb       119: cvs_commit_pre_exec(struct cvsroot *root)
1.18      joris     120: {
                    121:        struct cvs_flist cl;
                    122:        CVSFILE *cfp;
1.32      joris     123:        CVSFILE *tmp;
                    124:        int flags = CF_RECURSE | CF_IGNORE | CF_SORT;
                    125:
                    126:        SIMPLEQ_INIT(&cl);
                    127:
                    128:        if (commit_fcount != 0) {
                    129:                tmp = cvs_file_getspec(commit_files, commit_fcount,
                    130:                    flags, cvs_commit_prepare, &cl);
                    131:        } else {
                    132:                tmp = cvs_file_get(".", flags, cvs_commit_prepare, &cl);
                    133:        }
1.1       jfb       134:
1.32      joris     135:        if (tmp == NULL)
                    136:                return (CVS_EX_DATA);
                    137:
                    138:        if (SIMPLEQ_EMPTY(&cl)) {
                    139:                cvs_file_free(tmp);
1.11      jfb       140:                return (0);
1.32      joris     141:        }
1.7       jfb       142:
1.17      joris     143:        if (cvs_msg == NULL)
1.32      joris     144:                cvs_msg = cvs_logmsg_get(CVS_FILE_NAME(tmp),
1.14      jfb       145:                    NULL, &cl, NULL);
1.32      joris     146:
                    147:        cvs_file_free(tmp);
1.17      joris     148:
1.26      jfb       149:        while (!SIMPLEQ_EMPTY(&cl)) {
                    150:                cfp = SIMPLEQ_FIRST(&cl);
                    151:                SIMPLEQ_REMOVE_HEAD(&cl, cf_list);
1.17      joris     152:                cvs_file_free(cfp);
1.7       jfb       153:        }
1.17      joris     154:
                    155:        if (cvs_msg == NULL)
1.23      joris     156:                return (CVS_EX_DATA);
1.7       jfb       157:
                    158:        return (0);
                    159: }
                    160:
                    161: /*
                    162:  * cvs_commit_prepare()
                    163:  *
                    164:  * Examine the file <cf> to see if it will be part of the commit, in which
                    165:  * case it gets added to the list passed as second argument.
                    166:  */
                    167: int
                    168: cvs_commit_prepare(CVSFILE *cf, void *arg)
                    169: {
                    170:        CVSFILE *copy;
                    171:        struct cvs_flist *clp = (struct cvs_flist *)arg;
                    172:
1.27      joris     173:        if ((cf->cf_type == DT_REG) && ((cf->cf_cvstat == CVS_FST_MODIFIED) ||
1.29      jfb       174:            (cf->cf_cvstat == CVS_FST_ADDED) ||
                    175:            (cf->cf_cvstat == CVS_FST_REMOVED))) {
1.7       jfb       176:                copy = cvs_file_copy(cf);
                    177:                if (copy == NULL)
1.23      joris     178:                        return (CVS_EX_DATA);
1.7       jfb       179:
1.26      jfb       180:                SIMPLEQ_INSERT_TAIL(clp, copy, cf_list);
1.7       jfb       181:        }
1.3       krapht    182:
1.6       jfb       183:        return (0);
1.3       krapht    184: }
                    185:
                    186:
                    187: /*
1.6       jfb       188:  * cvs_commit_file()
1.3       krapht    189:  *
1.6       jfb       190:  * Commit a single file.
1.3       krapht    191:  */
1.6       jfb       192: int
                    193: cvs_commit_file(CVSFILE *cf, void *arg)
1.3       krapht    194: {
1.24      xsa       195:        int ret, l;
1.6       jfb       196:        char *repo, rcspath[MAXPATHLEN], fpath[MAXPATHLEN];
                    197:        RCSFILE *rf;
                    198:        struct cvsroot *root;
                    199:
1.13      jfb       200:        ret = 0;
1.7       jfb       201:        rf = NULL;
                    202:        repo = NULL;
1.12      jfb       203:        root = CVS_DIR_ROOT(cf);
1.6       jfb       204:
                    205:        if (cf->cf_type == DT_DIR) {
1.13      jfb       206:                if (root->cr_method != CVS_METHOD_LOCAL) {
1.33      joris     207:                        if (cf->cf_cvstat != CVS_FST_UNKNOWN) {
                    208:                                if (cvs_senddir(root, cf) < 0)
                    209:                                        return (CVS_EX_PROTO);
                    210:                        }
1.6       jfb       211:                }
1.3       krapht    212:
1.33      joris     213:                return (0);
1.3       krapht    214:        }
                    215:
1.7       jfb       216:        cvs_file_getpath(cf, fpath, sizeof(fpath));
                    217:
                    218:        if (cf->cf_parent != NULL)
1.26      jfb       219:                repo = cf->cf_parent->cf_repo;
1.3       krapht    220:
1.6       jfb       221:        if ((cf->cf_cvstat == CVS_FST_ADDED) ||
1.29      jfb       222:            (cf->cf_cvstat == CVS_FST_MODIFIED) ||
                    223:            (cf->cf_cvstat == CVS_FST_REMOVED)) {
1.16      joris     224:                if (root->cr_method != CVS_METHOD_LOCAL) {
1.26      jfb       225:                        if (cvs_sendentry(root, cf) < 0) {
1.23      joris     226:                                return (CVS_EX_PROTO);
1.16      joris     227:                        }
1.31      joris     228:
                    229:                        /* if it's removed, don't bother sending a
                    230:                         * Modified request together with the file its
                    231:                         * contents.
                    232:                         */
                    233:                        if (cf->cf_cvstat == CVS_FST_REMOVED)
                    234:                                return (0);
1.16      joris     235:
                    236:                        if (cvs_sendreq(root, CVS_REQ_MODIFIED,
                    237:                            CVS_FILE_NAME(cf)) < 0) {
1.23      joris     238:                                return (CVS_EX_PROTO);
1.16      joris     239:                        }
                    240:
                    241:                        if (cvs_sendfile(root, fpath) < 0) {
1.23      joris     242:                                return (CVS_EX_PROTO);
1.16      joris     243:                        }
1.5       krapht    244:                }
                    245:        }
1.3       krapht    246:
1.24      xsa       247:        l = snprintf(rcspath, sizeof(rcspath), "%s/%s/%s%s",
1.6       jfb       248:            root->cr_dir, repo, fpath, RCS_FILE_EXT);
1.24      xsa       249:        if (l == -1 || l >= (int)sizeof(rcspath)) {
                    250:                errno = ENAMETOOLONG;
                    251:                cvs_log(LP_ERRNO, "%s", rcspath);
1.33      joris     252:                return (CVS_EX_DATA);
1.24      xsa       253:        }
1.3       krapht    254:
1.6       jfb       255:        return (0);
1.1       jfb       256: }