xref: /openbsd-src/usr.bin/cvs/commit.c (revision fe0dabd9a4164e3b14a624bead23a4b2f17d22ff)
1 /*	$OpenBSD: commit.c,v 1.24 2005/04/15 14:34:15 xsa 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 
44 int cvs_commit_prepare(CVSFILE *, void *);
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,
57 	CVS_CMD_NEEDLOG | CVS_CMD_SENDDIR | CVS_CMD_SENDARGS2
58 };
59 
60 static char *mfile = NULL;
61 
62 int
63 cvs_commit_options(char *opt, int argc, char **argv, int *arg)
64 {
65 	int ch;
66 
67 	while ((ch = getopt(argc, argv, opt)) != -1) {
68 		switch (ch) {
69 		case 'F':
70 			mfile = optarg;
71 			break;
72 		case 'f':
73 			/* XXX half-implemented */
74 			cvs_commit.file_flags &= ~CF_RECURSE;
75 			break;
76 		case 'l':
77 			cvs_commit.file_flags &= ~CF_RECURSE;
78 			break;
79 		case 'm':
80 			cvs_msg = strdup(optarg);
81 			if (cvs_msg == NULL) {
82 				cvs_log(LP_ERRNO, "failed to copy message");
83 				return (CVS_EX_USAGE);
84 			}
85 			break;
86 		case 'R':
87 			cvs_commit.file_flags |= CF_RECURSE;
88 			break;
89 		default:
90 			return (CVS_EX_USAGE);
91 		}
92 	}
93 
94 	if ((cvs_msg != NULL) && (mfile != NULL)) {
95 		cvs_log(LP_ERR, "the -F and -m flags are mutually exclusive");
96 		return (CVS_EX_USAGE);
97 	}
98 
99 	if ((mfile != NULL) && (cvs_msg = cvs_logmsg_open(mfile)) == NULL)
100 		return (CVS_EX_DATA);
101 
102 	*arg = optind;
103 	return (0);
104 }
105 
106 int
107 cvs_commit_helper(void)
108 {
109 	struct cvs_flist cl;
110 	CVSFILE *cfp;
111 
112 	TAILQ_INIT(&cl);
113 	cvs_file_examine(cvs_files, cvs_commit_prepare, &cl);
114 	if (TAILQ_EMPTY(&cl))
115 		return (0);
116 
117 	if (cvs_msg == NULL)
118 		cvs_msg = cvs_logmsg_get(CVS_FILE_NAME(cvs_files),
119 		    NULL, &cl, NULL);
120 
121 	while (!TAILQ_EMPTY(&cl)) {
122 		cfp = TAILQ_FIRST(&cl);
123 		TAILQ_REMOVE(&cl, cfp, cf_list);
124 		cvs_file_free(cfp);
125 	}
126 
127 	if (cvs_msg == NULL)
128 		return (CVS_EX_DATA);
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 
145 	if ((cf->cf_type == DT_REG) && (cf->cf_cvstat == CVS_FST_MODIFIED)) {
146 		copy = cvs_file_copy(cf);
147 		if (copy == NULL)
148 			return (CVS_EX_DATA);
149 
150 		TAILQ_INSERT_TAIL(clp, copy, cf_list);
151 	}
152 
153 	return (0);
154 }
155 
156 
157 /*
158  * cvs_commit_file()
159  *
160  * Commit a single file.
161  */
162 int
163 cvs_commit_file(CVSFILE *cf, void *arg)
164 {
165 	int ret, l;
166 	char *repo, rcspath[MAXPATHLEN], fpath[MAXPATHLEN];
167 	RCSFILE *rf;
168 	struct cvsroot *root;
169 	struct cvs_ent *entp;
170 
171 	ret = 0;
172 	rf = NULL;
173 	repo = NULL;
174 	root = CVS_DIR_ROOT(cf);
175 
176 	if (cf->cf_type == DT_DIR) {
177 		if (root->cr_method != CVS_METHOD_LOCAL) {
178 			if (cf->cf_cvstat != CVS_FST_UNKNOWN)
179 				ret = cvs_senddir(root, cf);
180 		}
181 
182 		return (ret);
183 	}
184 
185 	cvs_file_getpath(cf, fpath, sizeof(fpath));
186 
187 	if (cf->cf_parent != NULL)
188 		repo = cf->cf_parent->cf_ddat->cd_repo;
189 
190 	entp = cvs_ent_getent(fpath);
191 	if (entp == NULL)
192 		return (CVS_EX_DATA);
193 
194 	if ((cf->cf_cvstat == CVS_FST_ADDED) ||
195 	    (cf->cf_cvstat == CVS_FST_MODIFIED)) {
196 		if (root->cr_method != CVS_METHOD_LOCAL) {
197 			if (cvs_sendentry(root, entp) < 0) {
198 				cvs_ent_free(entp);
199 				return (CVS_EX_PROTO);
200 			}
201 
202 			if (cvs_sendreq(root, CVS_REQ_MODIFIED,
203 			    CVS_FILE_NAME(cf)) < 0) {
204 				cvs_ent_free(entp);
205 				return (CVS_EX_PROTO);
206 			}
207 
208 			if (cvs_sendfile(root, fpath) < 0) {
209 				cvs_ent_free(entp);
210 				return (CVS_EX_PROTO);
211 			}
212 		}
213 	}
214 
215 	l = snprintf(rcspath, sizeof(rcspath), "%s/%s/%s%s",
216 	    root->cr_dir, repo, fpath, RCS_FILE_EXT);
217 	if (l == -1 || l >= (int)sizeof(rcspath)) {
218 		errno = ENAMETOOLONG;
219 		cvs_log(LP_ERRNO, "%s", rcspath);
220 		return (-1);
221 	}
222 
223 	cvs_ent_free(entp);
224 
225 	return (0);
226 }
227