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

1.39    ! xsa         1: /*     $OpenBSD: commit.c,v 1.38 2005/07/07 14:27:57 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>
1.37      xsa        32: #include <fcntl.h>
1.1       jfb        33: #include <stdio.h>
                     34: #include <stdlib.h>
1.37      xsa        35: #include <string.h>
1.1       jfb        36: #include <unistd.h>
                     37:
1.37      xsa        38: #include "buf.h"
1.1       jfb        39: #include "cvs.h"
                     40: #include "log.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 *);
1.39    ! xsa        46: static int cvs_commit_remote  (CVSFILE *, void *);
        !            47: static int cvs_commit_local   (CVSFILE *, void *);
1.34      jfb        48: static int cvs_commit_pre_exec(struct cvsroot *);
                     49:
                     50: struct cvs_cmd cvs_cmd_commit = {
                     51:        CVS_OP_COMMIT, CVS_REQ_CI, "commit",
                     52:        { "ci",  "com" },
                     53:        "Check files into the repository",
                     54:        "[-flR] [-F logfile | -m msg] [-r rev] ...",
                     55:        "F:flm:Rr:",
1.18      joris      56:        NULL,
1.34      jfb        57:        CF_RECURSE | CF_IGNORE | CF_SORT,
                     58:        cvs_commit_init,
                     59:        cvs_commit_pre_exec,
1.39    ! xsa        60:        cvs_commit_remote,
        !            61:        cvs_commit_local,
1.34      jfb        62:        NULL,
                     63:        NULL,
                     64:        CVS_CMD_ALLOWSPEC | CVS_CMD_SENDARGS2
1.18      joris      65: };
1.1       jfb        66:
1.18      joris      67: static char *mfile = NULL;
1.35      xsa        68: static char *rev = NULL;
1.32      joris      69: static char **commit_files = NULL;
                     70: static int commit_fcount = 0;
1.6       jfb        71:
1.34      jfb        72: static int
                     73: cvs_commit_init(struct cvs_cmd *cmd, int argc, char **argv, int *arg)
1.1       jfb        74: {
1.18      joris      75:        int ch;
1.3       krapht     76:
1.34      jfb        77:        while ((ch = getopt(argc, argv, cmd->cmd_opts)) != -1) {
1.1       jfb        78:                switch (ch) {
                     79:                case 'F':
                     80:                        mfile = optarg;
                     81:                        break;
                     82:                case 'f':
1.10      jfb        83:                        /* XXX half-implemented */
1.34      jfb        84:                        cmd->file_flags &= ~CF_RECURSE;
1.1       jfb        85:                        break;
                     86:                case 'l':
1.34      jfb        87:                        cmd->file_flags &= ~CF_RECURSE;
1.1       jfb        88:                        break;
                     89:                case 'm':
1.14      jfb        90:                        cvs_msg = strdup(optarg);
                     91:                        if (cvs_msg == NULL) {
                     92:                                cvs_log(LP_ERRNO, "failed to copy message");
1.23      joris      93:                                return (CVS_EX_USAGE);
1.14      jfb        94:                        }
1.1       jfb        95:                        break;
                     96:                case 'R':
1.34      jfb        97:                        cmd->file_flags |= CF_RECURSE;
1.1       jfb        98:                        break;
1.35      xsa        99:                case 'r':
                    100:                        rev = optarg;
                    101:                        break;
1.1       jfb       102:                default:
1.23      joris     103:                        return (CVS_EX_USAGE);
1.1       jfb       104:                }
                    105:        }
                    106:
1.14      jfb       107:        if ((cvs_msg != NULL) && (mfile != NULL)) {
1.1       jfb       108:                cvs_log(LP_ERR, "the -F and -m flags are mutually exclusive");
1.23      joris     109:                return (CVS_EX_USAGE);
1.1       jfb       110:        }
                    111:
1.14      jfb       112:        if ((mfile != NULL) && (cvs_msg = cvs_logmsg_open(mfile)) == NULL)
1.23      joris     113:                return (CVS_EX_DATA);
1.1       jfb       114:
1.18      joris     115:        *arg = optind;
1.32      joris     116:
                    117:        commit_files = (argv + optind);
                    118:        commit_fcount = (argc - optind);
                    119:
1.18      joris     120:        return (0);
                    121: }
1.1       jfb       122:
1.18      joris     123: int
1.34      jfb       124: cvs_commit_pre_exec(struct cvsroot *root)
1.18      joris     125: {
                    126:        struct cvs_flist cl;
                    127:        CVSFILE *cfp;
1.32      joris     128:        CVSFILE *tmp;
                    129:        int flags = CF_RECURSE | CF_IGNORE | CF_SORT;
1.38      joris     130:
1.32      joris     131:        SIMPLEQ_INIT(&cl);
                    132:
                    133:        if (commit_fcount != 0) {
                    134:                tmp = cvs_file_getspec(commit_files, commit_fcount,
                    135:                    flags, cvs_commit_prepare, &cl);
                    136:        } else {
                    137:                tmp = cvs_file_get(".", flags, cvs_commit_prepare, &cl);
                    138:        }
1.1       jfb       139:
1.32      joris     140:        if (tmp == NULL)
                    141:                return (CVS_EX_DATA);
                    142:
                    143:        if (SIMPLEQ_EMPTY(&cl)) {
                    144:                cvs_file_free(tmp);
1.11      jfb       145:                return (0);
1.32      joris     146:        }
1.7       jfb       147:
1.17      joris     148:        if (cvs_msg == NULL)
1.32      joris     149:                cvs_msg = cvs_logmsg_get(CVS_FILE_NAME(tmp),
1.14      jfb       150:                    NULL, &cl, NULL);
1.32      joris     151:
                    152:        cvs_file_free(tmp);
1.17      joris     153:
1.26      jfb       154:        while (!SIMPLEQ_EMPTY(&cl)) {
                    155:                cfp = SIMPLEQ_FIRST(&cl);
                    156:                SIMPLEQ_REMOVE_HEAD(&cl, cf_list);
1.17      joris     157:                cvs_file_free(cfp);
1.7       jfb       158:        }
1.17      joris     159:
                    160:        if (cvs_msg == NULL)
1.23      joris     161:                return (CVS_EX_DATA);
1.35      xsa       162:
                    163:        if (root->cr_method != CVS_METHOD_LOCAL) {
1.36      joris     164:                if (cvs_logmsg_send(root, cvs_msg) < 0)
                    165:                        return (CVS_EX_PROTO);
                    166:
1.35      xsa       167:                if (rev != NULL) {
                    168:                        if ((cvs_sendarg(root, "-r", 0) < 0) ||
                    169:                            (cvs_sendarg(root, rev, 0) < 0))
                    170:                                return (CVS_EX_PROTO);
                    171:                }
                    172:        }
1.7       jfb       173:
                    174:        return (0);
                    175: }
                    176:
                    177: /*
                    178:  * cvs_commit_prepare()
                    179:  *
                    180:  * Examine the file <cf> to see if it will be part of the commit, in which
                    181:  * case it gets added to the list passed as second argument.
                    182:  */
                    183: int
                    184: cvs_commit_prepare(CVSFILE *cf, void *arg)
                    185: {
                    186:        CVSFILE *copy;
                    187:        struct cvs_flist *clp = (struct cvs_flist *)arg;
                    188:
1.27      joris     189:        if ((cf->cf_type == DT_REG) && ((cf->cf_cvstat == CVS_FST_MODIFIED) ||
1.29      jfb       190:            (cf->cf_cvstat == CVS_FST_ADDED) ||
                    191:            (cf->cf_cvstat == CVS_FST_REMOVED))) {
1.7       jfb       192:                copy = cvs_file_copy(cf);
                    193:                if (copy == NULL)
1.23      joris     194:                        return (CVS_EX_DATA);
1.7       jfb       195:
1.26      jfb       196:                SIMPLEQ_INSERT_TAIL(clp, copy, cf_list);
1.7       jfb       197:        }
1.3       krapht    198:
1.6       jfb       199:        return (0);
1.3       krapht    200: }
                    201:
                    202:
                    203: /*
1.39    ! xsa       204:  * cvs_commit_remote()
1.3       krapht    205:  *
1.6       jfb       206:  * Commit a single file.
1.3       krapht    207:  */
1.6       jfb       208: int
1.39    ! xsa       209: cvs_commit_remote(CVSFILE *cf, void *arg)
1.3       krapht    210: {
1.39    ! xsa       211:        int ret;
        !           212:        char *repo, fpath[MAXPATHLEN];
1.6       jfb       213:        RCSFILE *rf;
                    214:        struct cvsroot *root;
                    215:
1.13      jfb       216:        ret = 0;
1.7       jfb       217:        rf = NULL;
                    218:        repo = NULL;
1.12      jfb       219:        root = CVS_DIR_ROOT(cf);
1.6       jfb       220:
                    221:        if (cf->cf_type == DT_DIR) {
1.39    ! xsa       222:                if (cf->cf_cvstat != CVS_FST_UNKNOWN) {
        !           223:                        if (cvs_senddir(root, cf) < 0)
        !           224:                                return (CVS_EX_PROTO);
1.6       jfb       225:                }
1.33      joris     226:                return (0);
1.3       krapht    227:        }
                    228:
1.7       jfb       229:        cvs_file_getpath(cf, fpath, sizeof(fpath));
                    230:
                    231:        if (cf->cf_parent != NULL)
1.26      jfb       232:                repo = cf->cf_parent->cf_repo;
1.3       krapht    233:
1.6       jfb       234:        if ((cf->cf_cvstat == CVS_FST_ADDED) ||
1.29      jfb       235:            (cf->cf_cvstat == CVS_FST_MODIFIED) ||
                    236:            (cf->cf_cvstat == CVS_FST_REMOVED)) {
1.39    ! xsa       237:                if (cvs_sendentry(root, cf) < 0) {
        !           238:                        return (CVS_EX_PROTO);
        !           239:                }
1.31      joris     240:
1.39    ! xsa       241:                /* if it's removed, don't bother sending a
        !           242:                 * Modified request together with the file its
        !           243:                 * contents.
        !           244:                 */
        !           245:                if (cf->cf_cvstat == CVS_FST_REMOVED)
        !           246:                        return (0);
1.16      joris     247:
1.39    ! xsa       248:                if (cvs_sendreq(root, CVS_REQ_MODIFIED,
        !           249:                    CVS_FILE_NAME(cf)) < 0) {
        !           250:                        return (CVS_EX_PROTO);
        !           251:                }
1.16      joris     252:
1.39    ! xsa       253:                if (cvs_sendfile(root, fpath) < 0) {
        !           254:                        return (CVS_EX_PROTO);
1.5       krapht    255:                }
                    256:        }
1.3       krapht    257:
1.39    ! xsa       258:        return (0);
        !           259: }
        !           260:
        !           261: static int
        !           262: cvs_commit_local(CVSFILE *cf, void *arg)
        !           263: {
        !           264:        int len;
        !           265:        char fpath[MAXPATHLEN], rcspath[MAXPATHLEN];
        !           266:        char *repo;
        !           267:        struct cvsroot *root;
        !           268:
        !           269:        if (cf->cf_type == DT_DIR) {
        !           270:                if (verbosity > 1)
        !           271:                        cvs_log(LP_INFO, "Examining %s", cf->cf_name);
        !           272:                return (0);
        !           273:        }
        !           274:
        !           275:        root = CVS_DIR_ROOT(cf);
        !           276:        repo = CVS_DIR_REPO(cf);
        !           277:
        !           278:        cvs_file_getpath(cf, fpath, sizeof(fpath));
        !           279:
        !           280:        len = snprintf(rcspath, sizeof(rcspath), "%s/%s/%s%s",
1.6       jfb       281:            root->cr_dir, repo, fpath, RCS_FILE_EXT);
1.39    ! xsa       282:        if (len == -1 || len >= (int)sizeof(rcspath)) {
1.24      xsa       283:                errno = ENAMETOOLONG;
                    284:                cvs_log(LP_ERRNO, "%s", rcspath);
1.33      joris     285:                return (CVS_EX_DATA);
1.24      xsa       286:        }
1.3       krapht    287:
1.6       jfb       288:        return (0);
1.1       jfb       289: }