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

1.8     ! deraadt     1: /*     $OpenBSD: commit.c,v 1.7 2004/12/02 19:23:44 jfb Exp $  */
1.1       jfb         2: /*
                      3:  * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
                      4:  * All rights reserved.
                      5:  *
                      6:  * Redistribution and use in source and binary forms, with or without
                      7:  * modification, are permitted provided that the following conditions
                      8:  * are met:
                      9:  *
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. The name of the author may not be used to endorse or promote products
                     13:  *    derived from this software without specific prior written permission.
                     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
                     24:  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     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: #include <sysexits.h>
                     38:
                     39: #include "cvs.h"
                     40: #include "log.h"
1.5       krapht     41: #include "buf.h"
1.2       jfb        42: #include "proto.h"
1.1       jfb        43:
                     44:
                     45:
1.6       jfb        46:
1.7       jfb        47: int    cvs_commit_prepare  (CVSFILE *, void *);
1.6       jfb        48: int    cvs_commit_file     (CVSFILE *, void *);
1.1       jfb        49:
                     50:
                     51: /*
                     52:  * cvs_commit()
                     53:  *
                     54:  * Handler for the `cvs commit' command.
                     55:  */
                     56:
                     57: int
                     58: cvs_commit(int argc, char **argv)
                     59: {
1.7       jfb        60:        int i, ch, recurse, flags;
1.1       jfb        61:        char *msg, *mfile;
1.7       jfb        62:        struct cvs_flist cl;
                     63:        struct cvsroot *root;
1.1       jfb        64:
1.7       jfb        65:        flags = CF_RECURSE|CF_IGNORE|CF_SORT;
1.1       jfb        66:        recurse = 1;
                     67:        mfile = NULL;
                     68:        msg = NULL;
1.7       jfb        69:        TAILQ_INIT(&cl);
1.3       krapht     70:
1.1       jfb        71:        while ((ch = getopt(argc, argv, "F:flm:R")) != -1) {
                     72:                switch (ch) {
                     73:                case 'F':
                     74:                        mfile = optarg;
                     75:                        break;
                     76:                case 'f':
                     77:                        recurse = 0;
                     78:                        break;
                     79:                case 'l':
                     80:                        recurse = 0;
                     81:                        break;
                     82:                case 'm':
                     83:                        msg = optarg;
                     84:                        break;
                     85:                case 'R':
                     86:                        recurse = 1;
                     87:                        break;
                     88:                default:
                     89:                        return (EX_USAGE);
                     90:                }
                     91:        }
                     92:
                     93:        if ((msg != NULL) && (mfile != NULL)) {
                     94:                cvs_log(LP_ERR, "the -F and -m flags are mutually exclusive");
                     95:                return (EX_USAGE);
                     96:        }
                     97:
1.6       jfb        98:        if ((mfile != NULL) && (msg = cvs_logmsg_open(mfile)) == NULL)
1.1       jfb        99:                return (EX_DATAERR);
                    100:
                    101:        argc -= optind;
                    102:        argv += optind;
                    103:
1.7       jfb       104:        if (argc == 0) {
1.6       jfb       105:                cvs_files = cvs_file_get(".", flags);
1.8     ! deraadt   106:        } else {
1.6       jfb       107:                cvs_files = cvs_file_getspec(argv, argc, flags);
1.1       jfb       108:        }
1.6       jfb       109:        if (cvs_files == NULL)
                    110:                return (EX_DATAERR);
1.1       jfb       111:
1.7       jfb       112:        cvs_file_examine(cvs_files, cvs_commit_prepare, &cl);
                    113:
                    114:        if (msg == NULL) {
                    115:                msg = cvs_logmsg_get(CVS_FILE_NAME(cvs_files), &cl);
                    116:                if (msg == NULL)
                    117:                        return (1);
                    118:        }
                    119:
                    120:        root = CVS_DIR_ROOT(cvs_files);
                    121:        cvs_connect(root);
                    122:        cvs_logmsg_send(root, msg);
                    123:
1.6       jfb       124:        cvs_file_examine(cvs_files, cvs_commit_file, &cl);
1.3       krapht    125:
1.7       jfb       126:        if (root->cr_method != CVS_METHOD_LOCAL) {
                    127:                cvs_senddir(root, cvs_files);
                    128:                if (argc > 0) {
                    129:                        for (i = 0; i < argc; i++)
                    130:                                cvs_sendarg(root, argv[i], 0);
                    131:                }
                    132:                cvs_sendreq(root, CVS_REQ_CI, NULL);
                    133:        }
                    134:
                    135:        return (0);
                    136: }
                    137:
                    138:
                    139: /*
                    140:  * cvs_commit_prepare()
                    141:  *
                    142:  * Examine the file <cf> to see if it will be part of the commit, in which
                    143:  * case it gets added to the list passed as second argument.
                    144:  */
                    145:
                    146: int
                    147: cvs_commit_prepare(CVSFILE *cf, void *arg)
                    148: {
                    149:        CVSFILE *copy;
                    150:        struct cvs_flist *clp = (struct cvs_flist *)arg;
                    151:
                    152:        if ((cf->cf_type == DT_REG) && (cf->cf_cvstat == CVS_FST_MODIFIED)) {
                    153:                copy = cvs_file_copy(cf);
                    154:                if (copy == NULL)
                    155:                        return (-1);
                    156:
                    157:                TAILQ_INSERT_TAIL(clp, copy, cf_list);
                    158:        }
1.3       krapht    159:
1.6       jfb       160:        return (0);
1.3       krapht    161: }
                    162:
                    163:
                    164: /*
1.6       jfb       165:  * cvs_commit_file()
1.3       krapht    166:  *
1.6       jfb       167:  * Commit a single file.
1.3       krapht    168:  */
                    169:
1.6       jfb       170: int
                    171: cvs_commit_file(CVSFILE *cf, void *arg)
1.3       krapht    172: {
1.6       jfb       173:        char *repo, rcspath[MAXPATHLEN], fpath[MAXPATHLEN];
                    174:        RCSFILE *rf;
                    175:        struct cvsroot *root;
                    176:        struct cvs_ent *entp;
                    177:
1.7       jfb       178:        rf = NULL;
                    179:        repo = NULL;
1.6       jfb       180:
                    181:        if (cf->cf_type == DT_DIR) {
                    182:                if (cf->cf_cvstat != CVS_FST_UNKNOWN) {
1.7       jfb       183:                        root = CVS_DIR_ROOT(cf);
                    184:                        if ((cf->cf_parent != NULL) &&
1.6       jfb       185:                            (root != cf->cf_parent->cf_ddat->cd_root)) {
                    186:                                cvs_connect(root);
                    187:                        }
                    188:
                    189:                        cvs_senddir(root, cf);
                    190:                }
1.3       krapht    191:
1.6       jfb       192:                return (0);
1.3       krapht    193:        }
                    194:
1.7       jfb       195:
                    196:        root = CVS_DIR_ROOT(cf);
                    197:        cvs_file_getpath(cf, fpath, sizeof(fpath));
                    198:
                    199:        if (cf->cf_parent != NULL)
1.6       jfb       200:                repo = cf->cf_parent->cf_ddat->cd_repo;
1.5       krapht    201:
1.6       jfb       202:        entp = cvs_ent_getent(fpath);
                    203:        if (entp == NULL)
                    204:                return (-1);
1.3       krapht    205:
1.6       jfb       206:        if ((cf->cf_cvstat == CVS_FST_ADDED) ||
                    207:            (cf->cf_cvstat == CVS_FST_MODIFIED)) {
                    208:                if ((root->cr_method != CVS_METHOD_LOCAL) &&
                    209:                    (cvs_sendentry(root, entp) < 0)) {
                    210:                        cvs_ent_free(entp);
                    211:                        return (-1);
1.5       krapht    212:                }
1.3       krapht    213:
1.6       jfb       214:                cvs_sendreq(root, CVS_REQ_MODIFIED, CVS_FILE_NAME(cf));
                    215:                cvs_sendfile(root, fpath);
1.5       krapht    216:        }
1.3       krapht    217:
1.6       jfb       218:        snprintf(rcspath, sizeof(rcspath), "%s/%s/%s%s",
                    219:            root->cr_dir, repo, fpath, RCS_FILE_EXT);
1.3       krapht    220:
1.6       jfb       221:        cvs_ent_free(entp);
1.3       krapht    222:
1.6       jfb       223:        return (0);
1.1       jfb       224: }