Annotation of src/usr.bin/cvs/import.c, Revision 1.105
1.105 ! nicm 1: /* $OpenBSD: import.c,v 1.104 2015/01/16 06:40:07 deraadt 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.64 joris 130: if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
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.93 joris 214: }
215:
1.45 joris 216: void
217: cvs_import_local(struct cvs_file *cf)
1.7 joris 218: {
1.45 joris 219: int isnew;
220: struct stat st;
1.104 deraadt 221: char repo[PATH_MAX];
1.15 jfb 222:
1.45 joris 223: cvs_log(LP_TRACE, "cvs_import_local(%s)", cf->file_path);
1.1 krapht 224:
1.77 joris 225: cvs_file_classify(cf, cvs_directory_tag);
1.1 krapht 226:
1.45 joris 227: if (cf->file_type == CVS_DIR) {
228: if (!strcmp(cf->file_path, "."))
229: return;
1.19 jfb 230:
1.45 joris 231: if (verbosity > 1)
232: cvs_log(LP_NOTICE, "Importing %s", cf->file_path);
1.19 jfb 233:
1.48 joris 234: if (cvs_noexec == 1)
235: return;
1.19 jfb 236:
1.45 joris 237: if (mkdir(cf->file_rpath, 0755) == -1 && errno != EEXIST)
238: fatal("cvs_import_local: %s: %s", cf->file_rpath,
239: strerror(errno));
1.15 jfb 240:
1.45 joris 241: return;
1.15 jfb 242: }
243:
1.45 joris 244: isnew = 1;
1.67 xsa 245: (void)xsnprintf(repo, sizeof(repo), "%s/%s/%s/%s%s",
1.45 joris 246: current_cvsroot->cr_dir, cf->file_wd, CVS_PATH_ATTIC,
247: cf->file_name, RCS_FILE_EXT);
1.34 joris 248:
1.45 joris 249: if (cf->file_rcs != NULL || stat(repo, &st) != -1)
250: isnew = 0;
1.15 jfb 251:
1.45 joris 252: if (isnew == 1)
253: import_new(cf);
254: else
255: import_update(cf);
1.15 jfb 256: }
257:
1.45 joris 258: static void
1.93 joris 259: import_loginfo(char *repo)
260: {
261: int i;
1.104 deraadt 262: char pwd[PATH_MAX];
1.93 joris 263:
264: if (getcwd(pwd, sizeof(pwd)) == NULL)
265: fatal("Can't get working directory");
266:
1.102 ray 267: logbuf = buf_alloc(1024);
1.93 joris 268: cvs_trigger_loginfo_header(logbuf, repo);
269:
1.102 ray 270: buf_puts(logbuf, "Log Message:\n");
271: buf_puts(logbuf, logmsg);
1.93 joris 272: if (logmsg[0] != '\0' && logmsg[strlen(logmsg) - 1] != '\n')
1.102 ray 273: buf_putc(logbuf, '\n');
274: buf_putc(logbuf, '\n');
1.93 joris 275:
1.102 ray 276: buf_puts(logbuf, "Status:\n\n");
1.93 joris 277:
1.102 ray 278: buf_puts(logbuf, "Vendor Tag:\t");
279: buf_puts(logbuf, vendor_tag);
280: buf_putc(logbuf, '\n');
281: buf_puts(logbuf, "Release Tags:\t");
1.93 joris 282:
283: for (i = 0; i < tagcount ; i++) {
1.102 ray 284: buf_puts(logbuf, "\t\t");
285: buf_puts(logbuf, release_tags[i]);
286: buf_putc(logbuf, '\n');
1.93 joris 287: }
1.102 ray 288: buf_putc(logbuf, '\n');
289: buf_putc(logbuf, '\n');
1.93 joris 290: }
291:
292: static void
1.45 joris 293: import_new(struct cvs_file *cf)
1.15 jfb 294: {
1.91 joris 295: int i;
1.45 joris 296: BUF *bp;
1.95 joris 297: mode_t mode;
1.51 xsa 298: time_t tstamp;
299: struct stat st;
1.45 joris 300: struct rcs_branch *brp;
1.41 joris 301: struct rcs_delta *rdp;
1.45 joris 302: RCSNUM *branch, *brev;
303:
1.51 xsa 304: tstamp = -1;
305:
1.45 joris 306: cvs_log(LP_TRACE, "import_new(%s)", cf->file_name);
307:
1.48 joris 308: if (cvs_noexec == 1) {
1.93 joris 309: import_printf("N %s/%s\n", import_repository, cf->file_path);
1.48 joris 310: return;
311: }
312:
1.95 joris 313: if (fstat(cf->fd, &st) == -1)
314: fatal("import_new: %s", strerror(errno));
1.51 xsa 315:
1.95 joris 316: mode = st.st_mode;
317:
318: if (dflag == 1)
1.51 xsa 319: tstamp = st.st_mtime;
320:
1.45 joris 321: if ((branch = rcsnum_parse(import_branch)) == NULL)
322: fatal("import_new: failed to parse branch");
323:
1.102 ray 324: bp = buf_load_fd(cf->fd);
1.45 joris 325:
326: if ((brev = rcsnum_brtorev(branch)) == NULL)
327: fatal("import_new: failed to get first branch revision");
1.24 xsa 328:
1.97 joris 329: cf->repo_fd = open(cf->file_rpath, O_CREAT | O_RDONLY);
1.45 joris 330: if (cf->repo_fd < 0)
331: fatal("import_new: %s: %s", cf->file_rpath, strerror(errno));
1.1 krapht 332:
1.95 joris 333: cf->file_rcs = rcs_open(cf->file_rpath, cf->repo_fd, RCS_CREATE,
334: (mode & ~(S_IWUSR | S_IWGRP | S_IWOTH)));
1.45 joris 335: if (cf->file_rcs == NULL)
336: fatal("import_new: failed to create RCS file for %s",
337: cf->file_path);
1.1 krapht 338:
1.45 joris 339: rcs_branch_set(cf->file_rcs, branch);
1.18 jfb 340:
1.45 joris 341: if (rcs_sym_add(cf->file_rcs, vendor_tag, branch) == -1)
1.88 xsa 342: fatal("import_new: failed to add vendor tag");
1.18 jfb 343:
1.91 joris 344: for (i = 0; i < tagcount; i++) {
345: if (rcs_sym_add(cf->file_rcs, release_tags[i], brev) == -1)
346: fatal("import_new: failed to add release tag");
347: }
1.17 jfb 348:
1.51 xsa 349: if (rcs_rev_add(cf->file_rcs, brev, logmsg, tstamp, NULL) == -1)
1.45 joris 350: fatal("import_new: failed to create first branch revision");
1.1 krapht 351:
1.76 tobias 352: if (rcs_rev_add(cf->file_rcs, RCS_HEAD_REV, "Initial revision",
353: tstamp, NULL) == -1)
1.45 joris 354: fatal("import_new: failed to create first revision");
1.20 jfb 355:
1.45 joris 356: if ((rdp = rcs_findrev(cf->file_rcs, cf->file_rcs->rf_head)) == NULL)
357: fatal("import_new: cannot find newly added revision");
1.41 joris 358:
1.42 ray 359: brp = xmalloc(sizeof(*brp));
1.41 joris 360: brp->rb_num = rcsnum_alloc();
361: rcsnum_cpy(brev, brp->rb_num, 0);
362: TAILQ_INSERT_TAIL(&(rdp->rd_branches), brp, rb_list);
1.30 joris 363:
1.45 joris 364: if (rcs_deltatext_set(cf->file_rcs,
1.60 joris 365: cf->file_rcs->rf_head, bp) == -1)
1.45 joris 366: fatal("import_new: failed to set deltatext");
1.30 joris 367:
1.82 tobias 368: if (kflag)
1.68 xsa 369: rcs_kwexp_set(cf->file_rcs, kflag);
370:
1.45 joris 371: rcs_write(cf->file_rcs);
1.93 joris 372: import_printf("N %s/%s\n", import_repository, cf->file_path);
1.30 joris 373:
1.45 joris 374: rcsnum_free(branch);
375: rcsnum_free(brev);
1.19 jfb 376: }
377:
1.45 joris 378: static void
379: import_update(struct cvs_file *cf)
1.19 jfb 380: {
1.64 joris 381: int ret;
1.62 joris 382: BUF *b1, *b2, *d;
1.74 xsa 383: char branch[CVS_REV_BUFSZ];
1.81 joris 384: RCSNUM *newrev, *rev, *brev;
1.47 joris 385:
1.45 joris 386: cvs_log(LP_TRACE, "import_update(%s)", cf->file_path);
1.96 tobias 387:
388: if (cf->file_rcs->rf_head == NULL)
389: fatal("no head revision in RCS file for `%s'", cf->file_path);
1.47 joris 390:
1.66 niallo 391: if ((rev = rcs_translate_tag(import_branch, cf->file_rcs)) == NULL)
1.84 tobias 392: fatal("import_update: could not translate tag `%s'",
393: import_branch);
1.47 joris 394:
395: if ((brev = rcsnum_parse(import_branch)) == NULL)
396: fatal("import_update: rcsnum_parse failed");
397:
1.84 tobias 398: b1 = rcs_rev_getbuf(cf->file_rcs, rev, RCS_KWEXP_NONE);
1.102 ray 399: b2 = buf_load_fd(cf->fd);
1.81 joris 400:
1.102 ray 401: ret = buf_differ(b1, b2);
402: buf_free(b1);
403: buf_free(b2);
1.81 joris 404: if (ret == 0) {
405: import_tag(cf, brev, rev);
406: rcsnum_free(brev);
1.86 joris 407: if (cvs_noexec != 1)
408: rcs_write(cf->file_rcs);
1.93 joris 409: import_printf("U %s/%s\n", import_repository, cf->file_path);
1.81 joris 410: return;
1.47 joris 411: }
412:
413: if (cf->file_rcs->rf_branch != NULL)
414: rcsnum_tostr(cf->file_rcs->rf_branch, branch, sizeof(branch));
415:
1.86 joris 416: if (cf->file_rcs->rf_branch == NULL || cf->in_attic == 1 ||
417: strcmp(branch, import_branch)) {
418: import_conflicts++;
1.93 joris 419: import_printf("C %s/%s\n", import_repository, cf->file_path);
1.86 joris 420: } else {
1.93 joris 421: import_printf("U %s/%s\n", import_repository, cf->file_path);
1.86 joris 422: }
423:
424: if (cvs_noexec == 1)
425: return;
426:
1.81 joris 427: d = import_get_rcsdiff(cf, rev);
428: newrev = rcsnum_inc(rev);
1.47 joris 429:
430: if (rcs_rev_add(cf->file_rcs, newrev, logmsg, -1, NULL) == -1)
431: fatal("import_update: failed to add new revision");
432:
433: if (rcs_deltatext_set(cf->file_rcs, newrev, d) == -1)
434: fatal("import_update: failed to set deltatext");
1.48 joris 435:
1.47 joris 436: import_tag(cf, brev, newrev);
1.68 xsa 437:
1.82 tobias 438: if (kflag)
1.68 xsa 439: rcs_kwexp_set(cf->file_rcs, kflag);
1.47 joris 440:
441: rcsnum_free(brev);
442: rcs_write(cf->file_rcs);
443: }
444:
445: static void
446: import_tag(struct cvs_file *cf, RCSNUM *branch, RCSNUM *newrev)
447: {
1.91 joris 448: int i;
1.47 joris 449:
1.48 joris 450: if (cvs_noexec != 1) {
451: rcs_sym_add(cf->file_rcs, vendor_tag, branch);
1.47 joris 452:
1.91 joris 453: for (i = 0; i < tagcount; i++)
454: rcs_sym_add(cf->file_rcs, release_tags[i], newrev);
1.48 joris 455: }
1.47 joris 456: }
457:
1.62 joris 458: static BUF *
1.47 joris 459: import_get_rcsdiff(struct cvs_file *cf, RCSNUM *rev)
460: {
1.62 joris 461: char *p1, *p2;
1.61 niallo 462: BUF *b1, *b2;
1.85 joris 463: int fd1, fd2;
1.47 joris 464:
1.102 ray 465: b2 = buf_alloc(128);
1.47 joris 466:
1.102 ray 467: b1 = buf_load_fd(cf->fd);
1.64 joris 468:
1.87 joris 469: (void)xasprintf(&p1, "%s/diff1.XXXXXXXXXX", cvs_tmpdir);
1.102 ray 470: fd1 = buf_write_stmp(b1, p1, NULL);
471: buf_free(b1);
1.48 joris 472:
1.87 joris 473: (void)xasprintf(&p2, "%s/diff2.XXXXXXXXXX", cvs_tmpdir);
474: fd2 = rcs_rev_write_stmp(cf->file_rcs, rev, p2, RCS_KWEXP_NONE);
1.48 joris 475:
1.87 joris 476: diff_format = D_RCSDIFF;
1.100 ray 477: if (diffreg(p2, p1, fd2, fd1, b2, D_FORCEASCII) == D_ERROR)
1.87 joris 478: fatal("import_get_rcsdiff: failed to get RCS patch");
1.85 joris 479:
1.87 joris 480: close(fd1);
481: close(fd2);
1.59 xsa 482:
1.87 joris 483: (void)unlink(p1);
484: (void)unlink(p2);
1.55 xsa 485:
1.105 ! nicm 486: free(p1);
! 487: free(p2);
1.55 xsa 488:
1.62 joris 489: return (b2);
1.1 krapht 490: }