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