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