Annotation of src/usr.bin/cvs/import.c, Revision 1.58
1.58 ! xsa 1: /* $OpenBSD: import.c,v 1.57 2007/01/07 02:47:57 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.56 xsa 57: int ch;
1.45 joris 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)
1.57 joris 85: logmsg = cvs_logmsg_create(NULL, NULL, NULL);
86:
87: if (logmsg == NULL)
88: fatal("This shouldnt happen, honestly!");
1.15 jfb 89:
1.45 joris 90: import_repository = argv[0];
91: vendor_tag = argv[1];
92: release_tag = argv[2];
93:
1.56 xsa 94: if (cvs_path_cat(current_cvsroot->cr_dir, import_repository,
95: repo, sizeof(repo)) >= sizeof(repo))
96: fatal("cvs_import: truncation");
1.45 joris 97:
1.48 joris 98: if (cvs_noexec != 1) {
99: if (mkdir(repo, 0755) == -1 && errno != EEXIST)
100: fatal("cvs_import: %s: %s", repo, strerror(errno));
101: }
1.45 joris 102:
103: cr.enterdir = NULL;
104: cr.leavedir = NULL;
1.53 joris 105: cr.fileproc = cvs_import_local;
1.45 joris 106: cr.flags = CR_RECURSE_DIRS;
107: cvs_file_run(1, &arg, &cr);
1.1 krapht 108:
1.47 joris 109: if (import_conflicts != 0) {
110: cvs_printf("\n%d conflicts created by this import.\n\n",
111: import_conflicts);
112: cvs_printf("Use the following command to help the merge:\n");
113: cvs_printf("\topencvs checkout ");
114: cvs_printf("-j%s:yesterday -j%s %s\n\n", vendor_tag,
115: vendor_tag, import_repository);
116: } else {
117: cvs_printf("\nNo conflicts created by this import.\n\n");
118: }
119:
1.7 joris 120: return (0);
121: }
1.1 krapht 122:
1.45 joris 123: void
124: cvs_import_local(struct cvs_file *cf)
1.7 joris 125: {
1.45 joris 126: int l;
127: int isnew;
128: struct stat st;
129: char repo[MAXPATHLEN];
1.15 jfb 130:
1.45 joris 131: cvs_log(LP_TRACE, "cvs_import_local(%s)", cf->file_path);
1.1 krapht 132:
1.46 joris 133: cvs_file_classify(cf, NULL, 0);
1.1 krapht 134:
1.45 joris 135: if (cf->file_type == CVS_DIR) {
136: if (!strcmp(cf->file_path, "."))
137: return;
1.19 jfb 138:
1.45 joris 139: if (verbosity > 1)
140: cvs_log(LP_NOTICE, "Importing %s", cf->file_path);
1.19 jfb 141:
1.48 joris 142: if (cvs_noexec == 1)
143: return;
1.19 jfb 144:
1.45 joris 145: if (mkdir(cf->file_rpath, 0755) == -1 && errno != EEXIST)
146: fatal("cvs_import_local: %s: %s", cf->file_rpath,
147: strerror(errno));
1.15 jfb 148:
1.45 joris 149: return;
1.15 jfb 150: }
151:
1.45 joris 152: isnew = 1;
153: l = snprintf(repo, sizeof(repo), "%s/%s/%s/%s%s",
154: current_cvsroot->cr_dir, cf->file_wd, CVS_PATH_ATTIC,
155: cf->file_name, RCS_FILE_EXT);
156: if (l == -1 || l >= (int)sizeof(repo))
157: fatal("import_new: overflow");
1.34 joris 158:
1.45 joris 159: if (cf->file_rcs != NULL || stat(repo, &st) != -1)
160: isnew = 0;
1.15 jfb 161:
1.45 joris 162: if (isnew == 1)
163: import_new(cf);
164: else
165: import_update(cf);
1.15 jfb 166: }
167:
1.45 joris 168: static void
169: import_new(struct cvs_file *cf)
1.15 jfb 170: {
1.45 joris 171: BUF *bp;
172: char *content;
1.51 xsa 173: time_t tstamp;
174: struct stat st;
1.45 joris 175: struct rcs_branch *brp;
1.41 joris 176: struct rcs_delta *rdp;
1.45 joris 177: RCSNUM *branch, *brev;
178:
1.51 xsa 179: tstamp = -1;
180:
1.45 joris 181: cvs_log(LP_TRACE, "import_new(%s)", cf->file_name);
182:
1.48 joris 183: if (cvs_noexec == 1) {
184: cvs_printf("N %s/%s\n", import_repository, cf->file_path);
185: return;
186: }
187:
1.51 xsa 188: if (dflag == 1) {
189: if (fstat(cf->fd, &st) == -1)
190: fatal("import_new: %s", strerror(errno));
191:
192: tstamp = st.st_mtime;
193: }
194:
1.45 joris 195: if ((branch = rcsnum_parse(import_branch)) == NULL)
196: fatal("import_new: failed to parse branch");
197:
1.50 joris 198: if ((bp = cvs_buf_load_fd(cf->fd, BUF_AUTOEXT)) == NULL)
1.45 joris 199: fatal("import_new: failed to load %s", cf->file_path);
200:
201: cvs_buf_putc(bp, '\0');
202: content = cvs_buf_release(bp);
1.15 jfb 203:
1.45 joris 204: if ((brev = rcsnum_brtorev(branch)) == NULL)
205: fatal("import_new: failed to get first branch revision");
1.24 xsa 206:
1.45 joris 207: cf->repo_fd = open(cf->file_rpath, O_CREAT|O_TRUNC|O_WRONLY);
208: if (cf->repo_fd < 0)
209: fatal("import_new: %s: %s", cf->file_rpath, strerror(errno));
1.1 krapht 210:
1.45 joris 211: cf->file_rcs = rcs_open(cf->file_rpath, cf->repo_fd, RCS_CREATE, 0444);
212: if (cf->file_rcs == NULL)
213: fatal("import_new: failed to create RCS file for %s",
214: cf->file_path);
1.1 krapht 215:
1.45 joris 216: rcs_branch_set(cf->file_rcs, branch);
1.18 jfb 217:
1.45 joris 218: if (rcs_sym_add(cf->file_rcs, vendor_tag, branch) == -1)
219: fatal("import_new: failed to add release tag");
1.18 jfb 220:
1.47 joris 221: if (rcs_sym_add(cf->file_rcs, release_tag, brev) == -1)
1.45 joris 222: fatal("import_new: failed to add vendor tag");
1.17 jfb 223:
1.51 xsa 224: if (rcs_rev_add(cf->file_rcs, brev, logmsg, tstamp, NULL) == -1)
1.45 joris 225: fatal("import_new: failed to create first branch revision");
1.1 krapht 226:
1.51 xsa 227: if (rcs_rev_add(cf->file_rcs, RCS_HEAD_REV, logmsg, tstamp, NULL) == -1)
1.45 joris 228: fatal("import_new: failed to create first revision");
1.20 jfb 229:
1.45 joris 230: if ((rdp = rcs_findrev(cf->file_rcs, cf->file_rcs->rf_head)) == NULL)
231: fatal("import_new: cannot find newly added revision");
1.41 joris 232:
1.42 ray 233: brp = xmalloc(sizeof(*brp));
1.41 joris 234: brp->rb_num = rcsnum_alloc();
235: rcsnum_cpy(brev, brp->rb_num, 0);
236: TAILQ_INSERT_TAIL(&(rdp->rd_branches), brp, rb_list);
1.30 joris 237:
1.45 joris 238: if (rcs_deltatext_set(cf->file_rcs,
239: cf->file_rcs->rf_head, content) == -1)
240: fatal("import_new: failed to set deltatext");
1.30 joris 241:
1.45 joris 242: rcs_write(cf->file_rcs);
1.47 joris 243: cvs_printf("N %s/%s\n", import_repository, cf->file_path);
1.30 joris 244:
1.47 joris 245: xfree(content);
1.45 joris 246: rcsnum_free(branch);
247: rcsnum_free(brev);
1.19 jfb 248: }
249:
1.45 joris 250: static void
251: import_update(struct cvs_file *cf)
1.19 jfb 252: {
1.47 joris 253: BUF *b1, *b2;
1.49 joris 254: char *d, branch[16];
1.47 joris 255: RCSNUM *newrev, *rev, *brev;
256:
1.45 joris 257: cvs_log(LP_TRACE, "import_update(%s)", cf->file_path);
1.47 joris 258:
259: rev = rcs_translate_tag(import_branch, cf->file_rcs);
260:
261: if ((brev = rcsnum_parse(import_branch)) == NULL)
262: fatal("import_update: rcsnum_parse failed");
263:
264: if (rev != NULL) {
265: if ((b1 = rcs_getrev(cf->file_rcs, rev)) == NULL)
266: fatal("import_update: failed to grab revision");
267:
1.50 joris 268: if ((b2 = cvs_buf_load_fd(cf->fd, BUF_AUTOEXT)) == NULL)
1.47 joris 269: fatal("import_update: failed to load %s",
270: cf->file_path);
271:
272: if (cvs_buf_differ(b1, b2) == 0) {
273: import_tag(cf, brev, rev);
274: cvs_printf("U %s/%s\n", import_repository,
275: cf->file_path);
276: rcsnum_free(rev);
277: rcsnum_free(brev);
278: rcs_write(cf->file_rcs);
279: return;
280: }
281: }
282:
283: if (cf->file_rcs->rf_branch != NULL)
284: rcsnum_tostr(cf->file_rcs->rf_branch, branch, sizeof(branch));
285:
286: if (rev != NULL) {
287: d = import_get_rcsdiff(cf, rev);
288: newrev = rcsnum_inc(rev);
289: } else {
290: d = import_get_rcsdiff(cf, rcs_head_get(cf->file_rcs));
291: newrev = rcsnum_brtorev(brev);
292: }
293:
294: if (rcs_rev_add(cf->file_rcs, newrev, logmsg, -1, NULL) == -1)
295: fatal("import_update: failed to add new revision");
296:
297: if (rcs_deltatext_set(cf->file_rcs, newrev, d) == -1)
298: fatal("import_update: failed to set deltatext");
1.48 joris 299:
1.47 joris 300: xfree(d);
301:
302: import_tag(cf, brev, newrev);
303:
304: if (cf->file_rcs->rf_branch == NULL || cf->file_rcs->rf_inattic == 1 ||
305: strcmp(branch, import_branch)) {
306: import_conflicts++;
307: cvs_printf("C %s/%s\n", import_repository, cf->file_path);
308: } else {
309: cvs_printf("U %s/%s\n", import_repository, cf->file_path);
310: }
311:
312: if (rev != NULL)
313: rcsnum_free(rev);
314: rcsnum_free(brev);
315:
316: rcs_write(cf->file_rcs);
317: }
318:
319: static void
320: import_tag(struct cvs_file *cf, RCSNUM *branch, RCSNUM *newrev)
321: {
322: char b[16];
323:
1.48 joris 324: if (cvs_noexec != 1) {
325: rcsnum_tostr(branch, b, sizeof(b));
326: rcs_sym_add(cf->file_rcs, vendor_tag, branch);
1.47 joris 327:
1.48 joris 328: rcsnum_tostr(newrev, b, sizeof(b));
329: rcs_sym_add(cf->file_rcs, release_tag, newrev);
330: }
1.47 joris 331: }
332:
333: static char *
334: import_get_rcsdiff(struct cvs_file *cf, RCSNUM *rev)
335: {
336: char *delta, *p1, *p2;
337: BUF *b1, *b2, *b3;
338:
1.50 joris 339: if ((b1 = cvs_buf_load_fd(cf->fd, BUF_AUTOEXT)) == NULL)
1.47 joris 340: fatal("import_get_rcsdiff: failed loading %s", cf->file_path);
341:
342: if ((b2 = rcs_getrev(cf->file_rcs, rev)) == NULL)
343: fatal("import_get_rcsdiff: failed loading revision");
344:
345: b3 = cvs_buf_alloc(128, BUF_AUTOEXT);
346:
1.48 joris 347: if (cvs_noexec != 1) {
348: (void)xasprintf(&p1, "%s/diff1.XXXXXXXXXX", cvs_tmpdir);
1.54 ray 349: cvs_buf_write_stmp(b1, p1, NULL);
1.48 joris 350: cvs_buf_free(b1);
351:
352: (void)xasprintf(&p2, "%s/diff2.XXXXXXXXXX", cvs_tmpdir);
1.54 ray 353: cvs_buf_write_stmp(b2, p2, NULL);
1.48 joris 354: cvs_buf_free(b2);
355:
356: diff_format = D_RCSDIFF;
357: if (cvs_diffreg(p2, p1, b3) == D_ERROR)
358: fatal("import_get_rcsdiff: failed to get RCS patch");
1.55 xsa 359:
360: if (p1 != NULL)
361: xfree(p1);
362: if (p2 != NULL)
363: xfree(p2);
1.48 joris 364: }
1.47 joris 365:
366: cvs_buf_putc(b3, '\0');
367: delta = cvs_buf_release(b3);
1.55 xsa 368:
369: if (b1 != NULL)
370: cvs_buf_free(b1);
371: if (b2 != NULL)
372: cvs_buf_free(b2);
373: if (b3 != NULL)
374: cvs_buf_free(b3);
375:
1.47 joris 376: return (delta);
1.1 krapht 377: }