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