Annotation of src/usr.bin/cvs/commit.c, Revision 1.49
1.49 ! xsa 1: /* $OpenBSD: commit.c,v 1.48 2005/12/21 20:06:25 xsa 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.45 xsa 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_remote(CVSFILE *, void *);
47: static int cvs_commit_local(CVSFILE *, void *);
48: static int cvs_commit_pre_exec(struct cvsroot *);
1.34 jfb 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,
1.41 joris 64: CVS_CMD_SENDDIR | 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.43 joris 71: static int wantedstatus = 0;
1.6 jfb 72:
1.34 jfb 73: static int
74: cvs_commit_init(struct cvs_cmd *cmd, int argc, char **argv, int *arg)
1.1 jfb 75: {
1.18 joris 76: int ch;
1.3 krapht 77:
1.34 jfb 78: while ((ch = getopt(argc, argv, cmd->cmd_opts)) != -1) {
1.1 jfb 79: switch (ch) {
80: case 'F':
81: mfile = optarg;
82: break;
83: case 'f':
1.10 jfb 84: /* XXX half-implemented */
1.34 jfb 85: cmd->file_flags &= ~CF_RECURSE;
1.1 jfb 86: break;
87: case 'l':
1.34 jfb 88: cmd->file_flags &= ~CF_RECURSE;
1.1 jfb 89: break;
90: case 'm':
1.47 joris 91: cvs_msg = xstrdup(optarg);
1.1 jfb 92: break;
93: case 'R':
1.34 jfb 94: cmd->file_flags |= CF_RECURSE;
1.1 jfb 95: break;
1.35 xsa 96: case 'r':
97: rev = optarg;
98: break;
1.1 jfb 99: default:
1.23 joris 100: return (CVS_EX_USAGE);
1.1 jfb 101: }
102: }
103:
1.14 jfb 104: if ((cvs_msg != NULL) && (mfile != NULL)) {
1.1 jfb 105: cvs_log(LP_ERR, "the -F and -m flags are mutually exclusive");
1.23 joris 106: return (CVS_EX_USAGE);
1.1 jfb 107: }
108:
1.48 xsa 109: if (mfile != NULL)
110: cvs_msg = cvs_logmsg_open(mfile);
1.1 jfb 111:
1.18 joris 112: *arg = optind;
1.32 joris 113:
114: commit_files = (argv + optind);
115: commit_fcount = (argc - optind);
116:
1.18 joris 117: return (0);
118: }
1.1 jfb 119:
1.18 joris 120: int
1.34 jfb 121: cvs_commit_pre_exec(struct cvsroot *root)
1.18 joris 122: {
123: CVSFILE *cfp;
1.32 joris 124: CVSFILE *tmp;
1.44 joris 125: int ret, i, flags = CF_RECURSE | CF_IGNORE | CF_SORT;
1.43 joris 126: struct cvs_flist added, modified, removed, *cl[3];
127: int stattype[] = { CVS_FST_ADDED, CVS_FST_MODIFIED, CVS_FST_REMOVED };
128:
129: SIMPLEQ_INIT(&added);
130: SIMPLEQ_INIT(&modified);
131: SIMPLEQ_INIT(&removed);
132:
133: cl[0] = &added;
134: cl[1] = &modified;
135: cl[2] = &removed;
136:
1.44 joris 137: if ((tmp = cvs_file_loadinfo(".", CF_NOFILES, NULL, NULL, 1)) == NULL)
138: return (CVS_EX_DATA);
139:
1.43 joris 140: /*
141: * Obtain the file lists for the logmessage.
142: */
143: for (i = 0; i < 3; i++) {
144: wantedstatus = stattype[i];
145: if (commit_fcount != 0) {
1.44 joris 146: ret = cvs_file_getspec(commit_files, commit_fcount,
147: flags, cvs_commit_prepare, cl[i], NULL);
1.43 joris 148: } else {
1.44 joris 149: ret = cvs_file_get(".", flags, cvs_commit_prepare,
150: cl[i], NULL);
1.43 joris 151: }
1.32 joris 152:
1.44 joris 153: if (ret != CVS_EX_OK) {
154: cvs_file_free(tmp);
1.43 joris 155: return (CVS_EX_DATA);
1.44 joris 156: }
1.32 joris 157: }
1.1 jfb 158:
1.43 joris 159: /*
160: * If we didn't catch any file, don't call the editor.
161: */
162: if (SIMPLEQ_EMPTY(&added) && SIMPLEQ_EMPTY(&modified) &&
163: SIMPLEQ_EMPTY(&removed)) {
1.32 joris 164: cvs_file_free(tmp);
1.11 jfb 165: return (0);
1.32 joris 166: }
1.7 jfb 167:
1.43 joris 168: /*
169: * Fetch the log message for real, with all the files.
170: */
1.17 joris 171: if (cvs_msg == NULL)
1.43 joris 172: cvs_msg = cvs_logmsg_get(tmp->cf_name, &added, &modified,
173: &removed);
1.32 joris 174:
175: cvs_file_free(tmp);
1.17 joris 176:
1.43 joris 177: /* free the file lists */
178: for (i = 0; i < 3; i++) {
179: while (!SIMPLEQ_EMPTY(cl[i])) {
180: cfp = SIMPLEQ_FIRST(cl[i]);
181: SIMPLEQ_REMOVE_HEAD(cl[i], cf_list);
182: cvs_file_free(cfp);
183: }
1.7 jfb 184: }
1.17 joris 185:
186: if (cvs_msg == NULL)
1.23 joris 187: return (CVS_EX_DATA);
1.35 xsa 188:
189: if (root->cr_method != CVS_METHOD_LOCAL) {
1.36 joris 190: if (cvs_logmsg_send(root, cvs_msg) < 0)
191: return (CVS_EX_PROTO);
192:
1.35 xsa 193: if (rev != NULL) {
194: if ((cvs_sendarg(root, "-r", 0) < 0) ||
195: (cvs_sendarg(root, rev, 0) < 0))
196: return (CVS_EX_PROTO);
197: }
198: }
1.7 jfb 199:
200: return (0);
201: }
202:
203: /*
204: * cvs_commit_prepare()
205: *
206: * Examine the file <cf> to see if it will be part of the commit, in which
207: * case it gets added to the list passed as second argument.
208: */
209: int
210: cvs_commit_prepare(CVSFILE *cf, void *arg)
211: {
212: CVSFILE *copy;
213: struct cvs_flist *clp = (struct cvs_flist *)arg;
214:
1.43 joris 215: if ((cf->cf_type == DT_REG) && (cf->cf_cvstat == wantedstatus)) {
1.7 jfb 216: copy = cvs_file_copy(cf);
217: if (copy == NULL)
1.23 joris 218: return (CVS_EX_DATA);
1.7 jfb 219:
1.26 jfb 220: SIMPLEQ_INSERT_TAIL(clp, copy, cf_list);
1.7 jfb 221: }
1.3 krapht 222:
1.6 jfb 223: return (0);
1.3 krapht 224: }
225:
226:
227: /*
1.39 xsa 228: * cvs_commit_remote()
1.3 krapht 229: *
1.6 jfb 230: * Commit a single file.
1.3 krapht 231: */
1.6 jfb 232: int
1.39 xsa 233: cvs_commit_remote(CVSFILE *cf, void *arg)
1.3 krapht 234: {
1.39 xsa 235: int ret;
236: char *repo, fpath[MAXPATHLEN];
1.6 jfb 237: RCSFILE *rf;
238: struct cvsroot *root;
239:
1.13 jfb 240: ret = 0;
1.7 jfb 241: rf = NULL;
242: repo = NULL;
1.12 jfb 243: root = CVS_DIR_ROOT(cf);
1.6 jfb 244:
245: if (cf->cf_type == DT_DIR) {
1.39 xsa 246: if (cf->cf_cvstat != CVS_FST_UNKNOWN) {
247: if (cvs_senddir(root, cf) < 0)
248: return (CVS_EX_PROTO);
1.6 jfb 249: }
1.33 joris 250: return (0);
1.3 krapht 251: }
252:
1.7 jfb 253: cvs_file_getpath(cf, fpath, sizeof(fpath));
254:
255: if (cf->cf_parent != NULL)
1.26 jfb 256: repo = cf->cf_parent->cf_repo;
1.3 krapht 257:
1.6 jfb 258: if ((cf->cf_cvstat == CVS_FST_ADDED) ||
1.29 jfb 259: (cf->cf_cvstat == CVS_FST_MODIFIED) ||
260: (cf->cf_cvstat == CVS_FST_REMOVED)) {
1.39 xsa 261: if (cvs_sendentry(root, cf) < 0) {
262: return (CVS_EX_PROTO);
263: }
1.31 joris 264:
1.39 xsa 265: /* if it's removed, don't bother sending a
266: * Modified request together with the file its
267: * contents.
268: */
269: if (cf->cf_cvstat == CVS_FST_REMOVED)
270: return (0);
1.16 joris 271:
1.42 joris 272: if (cvs_sendreq(root, CVS_REQ_MODIFIED, cf->cf_name) < 0)
1.39 xsa 273: return (CVS_EX_PROTO);
1.16 joris 274:
1.39 xsa 275: if (cvs_sendfile(root, fpath) < 0) {
276: return (CVS_EX_PROTO);
1.5 krapht 277: }
278: }
1.3 krapht 279:
1.39 xsa 280: return (0);
281: }
282:
283: static int
284: cvs_commit_local(CVSFILE *cf, void *arg)
285: {
286: char fpath[MAXPATHLEN], rcspath[MAXPATHLEN];
287:
288: if (cf->cf_type == DT_DIR) {
289: if (verbosity > 1)
1.46 xsa 290: cvs_log(LP_NOTICE, "Examining %s", cf->cf_name);
1.39 xsa 291: return (0);
292: }
293:
294: cvs_file_getpath(cf, fpath, sizeof(fpath));
1.49 ! xsa 295: cvs_rcs_getpath(cf, rcspath, sizeof(rcspath));
1.3 krapht 296:
1.6 jfb 297: return (0);
1.1 jfb 298: }