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