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