Annotation of src/usr.bin/cvs/commit.c, Revision 1.50
1.50 ! joris 1: /* $OpenBSD: commit.c,v 1.49 2005/12/22 14:59:54 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.50 ! joris 190: cvs_logmsg_send(root, cvs_msg);
1.36 joris 191:
1.35 xsa 192: if (rev != NULL) {
1.50 ! joris 193: cvs_sendarg(root, "-r", 0);
! 194: cvs_sendarg(root, rev, 0);
1.35 xsa 195: }
196: }
1.7 jfb 197:
198: return (0);
199: }
200:
201: /*
202: * cvs_commit_prepare()
203: *
204: * Examine the file <cf> to see if it will be part of the commit, in which
205: * case it gets added to the list passed as second argument.
206: */
207: int
208: cvs_commit_prepare(CVSFILE *cf, void *arg)
209: {
210: CVSFILE *copy;
211: struct cvs_flist *clp = (struct cvs_flist *)arg;
212:
1.43 joris 213: if ((cf->cf_type == DT_REG) && (cf->cf_cvstat == wantedstatus)) {
1.7 jfb 214: copy = cvs_file_copy(cf);
215: if (copy == NULL)
1.23 joris 216: return (CVS_EX_DATA);
1.7 jfb 217:
1.26 jfb 218: SIMPLEQ_INSERT_TAIL(clp, copy, cf_list);
1.7 jfb 219: }
1.3 krapht 220:
1.6 jfb 221: return (0);
1.3 krapht 222: }
223:
224:
225: /*
1.39 xsa 226: * cvs_commit_remote()
1.3 krapht 227: *
1.6 jfb 228: * Commit a single file.
1.3 krapht 229: */
1.6 jfb 230: int
1.39 xsa 231: cvs_commit_remote(CVSFILE *cf, void *arg)
1.3 krapht 232: {
1.39 xsa 233: int ret;
234: char *repo, fpath[MAXPATHLEN];
1.6 jfb 235: RCSFILE *rf;
236: struct cvsroot *root;
237:
1.13 jfb 238: ret = 0;
1.7 jfb 239: rf = NULL;
240: repo = NULL;
1.12 jfb 241: root = CVS_DIR_ROOT(cf);
1.6 jfb 242:
243: if (cf->cf_type == DT_DIR) {
1.50 ! joris 244: if (cf->cf_cvstat != CVS_FST_UNKNOWN)
! 245: cvs_senddir(root, cf);
1.33 joris 246: return (0);
1.3 krapht 247: }
248:
1.7 jfb 249: cvs_file_getpath(cf, fpath, sizeof(fpath));
250:
251: if (cf->cf_parent != NULL)
1.26 jfb 252: repo = cf->cf_parent->cf_repo;
1.3 krapht 253:
1.6 jfb 254: if ((cf->cf_cvstat == CVS_FST_ADDED) ||
1.29 jfb 255: (cf->cf_cvstat == CVS_FST_MODIFIED) ||
256: (cf->cf_cvstat == CVS_FST_REMOVED)) {
1.50 ! joris 257: cvs_sendentry(root, cf);
1.31 joris 258:
1.39 xsa 259: /* if it's removed, don't bother sending a
260: * Modified request together with the file its
261: * contents.
262: */
263: if (cf->cf_cvstat == CVS_FST_REMOVED)
264: return (0);
1.16 joris 265:
1.50 ! joris 266: cvs_sendreq(root, CVS_REQ_MODIFIED, cf->cf_name);
! 267: cvs_sendfile(root, fpath);
1.5 krapht 268: }
1.3 krapht 269:
1.39 xsa 270: return (0);
271: }
272:
273: static int
274: cvs_commit_local(CVSFILE *cf, void *arg)
275: {
276: char fpath[MAXPATHLEN], rcspath[MAXPATHLEN];
277:
278: if (cf->cf_type == DT_DIR) {
279: if (verbosity > 1)
1.46 xsa 280: cvs_log(LP_NOTICE, "Examining %s", cf->cf_name);
1.39 xsa 281: return (0);
282: }
283:
284: cvs_file_getpath(cf, fpath, sizeof(fpath));
1.49 xsa 285: cvs_rcs_getpath(cf, rcspath, sizeof(rcspath));
1.3 krapht 286:
1.6 jfb 287: return (0);
1.1 jfb 288: }