Annotation of src/usr.bin/cvs/commit.c, Revision 1.30
1.30 ! joris 1: /* $OpenBSD: commit.c,v 1.29 2005/04/19 02:04:56 jfb 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.19 joris 44: int cvs_commit_prepare(CVSFILE *, void *);
1.18 joris 45: int cvs_commit_file(CVSFILE *, void *);
46: int cvs_commit_options(char *, int, char **, int *);
47: int cvs_commit_helper(void);
48:
49: struct cvs_cmd_info cvs_commit = {
50: cvs_commit_options,
51: NULL,
52: cvs_commit_file,
53: NULL,
54: cvs_commit_helper,
55: CF_RECURSE | CF_IGNORE | CF_SORT,
56: CVS_REQ_CI,
1.30 ! joris 57: CVS_CMD_ALLOWSPEC | CVS_CMD_NEEDLOG | CVS_CMD_SENDDIR | CVS_CMD_SENDARGS2
1.18 joris 58: };
1.1 jfb 59:
1.18 joris 60: static char *mfile = NULL;
1.6 jfb 61:
1.1 jfb 62: int
1.18 joris 63: cvs_commit_options(char *opt, int argc, char **argv, int *arg)
1.1 jfb 64: {
1.18 joris 65: int ch;
1.3 krapht 66:
1.18 joris 67: while ((ch = getopt(argc, argv, opt)) != -1) {
1.1 jfb 68: switch (ch) {
69: case 'F':
70: mfile = optarg;
71: break;
72: case 'f':
1.10 jfb 73: /* XXX half-implemented */
1.18 joris 74: cvs_commit.file_flags &= ~CF_RECURSE;
1.1 jfb 75: break;
76: case 'l':
1.18 joris 77: cvs_commit.file_flags &= ~CF_RECURSE;
1.1 jfb 78: break;
79: case 'm':
1.14 jfb 80: cvs_msg = strdup(optarg);
81: if (cvs_msg == NULL) {
82: cvs_log(LP_ERRNO, "failed to copy message");
1.23 joris 83: return (CVS_EX_USAGE);
1.14 jfb 84: }
1.1 jfb 85: break;
86: case 'R':
1.18 joris 87: cvs_commit.file_flags |= CF_RECURSE;
1.1 jfb 88: break;
89: default:
1.23 joris 90: return (CVS_EX_USAGE);
1.1 jfb 91: }
92: }
93:
1.14 jfb 94: if ((cvs_msg != NULL) && (mfile != NULL)) {
1.1 jfb 95: cvs_log(LP_ERR, "the -F and -m flags are mutually exclusive");
1.23 joris 96: return (CVS_EX_USAGE);
1.1 jfb 97: }
98:
1.14 jfb 99: if ((mfile != NULL) && (cvs_msg = cvs_logmsg_open(mfile)) == NULL)
1.23 joris 100: return (CVS_EX_DATA);
1.1 jfb 101:
1.18 joris 102: *arg = optind;
103: return (0);
104: }
1.1 jfb 105:
1.18 joris 106: int
107: cvs_commit_helper(void)
108: {
109: struct cvs_flist cl;
110: CVSFILE *cfp;
1.1 jfb 111:
1.26 jfb 112: SIMPLEQ_INIT(&cl);
1.7 jfb 113: cvs_file_examine(cvs_files, cvs_commit_prepare, &cl);
1.26 jfb 114: if (SIMPLEQ_EMPTY(&cl))
1.11 jfb 115: return (0);
1.7 jfb 116:
1.17 joris 117: if (cvs_msg == NULL)
1.14 jfb 118: cvs_msg = cvs_logmsg_get(CVS_FILE_NAME(cvs_files),
119: NULL, &cl, NULL);
1.17 joris 120:
1.26 jfb 121: while (!SIMPLEQ_EMPTY(&cl)) {
122: cfp = SIMPLEQ_FIRST(&cl);
123: SIMPLEQ_REMOVE_HEAD(&cl, cf_list);
1.17 joris 124: cvs_file_free(cfp);
1.7 jfb 125: }
1.17 joris 126:
127: if (cvs_msg == NULL)
1.23 joris 128: return (CVS_EX_DATA);
1.7 jfb 129:
130: return (0);
131: }
132:
133: /*
134: * cvs_commit_prepare()
135: *
136: * Examine the file <cf> to see if it will be part of the commit, in which
137: * case it gets added to the list passed as second argument.
138: */
139: int
140: cvs_commit_prepare(CVSFILE *cf, void *arg)
141: {
142: CVSFILE *copy;
143: struct cvs_flist *clp = (struct cvs_flist *)arg;
144:
1.27 joris 145: if ((cf->cf_type == DT_REG) && ((cf->cf_cvstat == CVS_FST_MODIFIED) ||
1.29 jfb 146: (cf->cf_cvstat == CVS_FST_ADDED) ||
147: (cf->cf_cvstat == CVS_FST_REMOVED))) {
1.7 jfb 148: copy = cvs_file_copy(cf);
149: if (copy == NULL)
1.23 joris 150: return (CVS_EX_DATA);
1.7 jfb 151:
1.26 jfb 152: SIMPLEQ_INSERT_TAIL(clp, copy, cf_list);
1.7 jfb 153: }
1.3 krapht 154:
1.6 jfb 155: return (0);
1.3 krapht 156: }
157:
158:
159: /*
1.6 jfb 160: * cvs_commit_file()
1.3 krapht 161: *
1.6 jfb 162: * Commit a single file.
1.3 krapht 163: */
1.6 jfb 164: int
165: cvs_commit_file(CVSFILE *cf, void *arg)
1.3 krapht 166: {
1.24 xsa 167: int ret, l;
1.6 jfb 168: char *repo, rcspath[MAXPATHLEN], fpath[MAXPATHLEN];
169: RCSFILE *rf;
170: struct cvsroot *root;
171:
1.13 jfb 172: ret = 0;
1.7 jfb 173: rf = NULL;
174: repo = NULL;
1.12 jfb 175: root = CVS_DIR_ROOT(cf);
1.6 jfb 176:
177: if (cf->cf_type == DT_DIR) {
1.13 jfb 178: if (root->cr_method != CVS_METHOD_LOCAL) {
179: if (cf->cf_cvstat != CVS_FST_UNKNOWN)
180: ret = cvs_senddir(root, cf);
1.6 jfb 181: }
1.3 krapht 182:
1.13 jfb 183: return (ret);
1.3 krapht 184: }
185:
1.7 jfb 186: cvs_file_getpath(cf, fpath, sizeof(fpath));
187:
188: if (cf->cf_parent != NULL)
1.26 jfb 189: repo = cf->cf_parent->cf_repo;
1.3 krapht 190:
1.6 jfb 191: if ((cf->cf_cvstat == CVS_FST_ADDED) ||
1.29 jfb 192: (cf->cf_cvstat == CVS_FST_MODIFIED) ||
193: (cf->cf_cvstat == CVS_FST_REMOVED)) {
1.16 joris 194: if (root->cr_method != CVS_METHOD_LOCAL) {
1.26 jfb 195: if (cvs_sendentry(root, cf) < 0) {
1.23 joris 196: return (CVS_EX_PROTO);
1.16 joris 197: }
198:
199: if (cvs_sendreq(root, CVS_REQ_MODIFIED,
200: CVS_FILE_NAME(cf)) < 0) {
1.23 joris 201: return (CVS_EX_PROTO);
1.16 joris 202: }
203:
204: if (cvs_sendfile(root, fpath) < 0) {
1.23 joris 205: return (CVS_EX_PROTO);
1.16 joris 206: }
1.5 krapht 207: }
208: }
1.3 krapht 209:
1.24 xsa 210: l = snprintf(rcspath, sizeof(rcspath), "%s/%s/%s%s",
1.6 jfb 211: root->cr_dir, repo, fpath, RCS_FILE_EXT);
1.24 xsa 212: if (l == -1 || l >= (int)sizeof(rcspath)) {
213: errno = ENAMETOOLONG;
214: cvs_log(LP_ERRNO, "%s", rcspath);
215: return (-1);
216: }
1.3 krapht 217:
1.6 jfb 218: return (0);
1.1 jfb 219: }