Annotation of src/usr.bin/cvs/import.c, Revision 1.54
1.54 ! ray 1: /* $OpenBSD: import.c,v 1.53 2006/06/19 05:05:17 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.35 xsa 18: #include "includes.h"
1.1 krapht 19:
1.23 xsa 20: #include "cvs.h"
1.45 joris 21: #include "diff.h"
1.1 krapht 22: #include "log.h"
23:
1.45 joris 24: int cvs_import(int, char **);
25: void cvs_import_local(struct cvs_file *);
1.3 jfb 26:
1.45 joris 27: static void import_new(struct cvs_file *);
28: static void import_update(struct cvs_file *);
1.47 joris 29: static void import_tag(struct cvs_file *, RCSNUM *, RCSNUM *);
30: static char *import_get_rcsdiff(struct cvs_file *, RCSNUM *);
1.3 jfb 31:
1.45 joris 32: #define IMPORT_DEFAULT_BRANCH "1.1.1"
1.19 jfb 33:
1.45 joris 34: static char *import_branch = IMPORT_DEFAULT_BRANCH;
35: static char *logmsg = NULL;
36: static char *vendor_tag = NULL;
37: static char *release_tag = NULL;
1.19 jfb 38:
1.51 xsa 39: static int dflag = 0;
40:
1.45 joris 41: char *import_repository = NULL;
1.47 joris 42: int import_conflicts = 0;
1.3 jfb 43:
1.15 jfb 44: struct cvs_cmd cvs_cmd_import = {
1.52 joris 45: CVS_OP_IMPORT, 0, "import",
1.15 jfb 46: { "im", "imp" },
47: "Import sources into CVS, using vendor branches",
1.51 xsa 48: "[-d] [-b branch] [-m message] repository vendor-tag release-tags",
49: "b:dm:",
1.15 jfb 50: NULL,
1.45 joris 51: cvs_import
1.7 joris 52: };
1.1 krapht 53:
1.45 joris 54: int
55: cvs_import(int argc, char **argv)
1.1 krapht 56: {
1.45 joris 57: int ch, l;
58: char repo[MAXPATHLEN], *arg = ".";
59: struct cvs_recursion cr;
1.1 krapht 60:
1.45 joris 61: while ((ch = getopt(argc, argv, cvs_cmd_import.cmd_opts)) != -1) {
1.1 krapht 62: switch (ch) {
63: case 'b':
1.45 joris 64: import_branch = optarg;
1.1 krapht 65: break;
1.51 xsa 66: case 'd':
67: dflag = 1;
68: break;
1.1 krapht 69: case 'm':
1.45 joris 70: logmsg = optarg;
1.1 krapht 71: break;
72: default:
1.45 joris 73: fatal("%s", cvs_cmd_import.cmd_synopsis);
74: break;
1.1 krapht 75: }
76: }
77:
78: argc -= optind;
79: argv += optind;
1.19 jfb 80:
1.45 joris 81: if (argc < 3)
82: fatal("%s", cvs_cmd_import.cmd_synopsis);
1.1 krapht 83:
1.45 joris 84: if (logmsg == NULL)
85: fatal("please specify a logmessage using -m for now");
1.15 jfb 86:
1.45 joris 87: import_repository = argv[0];
88: vendor_tag = argv[1];
89: release_tag = argv[2];
90:
91: l = snprintf(repo, sizeof(repo), "%s/%s", current_cvsroot->cr_dir,
92: import_repository);
93: if (l == -1 || l >= (int)sizeof(repo))
94: fatal("cvs_import: overflow");
95:
1.48 joris 96: if (cvs_noexec != 1) {
97: if (mkdir(repo, 0755) == -1 && errno != EEXIST)
98: fatal("cvs_import: %s: %s", repo, strerror(errno));
99: }
1.45 joris 100:
101: cr.enterdir = NULL;
102: cr.leavedir = NULL;
1.53 joris 103: cr.fileproc = cvs_import_local;
1.45 joris 104: cr.flags = CR_RECURSE_DIRS;
105: cvs_file_run(1, &arg, &cr);
1.1 krapht 106:
1.47 joris 107: if (import_conflicts != 0) {
108: cvs_printf("\n%d conflicts created by this import.\n\n",
109: import_conflicts);
110: cvs_printf("Use the following command to help the merge:\n");
111: cvs_printf("\topencvs checkout ");
112: cvs_printf("-j%s:yesterday -j%s %s\n\n", vendor_tag,
113: vendor_tag, import_repository);
114: } else {
115: cvs_printf("\nNo conflicts created by this import.\n\n");
116: }
117:
1.7 joris 118: return (0);
119: }
1.1 krapht 120:
1.45 joris 121: void
122: cvs_import_local(struct cvs_file *cf)
1.7 joris 123: {
1.45 joris 124: int l;
125: int isnew;
126: struct stat st;
127: char repo[MAXPATHLEN];
1.15 jfb 128:
1.45 joris 129: cvs_log(LP_TRACE, "cvs_import_local(%s)", cf->file_path);
1.1 krapht 130:
1.46 joris 131: cvs_file_classify(cf, NULL, 0);
1.1 krapht 132:
1.45 joris 133: if (cf->file_type == CVS_DIR) {
134: if (!strcmp(cf->file_path, "."))
135: return;
1.19 jfb 136:
1.45 joris 137: if (verbosity > 1)
138: cvs_log(LP_NOTICE, "Importing %s", cf->file_path);
1.19 jfb 139:
1.48 joris 140: if (cvs_noexec == 1)
141: return;
1.19 jfb 142:
1.45 joris 143: if (mkdir(cf->file_rpath, 0755) == -1 && errno != EEXIST)
144: fatal("cvs_import_local: %s: %s", cf->file_rpath,
145: strerror(errno));
1.15 jfb 146:
1.45 joris 147: return;
1.15 jfb 148: }
149:
1.45 joris 150: isnew = 1;
151: l = snprintf(repo, sizeof(repo), "%s/%s/%s/%s%s",
152: current_cvsroot->cr_dir, cf->file_wd, CVS_PATH_ATTIC,
153: cf->file_name, RCS_FILE_EXT);
154: if (l == -1 || l >= (int)sizeof(repo))
155: fatal("import_new: overflow");
1.34 joris 156:
1.45 joris 157: if (cf->file_rcs != NULL || stat(repo, &st) != -1)
158: isnew = 0;
1.15 jfb 159:
1.45 joris 160: if (isnew == 1)
161: import_new(cf);
162: else
163: import_update(cf);
1.15 jfb 164: }
165:
1.45 joris 166: static void
167: import_new(struct cvs_file *cf)
1.15 jfb 168: {
1.45 joris 169: BUF *bp;
170: char *content;
1.51 xsa 171: time_t tstamp;
172: struct stat st;
1.45 joris 173: struct rcs_branch *brp;
1.41 joris 174: struct rcs_delta *rdp;
1.45 joris 175: RCSNUM *branch, *brev;
176:
1.51 xsa 177: tstamp = -1;
178:
1.45 joris 179: cvs_log(LP_TRACE, "import_new(%s)", cf->file_name);
180:
1.48 joris 181: if (cvs_noexec == 1) {
182: cvs_printf("N %s/%s\n", import_repository, cf->file_path);
183: return;
184: }
185:
1.51 xsa 186: if (dflag == 1) {
187: if (fstat(cf->fd, &st) == -1)
188: fatal("import_new: %s", strerror(errno));
189:
190: tstamp = st.st_mtime;
191: }
192:
1.45 joris 193: if ((branch = rcsnum_parse(import_branch)) == NULL)
194: fatal("import_new: failed to parse branch");
195:
1.50 joris 196: if ((bp = cvs_buf_load_fd(cf->fd, BUF_AUTOEXT)) == NULL)
1.45 joris 197: fatal("import_new: failed to load %s", cf->file_path);
198:
199: cvs_buf_putc(bp, '\0');
200: content = cvs_buf_release(bp);
1.15 jfb 201:
1.45 joris 202: if ((brev = rcsnum_brtorev(branch)) == NULL)
203: fatal("import_new: failed to get first branch revision");
1.24 xsa 204:
1.45 joris 205: cf->repo_fd = open(cf->file_rpath, O_CREAT|O_TRUNC|O_WRONLY);
206: if (cf->repo_fd < 0)
207: fatal("import_new: %s: %s", cf->file_rpath, strerror(errno));
1.1 krapht 208:
1.45 joris 209: cf->file_rcs = rcs_open(cf->file_rpath, cf->repo_fd, RCS_CREATE, 0444);
210: if (cf->file_rcs == NULL)
211: fatal("import_new: failed to create RCS file for %s",
212: cf->file_path);
1.1 krapht 213:
1.45 joris 214: rcs_branch_set(cf->file_rcs, branch);
1.18 jfb 215:
1.45 joris 216: if (rcs_sym_add(cf->file_rcs, vendor_tag, branch) == -1)
217: fatal("import_new: failed to add release tag");
1.18 jfb 218:
1.47 joris 219: if (rcs_sym_add(cf->file_rcs, release_tag, brev) == -1)
1.45 joris 220: fatal("import_new: failed to add vendor tag");
1.17 jfb 221:
1.51 xsa 222: if (rcs_rev_add(cf->file_rcs, brev, logmsg, tstamp, NULL) == -1)
1.45 joris 223: fatal("import_new: failed to create first branch revision");
1.1 krapht 224:
1.51 xsa 225: if (rcs_rev_add(cf->file_rcs, RCS_HEAD_REV, logmsg, tstamp, NULL) == -1)
1.45 joris 226: fatal("import_new: failed to create first revision");
1.20 jfb 227:
1.45 joris 228: if ((rdp = rcs_findrev(cf->file_rcs, cf->file_rcs->rf_head)) == NULL)
229: fatal("import_new: cannot find newly added revision");
1.41 joris 230:
1.42 ray 231: brp = xmalloc(sizeof(*brp));
1.41 joris 232: brp->rb_num = rcsnum_alloc();
233: rcsnum_cpy(brev, brp->rb_num, 0);
234: TAILQ_INSERT_TAIL(&(rdp->rd_branches), brp, rb_list);
1.30 joris 235:
1.45 joris 236: if (rcs_deltatext_set(cf->file_rcs,
237: cf->file_rcs->rf_head, content) == -1)
238: fatal("import_new: failed to set deltatext");
1.30 joris 239:
1.45 joris 240: rcs_write(cf->file_rcs);
1.47 joris 241: cvs_printf("N %s/%s\n", import_repository, cf->file_path);
1.30 joris 242:
1.47 joris 243: xfree(content);
1.45 joris 244: rcsnum_free(branch);
245: rcsnum_free(brev);
1.19 jfb 246: }
247:
1.45 joris 248: static void
249: import_update(struct cvs_file *cf)
1.19 jfb 250: {
1.47 joris 251: BUF *b1, *b2;
1.49 joris 252: char *d, branch[16];
1.47 joris 253: RCSNUM *newrev, *rev, *brev;
254:
1.45 joris 255: cvs_log(LP_TRACE, "import_update(%s)", cf->file_path);
1.47 joris 256:
257: rev = rcs_translate_tag(import_branch, cf->file_rcs);
258:
259: if ((brev = rcsnum_parse(import_branch)) == NULL)
260: fatal("import_update: rcsnum_parse failed");
261:
262: if (rev != NULL) {
263: if ((b1 = rcs_getrev(cf->file_rcs, rev)) == NULL)
264: fatal("import_update: failed to grab revision");
265:
1.50 joris 266: if ((b2 = cvs_buf_load_fd(cf->fd, BUF_AUTOEXT)) == NULL)
1.47 joris 267: fatal("import_update: failed to load %s",
268: cf->file_path);
269:
270: if (cvs_buf_differ(b1, b2) == 0) {
271: import_tag(cf, brev, rev);
272: cvs_printf("U %s/%s\n", import_repository,
273: cf->file_path);
274: rcsnum_free(rev);
275: rcsnum_free(brev);
276: rcs_write(cf->file_rcs);
277: return;
278: }
279: }
280:
281: if (cf->file_rcs->rf_branch != NULL)
282: rcsnum_tostr(cf->file_rcs->rf_branch, branch, sizeof(branch));
283:
284: if (rev != NULL) {
285: d = import_get_rcsdiff(cf, rev);
286: newrev = rcsnum_inc(rev);
287: } else {
288: d = import_get_rcsdiff(cf, rcs_head_get(cf->file_rcs));
289: newrev = rcsnum_brtorev(brev);
290: }
291:
292: if (rcs_rev_add(cf->file_rcs, newrev, logmsg, -1, NULL) == -1)
293: fatal("import_update: failed to add new revision");
294:
295: if (rcs_deltatext_set(cf->file_rcs, newrev, d) == -1)
296: fatal("import_update: failed to set deltatext");
1.48 joris 297:
1.47 joris 298: xfree(d);
299:
300: import_tag(cf, brev, newrev);
301:
302: if (cf->file_rcs->rf_branch == NULL || cf->file_rcs->rf_inattic == 1 ||
303: strcmp(branch, import_branch)) {
304: import_conflicts++;
305: cvs_printf("C %s/%s\n", import_repository, cf->file_path);
306: } else {
307: cvs_printf("U %s/%s\n", import_repository, cf->file_path);
308: }
309:
310: if (rev != NULL)
311: rcsnum_free(rev);
312: rcsnum_free(brev);
313:
314: rcs_write(cf->file_rcs);
315: }
316:
317: static void
318: import_tag(struct cvs_file *cf, RCSNUM *branch, RCSNUM *newrev)
319: {
320: char b[16];
321:
1.48 joris 322: if (cvs_noexec != 1) {
323: rcsnum_tostr(branch, b, sizeof(b));
324: rcs_sym_add(cf->file_rcs, vendor_tag, branch);
1.47 joris 325:
1.48 joris 326: rcsnum_tostr(newrev, b, sizeof(b));
327: rcs_sym_add(cf->file_rcs, release_tag, newrev);
328: }
1.47 joris 329: }
330:
331: static char *
332: import_get_rcsdiff(struct cvs_file *cf, RCSNUM *rev)
333: {
334: char *delta, *p1, *p2;
335: BUF *b1, *b2, *b3;
336:
1.50 joris 337: if ((b1 = cvs_buf_load_fd(cf->fd, BUF_AUTOEXT)) == NULL)
1.47 joris 338: fatal("import_get_rcsdiff: failed loading %s", cf->file_path);
339:
340: if ((b2 = rcs_getrev(cf->file_rcs, rev)) == NULL)
341: fatal("import_get_rcsdiff: failed loading revision");
342:
343: b3 = cvs_buf_alloc(128, BUF_AUTOEXT);
344:
1.48 joris 345: if (cvs_noexec != 1) {
346: (void)xasprintf(&p1, "%s/diff1.XXXXXXXXXX", cvs_tmpdir);
1.54 ! ray 347: cvs_buf_write_stmp(b1, p1, NULL);
1.48 joris 348: cvs_buf_free(b1);
349:
350: (void)xasprintf(&p2, "%s/diff2.XXXXXXXXXX", cvs_tmpdir);
1.54 ! ray 351: cvs_buf_write_stmp(b2, p2, NULL);
1.48 joris 352: cvs_buf_free(b2);
353:
354: diff_format = D_RCSDIFF;
355: if (cvs_diffreg(p2, p1, b3) == D_ERROR)
356: fatal("import_get_rcsdiff: failed to get RCS patch");
357: }
1.47 joris 358:
359: cvs_buf_putc(b3, '\0');
360: delta = cvs_buf_release(b3);
361: return (delta);
1.1 krapht 362: }