xref: /openbsd-src/usr.bin/cvs/import.c (revision 91f110e064cd7c194e59e019b83bb7496c1c84d4)
1 /*	$OpenBSD: import.c,v 1.103 2010/09/23 18:10:16 nicm Exp $	*/
2 /*
3  * Copyright (c) 2006 Joris Vink <joris@openbsd.org>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include <sys/stat.h>
19 
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <string.h>
23 #include <unistd.h>
24 
25 #include "cvs.h"
26 #include "diff.h"
27 #include "remote.h"
28 
29 void	cvs_import_local(struct cvs_file *);
30 
31 static void import_loginfo(char *);
32 static void import_new(struct cvs_file *);
33 static void import_printf(const char *, ...)
34 	__attribute__((format(printf, 1, 2)));
35 static void import_update(struct cvs_file *);
36 static void import_tag(struct cvs_file *, RCSNUM *, RCSNUM *);
37 static BUF *import_get_rcsdiff(struct cvs_file *, RCSNUM *);
38 
39 #define IMPORT_DEFAULT_BRANCH	"1.1.1"
40 
41 extern char *loginfo;
42 extern char *logmsg;
43 
44 static char *import_branch = IMPORT_DEFAULT_BRANCH;
45 static char *vendor_tag = NULL;
46 static char **release_tags;
47 static char *koptstr;
48 static int dflag = 0;
49 static int tagcount = 0;
50 static BUF *logbuf;
51 
52 char *import_repository = NULL;
53 int import_conflicts = 0;
54 
55 struct cvs_cmd cvs_cmd_import = {
56 	CVS_OP_IMPORT, CVS_USE_WDIR, "import",
57 	{ "im", "imp" },
58 	"Import sources into CVS, using vendor branches",
59 	"[-b branch] [-d] [-k mode] [-m message] "
60 	"repository vendor-tag release-tags",
61 	"b:dk:m:",
62 	NULL,
63 	cvs_import
64 };
65 
66 int
67 cvs_import(int argc, char **argv)
68 {
69 	int i, ch;
70 	char repo[MAXPATHLEN], *arg = ".";
71 	struct cvs_recursion cr;
72 	struct trigger_list *line_list;
73 
74 	while ((ch = getopt(argc, argv, cvs_cmd_import.cmd_opts)) != -1) {
75 		switch (ch) {
76 		case 'b':
77 			import_branch = optarg;
78 			break;
79 		case 'd':
80 			dflag = 1;
81 			break;
82 		case 'k':
83 			koptstr = optarg;
84 			kflag = rcs_kflag_get(koptstr);
85 			if (RCS_KWEXP_INVAL(kflag)) {
86 				cvs_log(LP_ERR,
87 				    "invalid RCS keyword expansion mode");
88 				fatal("%s", cvs_cmd_import.cmd_synopsis);
89 			}
90 			break;
91 		case 'm':
92 			logmsg = optarg;
93 			break;
94 		default:
95 			fatal("%s", cvs_cmd_import.cmd_synopsis);
96 			break;
97 		}
98 	}
99 
100 	argc -= optind;
101 	argv += optind;
102 
103 	if (argc < 3)
104 		fatal("%s", cvs_cmd_import.cmd_synopsis);
105 
106 	import_repository = argv[0];
107 	vendor_tag = argv[1];
108 	argc -= 2;
109 	argv += 2;
110 
111 	release_tags = argv;
112 	tagcount = argc;
113 
114 	if (!rcs_sym_check(vendor_tag))
115 		fatal("invalid symbol: %s", vendor_tag);
116 
117 	for (i = 0; i < tagcount; i++) {
118 		if (!rcs_sym_check(release_tags[i]))
119 			fatal("invalid symbol: %s", release_tags[i]);
120 	}
121 
122 	if (logmsg == NULL) {
123 		if (cvs_server_active)
124 			fatal("no log message specified");
125 		else
126 			logmsg = cvs_logmsg_create(NULL, NULL, NULL, NULL);
127 	}
128 
129 	if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
130 		cvs_client_connect_to_server();
131 
132 		cvs_client_send_request("Argument -b%s", IMPORT_DEFAULT_BRANCH);
133 
134 		if (kflag)
135 			cvs_client_send_request("Argument -k%s", koptstr);
136 
137 		cvs_client_send_logmsg(logmsg);
138 		cvs_client_send_request("Argument %s", import_repository);
139 		cvs_client_send_request("Argument %s", vendor_tag);
140 		for (i = 0; i < tagcount; i++)
141 			cvs_client_send_request("Argument %s", release_tags[i]);
142 
143 		cr.enterdir = NULL;
144 		cr.leavedir = NULL;
145 		cr.fileproc = cvs_client_sendfile;
146 		cr.flags = CR_RECURSE_DIRS;
147 
148 		cvs_file_run(1, &arg, &cr);
149 		cvs_client_senddir(".");
150 		cvs_client_send_request("import");
151 
152 		cvs_client_get_responses();
153 		return (0);
154 	}
155 
156 	if (cvs_logmsg_verify(logmsg))
157 		return (0);
158 
159 	(void)xsnprintf(repo, sizeof(repo), "%s/%s",
160 	    current_cvsroot->cr_dir, import_repository);
161 
162 	import_loginfo(import_repository);
163 
164 	if (cvs_noexec != 1)
165 		cvs_mkdir(repo, 0755);
166 
167 	cr.enterdir = NULL;
168 	cr.leavedir = NULL;
169 	cr.fileproc = cvs_import_local;
170 	cr.flags = CR_RECURSE_DIRS;
171 	cvs_file_run(1, &arg, &cr);
172 
173 	if (import_conflicts != 0) {
174 		import_printf("\n%d conflicts created by this import.\n\n",
175 		    import_conflicts);
176 		import_printf("Use the following command to help the merge:\n");
177 		import_printf("\topencvs checkout ");
178 		import_printf("-j%s:yesterday -j%s %s\n\n", vendor_tag,
179 		    vendor_tag, import_repository);
180 	} else {
181 		import_printf("\nNo conflicts created by this import.\n\n");
182 	}
183 
184 	loginfo = buf_release(logbuf);
185 	logbuf = NULL;
186 
187 	line_list = cvs_trigger_getlines(CVS_PATH_LOGINFO, import_repository);
188 	if (line_list != NULL) {
189 		cvs_trigger_handle(CVS_TRIGGER_LOGINFO, import_repository,
190 		    loginfo, line_list, NULL);
191 		cvs_trigger_freelist(line_list);
192 	}
193 
194 	xfree(loginfo);
195 	return (0);
196 }
197 
198 static void
199 import_printf(const char *fmt, ...)
200 {
201 	char *str;
202 	va_list vap;
203 
204 	va_start(vap, fmt);
205 	if (vasprintf(&str, fmt, vap) == -1)
206 		fatal("import_printf: could not allocate memory");
207 	va_end(vap);
208 
209 	cvs_printf("%s", str);
210 	buf_puts(logbuf, str);
211 
212 	xfree(str);
213 }
214 
215 void
216 cvs_import_local(struct cvs_file *cf)
217 {
218 	int isnew;
219 	struct stat st;
220 	char repo[MAXPATHLEN];
221 
222 	cvs_log(LP_TRACE, "cvs_import_local(%s)", cf->file_path);
223 
224 	cvs_file_classify(cf, cvs_directory_tag);
225 
226 	if (cf->file_type == CVS_DIR) {
227 		if (!strcmp(cf->file_path, "."))
228 			return;
229 
230 		if (verbosity > 1)
231 			cvs_log(LP_NOTICE, "Importing %s", cf->file_path);
232 
233 		if (cvs_noexec == 1)
234 			return;
235 
236 		if (mkdir(cf->file_rpath, 0755) == -1 && errno != EEXIST)
237 			fatal("cvs_import_local: %s: %s", cf->file_rpath,
238 			    strerror(errno));
239 
240 		return;
241 	}
242 
243 	isnew = 1;
244 	(void)xsnprintf(repo, sizeof(repo), "%s/%s/%s/%s%s",
245 	    current_cvsroot->cr_dir, cf->file_wd, CVS_PATH_ATTIC,
246 	    cf->file_name, RCS_FILE_EXT);
247 
248 	if (cf->file_rcs != NULL || stat(repo, &st) != -1)
249 		isnew = 0;
250 
251 	if (isnew == 1)
252 		import_new(cf);
253 	else
254 		import_update(cf);
255 }
256 
257 static void
258 import_loginfo(char *repo)
259 {
260 	int i;
261 	char pwd[MAXPATHLEN];
262 
263 	if (getcwd(pwd, sizeof(pwd)) == NULL)
264 		fatal("Can't get working directory");
265 
266 	logbuf = buf_alloc(1024);
267 	cvs_trigger_loginfo_header(logbuf, repo);
268 
269 	buf_puts(logbuf, "Log Message:\n");
270 	buf_puts(logbuf, logmsg);
271 	if (logmsg[0] != '\0' && logmsg[strlen(logmsg) - 1] != '\n')
272 		buf_putc(logbuf, '\n');
273 	buf_putc(logbuf, '\n');
274 
275 	buf_puts(logbuf, "Status:\n\n");
276 
277 	buf_puts(logbuf, "Vendor Tag:\t");
278 	buf_puts(logbuf, vendor_tag);
279 	buf_putc(logbuf, '\n');
280 	buf_puts(logbuf, "Release Tags:\t");
281 
282 	for (i = 0; i < tagcount ; i++) {
283 		buf_puts(logbuf, "\t\t");
284 		buf_puts(logbuf, release_tags[i]);
285 		buf_putc(logbuf, '\n');
286 	}
287 	buf_putc(logbuf, '\n');
288 	buf_putc(logbuf, '\n');
289 }
290 
291 static void
292 import_new(struct cvs_file *cf)
293 {
294 	int i;
295 	BUF *bp;
296 	mode_t mode;
297 	time_t tstamp;
298 	struct stat st;
299 	struct rcs_branch *brp;
300 	struct rcs_delta *rdp;
301 	RCSNUM *branch, *brev;
302 
303 	tstamp = -1;
304 
305 	cvs_log(LP_TRACE, "import_new(%s)", cf->file_name);
306 
307 	if (cvs_noexec == 1) {
308 		import_printf("N %s/%s\n", import_repository, cf->file_path);
309 		return;
310 	}
311 
312 	if (fstat(cf->fd, &st) == -1)
313 		fatal("import_new: %s", strerror(errno));
314 
315 	mode = st.st_mode;
316 
317 	if (dflag == 1)
318 		tstamp = st.st_mtime;
319 
320 	if ((branch = rcsnum_parse(import_branch)) == NULL)
321 		fatal("import_new: failed to parse branch");
322 
323 	bp = buf_load_fd(cf->fd);
324 
325 	if ((brev = rcsnum_brtorev(branch)) == NULL)
326 		fatal("import_new: failed to get first branch revision");
327 
328 	cf->repo_fd = open(cf->file_rpath, O_CREAT | O_RDONLY);
329 	if (cf->repo_fd < 0)
330 		fatal("import_new: %s: %s", cf->file_rpath, strerror(errno));
331 
332 	cf->file_rcs = rcs_open(cf->file_rpath, cf->repo_fd, RCS_CREATE,
333 	    (mode & ~(S_IWUSR | S_IWGRP | S_IWOTH)));
334 	if (cf->file_rcs == NULL)
335 		fatal("import_new: failed to create RCS file for %s",
336 		    cf->file_path);
337 
338 	rcs_branch_set(cf->file_rcs, branch);
339 
340 	if (rcs_sym_add(cf->file_rcs, vendor_tag, branch) == -1)
341 		fatal("import_new: failed to add vendor tag");
342 
343 	for (i = 0; i < tagcount; i++) {
344 		if (rcs_sym_add(cf->file_rcs, release_tags[i], brev) == -1)
345 			fatal("import_new: failed to add release tag");
346 	}
347 
348 	if (rcs_rev_add(cf->file_rcs, brev, logmsg, tstamp, NULL) == -1)
349 		fatal("import_new: failed to create first branch revision");
350 
351 	if (rcs_rev_add(cf->file_rcs, RCS_HEAD_REV, "Initial revision",
352 	    tstamp, NULL) == -1)
353 		fatal("import_new: failed to create first revision");
354 
355 	if ((rdp = rcs_findrev(cf->file_rcs, cf->file_rcs->rf_head)) == NULL)
356 		fatal("import_new: cannot find newly added revision");
357 
358 	brp = xmalloc(sizeof(*brp));
359 	brp->rb_num = rcsnum_alloc();
360 	rcsnum_cpy(brev, brp->rb_num, 0);
361 	TAILQ_INSERT_TAIL(&(rdp->rd_branches), brp, rb_list);
362 
363 	if (rcs_deltatext_set(cf->file_rcs,
364 	    cf->file_rcs->rf_head, bp) == -1)
365 		fatal("import_new: failed to set deltatext");
366 
367 	if (kflag)
368 		rcs_kwexp_set(cf->file_rcs, kflag);
369 
370 	rcs_write(cf->file_rcs);
371 	import_printf("N %s/%s\n", import_repository, cf->file_path);
372 
373 	rcsnum_free(branch);
374 	rcsnum_free(brev);
375 }
376 
377 static void
378 import_update(struct cvs_file *cf)
379 {
380 	int ret;
381 	BUF *b1, *b2, *d;
382 	char branch[CVS_REV_BUFSZ];
383 	RCSNUM *newrev, *rev, *brev;
384 
385 	cvs_log(LP_TRACE, "import_update(%s)", cf->file_path);
386 
387 	if (cf->file_rcs->rf_head == NULL)
388 		fatal("no head revision in RCS file for `%s'", cf->file_path);
389 
390 	if ((rev = rcs_translate_tag(import_branch, cf->file_rcs)) == NULL)
391 		fatal("import_update: could not translate tag `%s'",
392 		    import_branch);
393 
394 	if ((brev = rcsnum_parse(import_branch)) == NULL)
395 		fatal("import_update: rcsnum_parse failed");
396 
397 	b1 = rcs_rev_getbuf(cf->file_rcs, rev, RCS_KWEXP_NONE);
398 	b2 = buf_load_fd(cf->fd);
399 
400 	ret = buf_differ(b1, b2);
401 	buf_free(b1);
402 	buf_free(b2);
403 	if (ret == 0) {
404 		import_tag(cf, brev, rev);
405 		rcsnum_free(brev);
406 		if (cvs_noexec != 1)
407 			rcs_write(cf->file_rcs);
408 		import_printf("U %s/%s\n", import_repository, cf->file_path);
409 		return;
410 	}
411 
412 	if (cf->file_rcs->rf_branch != NULL)
413 		rcsnum_tostr(cf->file_rcs->rf_branch, branch, sizeof(branch));
414 
415 	if (cf->file_rcs->rf_branch == NULL || cf->in_attic == 1 ||
416 	    strcmp(branch, import_branch)) {
417 		import_conflicts++;
418 		import_printf("C %s/%s\n", import_repository, cf->file_path);
419 	} else {
420 		import_printf("U %s/%s\n", import_repository, cf->file_path);
421 	}
422 
423 	if (cvs_noexec == 1)
424 		return;
425 
426 	d = import_get_rcsdiff(cf, rev);
427 	newrev = rcsnum_inc(rev);
428 
429 	if (rcs_rev_add(cf->file_rcs, newrev, logmsg, -1, NULL) == -1)
430 		fatal("import_update: failed to add new revision");
431 
432 	if (rcs_deltatext_set(cf->file_rcs, newrev, d) == -1)
433 		fatal("import_update: failed to set deltatext");
434 
435 	import_tag(cf, brev, newrev);
436 
437 	if (kflag)
438 		rcs_kwexp_set(cf->file_rcs, kflag);
439 
440 	rcsnum_free(brev);
441 	rcs_write(cf->file_rcs);
442 }
443 
444 static void
445 import_tag(struct cvs_file *cf, RCSNUM *branch, RCSNUM *newrev)
446 {
447 	int i;
448 
449 	if (cvs_noexec != 1) {
450 		rcs_sym_add(cf->file_rcs, vendor_tag, branch);
451 
452 		for (i = 0; i < tagcount; i++)
453 			rcs_sym_add(cf->file_rcs, release_tags[i], newrev);
454 	}
455 }
456 
457 static BUF *
458 import_get_rcsdiff(struct cvs_file *cf, RCSNUM *rev)
459 {
460 	char *p1, *p2;
461 	BUF *b1, *b2;
462 	int fd1, fd2;
463 
464 	b2 = buf_alloc(128);
465 
466 	b1 = buf_load_fd(cf->fd);
467 
468 	(void)xasprintf(&p1, "%s/diff1.XXXXXXXXXX", cvs_tmpdir);
469 	fd1 = buf_write_stmp(b1, p1, NULL);
470 	buf_free(b1);
471 
472 	(void)xasprintf(&p2, "%s/diff2.XXXXXXXXXX", cvs_tmpdir);
473 	fd2 = rcs_rev_write_stmp(cf->file_rcs, rev, p2, RCS_KWEXP_NONE);
474 
475 	diff_format = D_RCSDIFF;
476 	if (diffreg(p2, p1, fd2, fd1, b2, D_FORCEASCII) == D_ERROR)
477 		fatal("import_get_rcsdiff: failed to get RCS patch");
478 
479 	close(fd1);
480 	close(fd2);
481 
482 	(void)unlink(p1);
483 	(void)unlink(p2);
484 
485 	xfree(p1);
486 	xfree(p2);
487 
488 	return (b2);
489 }
490