xref: /openbsd-src/usr.bin/cvs/commit.c (revision 799f675f6700f14e59124f9825c723e9f2ce19dc)
1 /*	$OpenBSD: commit.c,v 1.101 2007/01/26 21:48:17 xsa Exp $	*/
2 /*
3  * Copyright (c) 2006 Joris Vink <joris@openbsd.org>
4  * Copyright (c) 2006 Xavier Santolaria <xsa@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include "includes.h"
20 
21 #include "cvs.h"
22 #include "diff.h"
23 #include "log.h"
24 #include "remote.h"
25 
26 void	cvs_commit_local(struct cvs_file *);
27 void	cvs_commit_check_files(struct cvs_file *);
28 
29 static BUF *commit_diff_file(struct cvs_file *);
30 static void commit_desc_set(struct cvs_file *);
31 
32 struct	cvs_flisthead files_affected;
33 struct	cvs_flisthead files_added;
34 struct	cvs_flisthead files_removed;
35 struct	cvs_flisthead files_modified;
36 
37 int	conflicts_found;
38 char	*logmsg = NULL;
39 
40 struct cvs_cmd cvs_cmd_commit = {
41 	CVS_OP_COMMIT, 0, "commit",
42 	{ "ci", "com" },
43 	"Check files into the repository",
44 	"[-flR] [-F logfile | -m msg] [-r rev] ...",
45 	"F:flm:Rr:",
46 	NULL,
47 	cvs_commit
48 };
49 
50 int
51 cvs_commit(int argc, char **argv)
52 {
53 	int ch;
54 	char *arg = ".";
55 	int flags;
56 	struct cvs_recursion cr;
57 
58 	flags = CR_RECURSE_DIRS;
59 
60 	while ((ch = getopt(argc, argv, cvs_cmd_commit.cmd_opts)) != -1) {
61 		switch (ch) {
62 		case 'F':
63 			logmsg = cvs_logmsg_read(optarg);
64 			break;
65 		case 'f':
66 			break;
67 		case 'l':
68 			flags &= ~CR_RECURSE_DIRS;
69 			break;
70 		case 'm':
71 			logmsg = xstrdup(optarg);
72 			break;
73 		case 'R':
74 			break;
75 		case 'r':
76 			break;
77 		default:
78 			fatal("%s", cvs_cmd_commit.cmd_synopsis);
79 		}
80 	}
81 
82 	argc -= optind;
83 	argv += optind;
84 
85 	TAILQ_INIT(&files_affected);
86 	TAILQ_INIT(&files_added);
87 	TAILQ_INIT(&files_removed);
88 	TAILQ_INIT(&files_modified);
89 	conflicts_found = 0;
90 
91 	cr.enterdir = NULL;
92 	cr.leavedir = NULL;
93 	cr.fileproc = cvs_commit_check_files;
94 	cr.flags = flags;
95 
96 	if (argc > 0)
97 		cvs_file_run(argc, argv, &cr);
98 	else
99 		cvs_file_run(1, &arg, &cr);
100 
101 	if (conflicts_found != 0)
102 		fatal("%d conflicts found, please correct these first",
103 		    conflicts_found);
104 
105 	if (TAILQ_EMPTY(&files_affected))
106 		return (0);
107 
108 	if (logmsg == NULL && cvs_server_active == 0) {
109 		logmsg = cvs_logmsg_create(&files_added, &files_removed,
110 		    &files_modified);
111 	}
112 
113 	if (logmsg == NULL)
114 		fatal("This shouldnt happen, honestly!");
115 
116 	cvs_file_freelist(&files_modified);
117 	cvs_file_freelist(&files_removed);
118 	cvs_file_freelist(&files_added);
119 
120 	if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
121 		cvs_client_connect_to_server();
122 		cr.fileproc = cvs_client_sendfile;
123 
124 		if (argc > 0)
125 			cvs_file_run(argc, argv, &cr);
126 		else
127 			cvs_file_run(1, &arg, &cr);
128 
129 		if (!(flags & CR_RECURSE_DIRS))
130 			cvs_client_send_request("Argument -l");
131 
132 		cvs_client_send_request("Argument -m%s", logmsg);
133 
134 		cvs_client_send_files(argv, argc);
135 		cvs_client_senddir(".");
136 		cvs_client_send_request("ci");
137 		cvs_client_get_responses();
138 	} else {
139 		cr.fileproc = cvs_commit_local;
140 		cvs_file_walklist(&files_affected, &cr);
141 		cvs_file_freelist(&files_affected);
142 	}
143 
144 	return (0);
145 }
146 
147 void
148 cvs_commit_check_files(struct cvs_file *cf)
149 {
150 	cvs_log(LP_TRACE, "cvs_commit_check_files(%s)", cf->file_path);
151 
152 	/*
153 	 * cvs_file_classify makes the noise for us
154 	 * XXX - we want that?
155 	 */
156 	cvs_file_classify(cf, NULL, 1);
157 
158 	if (cf->file_type == CVS_DIR) {
159 		if (verbosity > 1)
160 			cvs_log(LP_NOTICE, "Examining %s", cf->file_path);
161 		return;
162 	}
163 
164 	if (cf->file_status == FILE_CONFLICT ||
165 	    cf->file_status == FILE_UNLINK) {
166 		conflicts_found++;
167 		return;
168 	}
169 
170 	if (cf->file_status != FILE_REMOVED &&
171 	    update_has_conflict_markers(cf)) {
172 		cvs_log(LP_ERR, "conflict: unresolved conflicts in %s from "
173 		    "merging, please fix these first", cf->file_path);
174 		conflicts_found++;
175 		return;
176 	}
177 
178 	if (cf->file_status == FILE_MERGE ||
179 	    cf->file_status == FILE_PATCH ||
180 	    cf->file_status == FILE_CHECKOUT ||
181 	    cf->file_status == FILE_LOST) {
182 		cvs_log(LP_ERR, "conflict: %s is not up-to-date",
183 		    cf->file_path);
184 		conflicts_found++;
185 		return;
186 	}
187 
188 	if (cf->file_status == FILE_ADDED ||
189 	    cf->file_status == FILE_REMOVED ||
190 	    cf->file_status == FILE_MODIFIED)
191 		cvs_file_get(cf->file_path, &files_affected);
192 
193 	switch (cf->file_status) {
194 	case FILE_ADDED:
195 		cvs_file_get(cf->file_path, &files_added);
196 		break;
197 	case FILE_REMOVED:
198 		cvs_file_get(cf->file_path, &files_removed);
199 		break;
200 	case FILE_MODIFIED:
201 		cvs_file_get(cf->file_path, &files_modified);
202 		break;
203 	}
204 }
205 
206 void
207 cvs_commit_local(struct cvs_file *cf)
208 {
209 	BUF *b, *d;
210 	int isnew;
211 	RCSNUM *head;
212 	int l, openflags, rcsflags;
213 	char rbuf[24], nbuf[24];
214 	CVSENTRIES *entlist;
215 	char attic[MAXPATHLEN], repo[MAXPATHLEN], rcsfile[MAXPATHLEN];
216 
217 	cvs_log(LP_TRACE, "cvs_commit_local(%s)", cf->file_path);
218 	cvs_file_classify(cf, NULL, 0);
219 
220 	if (cvs_noexec == 1)
221 		return;
222 
223 	if (cf->file_type != CVS_FILE)
224 		fatal("cvs_commit_local: '%s' is not a file", cf->file_path);
225 
226 	if (cf->file_status == FILE_MODIFIED ||
227 	    cf->file_status == FILE_REMOVED || (cf->file_status == FILE_ADDED
228 	    && cf->file_rcs != NULL && cf->file_rcs->rf_dead == 1)) {
229 		head = rcs_head_get(cf->file_rcs);
230 		rcsnum_tostr(head, rbuf, sizeof(rbuf));
231 		rcsnum_free(head);
232 	} else {
233 		strlcpy(rbuf, "Non-existent", sizeof(rbuf));
234 	}
235 
236 	isnew = 0;
237 	if (cf->file_status == FILE_ADDED) {
238 		isnew = 1;
239 		rcsflags = RCS_CREATE;
240 		openflags = O_CREAT | O_TRUNC | O_WRONLY;
241 		if (cf->file_rcs != NULL) {
242 			if (cf->file_rcs->rf_inattic == 0)
243 				cvs_log(LP_ERR, "warning: expected %s "
244 				    "to be in the Attic", cf->file_path);
245 
246 			if (cf->file_rcs->rf_dead == 0)
247 				cvs_log(LP_ERR, "warning: expected %s "
248 				    "to be dead", cf->file_path);
249 
250 			cvs_get_repository_path(cf->file_wd, repo, MAXPATHLEN);
251 			l = snprintf(rcsfile, MAXPATHLEN, "%s/%s%s",
252 			    repo, cf->file_name, RCS_FILE_EXT);
253 			if (l == -1 || l >= MAXPATHLEN)
254 				fatal("cvs_commit_local: overflow");
255 
256 			if (rename(cf->file_rpath, rcsfile) == -1)
257 				fatal("cvs_commit_local: failed to move %s "
258 				    "outside the Attic: %s", cf->file_path,
259 				    strerror(errno));
260 
261 			xfree(cf->file_rpath);
262 			cf->file_rpath = xstrdup(rcsfile);
263 
264 			rcsflags = RCS_READ | RCS_PARSE_FULLY;
265 			openflags = O_RDONLY;
266 			rcs_close(cf->file_rcs);
267 			isnew = 0;
268 		}
269 
270 		cf->repo_fd = open(cf->file_rpath, openflags);
271 		if (cf->repo_fd < 0)
272 			fatal("cvs_commit_local: %s", strerror(errno));
273 
274 		cf->file_rcs = rcs_open(cf->file_rpath, cf->repo_fd,
275 		    rcsflags, 0600);
276 		if (cf->file_rcs == NULL)
277 			fatal("cvs_commit_local: failed to create RCS file "
278 			    "for %s", cf->file_path);
279 
280 		commit_desc_set(cf);
281 	}
282 
283 	if (verbosity > 1) {
284 		cvs_printf("Checking in %s:\n", cf->file_path);
285 		cvs_printf("%s <- %s\n", cf->file_rpath, cf->file_path);
286 		cvs_printf("old revision: %s; ", rbuf);
287 	}
288 
289 	if (isnew == 0)
290 		d = commit_diff_file(cf);
291 
292 	if (cf->file_status == FILE_REMOVED) {
293 		b = rcs_rev_getbuf(cf->file_rcs, cf->file_rcs->rf_head, 0);
294 		if (b == NULL)
295 			fatal("cvs_commit_local: failed to get HEAD");
296 	} else {
297 		if ((b = cvs_buf_load_fd(cf->fd, BUF_AUTOEXT)) == NULL)
298 			fatal("cvs_commit_local: failed to load file");
299 	}
300 
301 	if (isnew == 0) {
302 		if (rcs_deltatext_set(cf->file_rcs,
303 		    cf->file_rcs->rf_head, d) == -1)
304 			fatal("cvs_commit_local: failed to set delta");
305 	}
306 
307 	if (rcs_rev_add(cf->file_rcs, RCS_HEAD_REV, logmsg, -1, NULL) == -1)
308 		fatal("cvs_commit_local: failed to add new revision");
309 
310 	if (rcs_deltatext_set(cf->file_rcs, cf->file_rcs->rf_head, b) == -1)
311 		fatal("cvs_commit_local: failed to set new HEAD delta");
312 
313 	if (cf->file_status == FILE_REMOVED) {
314 		if (rcs_state_set(cf->file_rcs,
315 		    cf->file_rcs->rf_head, RCS_STATE_DEAD) == -1)
316 			fatal("cvs_commit_local: failed to set state");
317 	}
318 
319 	if (cf->file_rcs->rf_branch != NULL) {
320 		rcsnum_free(cf->file_rcs->rf_branch);
321 		cf->file_rcs->rf_branch = NULL;
322 	}
323 
324 	if (cf->file_status == FILE_ADDED && cf->file_ent->ce_opts != NULL) {
325 		int kflag;
326 
327 		kflag = rcs_kflag_get(cf->file_ent->ce_opts + 2);
328 		rcs_kwexp_set(cf->file_rcs, kflag);
329 	}
330 
331 	rcs_write(cf->file_rcs);
332 
333 	if (cf->file_status == FILE_REMOVED) {
334 		strlcpy(nbuf, "Removed", sizeof(nbuf));
335 	} else if (cf->file_status == FILE_ADDED) {
336 		if (cf->file_rcs->rf_dead == 1)
337 			strlcpy(nbuf, "Initial Revision", sizeof(nbuf));
338 		else
339 			rcsnum_tostr(cf->file_rcs->rf_head,
340 			    nbuf, sizeof(nbuf));
341 	} else if (cf->file_status == FILE_MODIFIED) {
342 		rcsnum_tostr(cf->file_rcs->rf_head, nbuf, sizeof(nbuf));
343 	}
344 
345 	if (verbosity > 1)
346 		cvs_printf("new revision: %s\n", nbuf);
347 
348 	(void)unlink(cf->file_path);
349 	(void)close(cf->fd);
350 	cf->fd = -1;
351 
352 	if (cf->file_status != FILE_REMOVED) {
353 		cvs_checkout_file(cf, cf->file_rcs->rf_head, CO_COMMIT);
354 	} else {
355 		entlist = cvs_ent_open(cf->file_wd);
356 		cvs_ent_remove(entlist, cf->file_name);
357 		cvs_ent_close(entlist, ENT_SYNC);
358 
359 		cvs_get_repository_path(cf->file_wd, repo, MAXPATHLEN);
360 
361 		l = snprintf(attic, MAXPATHLEN, "%s/%s", repo, CVS_PATH_ATTIC);
362 		if (l == -1 || l >= MAXPATHLEN)
363 			fatal("cvs_commit_local: overflow");
364 
365 		if (mkdir(attic, 0755) == -1 && errno != EEXIST)
366 			fatal("cvs_commit_local: failed to create Attic");
367 
368 		l = snprintf(attic, MAXPATHLEN, "%s/%s/%s%s", repo,
369 		    CVS_PATH_ATTIC, cf->file_name, RCS_FILE_EXT);
370 		if (l == -1 || l >= MAXPATHLEN)
371 			fatal("cvs_commit_local: overflow");
372 
373 		if (rename(cf->file_rpath, attic) == -1)
374 			fatal("cvs_commit_local: failed to move %s to Attic",
375 			    cf->file_path);
376 
377 		if (cvs_server_active == 1)
378 			cvs_server_update_entry("Remove-entry", cf);
379 	}
380 
381 	if (verbosity > 1)
382 		cvs_printf("done\n");
383 	else {
384 		cvs_log(LP_NOTICE, "checking in '%s'; revision %s -> %s",
385 		    cf->file_path, rbuf, nbuf);
386 	}
387 }
388 
389 static BUF *
390 commit_diff_file(struct cvs_file *cf)
391 {
392 	char *p1, *p2;
393 	BUF *b1, *b2;
394 
395 	(void)xasprintf(&p1, "%s/diff1.XXXXXXXXXX", cvs_tmpdir);
396 
397 	if (cf->file_status == FILE_MODIFIED ||
398 	    cf->file_status == FILE_ADDED) {
399 		if ((b1 = cvs_buf_load_fd(cf->fd, BUF_AUTOEXT)) == NULL)
400 			fatal("commit_diff_file: failed to load '%s'",
401 			    cf->file_path);
402 		cvs_buf_write_stmp(b1, p1, NULL);
403 		cvs_buf_free(b1);
404 	} else {
405 		rcs_rev_write_stmp(cf->file_rcs, cf->file_rcs->rf_head, p1, 0);
406 	}
407 
408 	(void)xasprintf(&p2, "%s/diff2.XXXXXXXXXX", cvs_tmpdir);
409 	rcs_rev_write_stmp(cf->file_rcs, cf->file_rcs->rf_head, p2, 0);
410 
411 	if ((b2 = cvs_buf_alloc(128, BUF_AUTOEXT)) == NULL)
412 		fatal("commit_diff_file: failed to create diff buf");
413 
414 	diff_format = D_RCSDIFF;
415 	if (cvs_diffreg(p1, p2, b2) == D_ERROR)
416 		fatal("commit_diff_file: failed to get RCS patch");
417 
418 	xfree(p1);
419 	xfree(p2);
420 
421 	return (b2);
422 }
423 
424 static void
425 commit_desc_set(struct cvs_file *cf)
426 {
427 	BUF *bp;
428 	int l, fd;
429 	char desc_path[MAXPATHLEN], *desc;
430 
431 	l = snprintf(desc_path, MAXPATHLEN, "%s/%s%s",
432 	    CVS_PATH_CVSDIR, cf->file_name, CVS_DESCR_FILE_EXT);
433 	if (l == -1 || l >= MAXPATHLEN)
434 		fatal("commit_desc_set: overflow");
435 
436 	if ((fd = open(desc_path, O_RDONLY)) == -1)
437 		return;
438 
439 	bp = cvs_buf_load_fd(fd, BUF_AUTOEXT);
440 	cvs_buf_putc(bp, '\0');
441 	desc = cvs_buf_release(bp);
442 
443 	rcs_desc_set(cf->file_rcs, desc);
444 
445 	(void)close(fd);
446 	(void)cvs_unlink(desc_path);
447 
448 	xfree(desc);
449 }
450