Annotation of src/usr.bin/cvs/import.c, Revision 1.62
1.62 ! joris 1: /* $OpenBSD: import.c,v 1.61 2007/01/12 23:32:01 niallo 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 *);
1.62 ! joris 30: static BUF *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;
1.51 xsa 172: time_t tstamp;
173: struct stat st;
1.45 joris 174: struct rcs_branch *brp;
1.41 joris 175: struct rcs_delta *rdp;
1.45 joris 176: RCSNUM *branch, *brev;
177:
1.51 xsa 178: tstamp = -1;
179:
1.45 joris 180: cvs_log(LP_TRACE, "import_new(%s)", cf->file_name);
181:
1.48 joris 182: if (cvs_noexec == 1) {
183: cvs_printf("N %s/%s\n", import_repository, cf->file_path);
184: return;
185: }
186:
1.51 xsa 187: if (dflag == 1) {
188: if (fstat(cf->fd, &st) == -1)
189: fatal("import_new: %s", strerror(errno));
190:
191: tstamp = st.st_mtime;
192: }
193:
1.45 joris 194: if ((branch = rcsnum_parse(import_branch)) == NULL)
195: fatal("import_new: failed to parse branch");
196:
1.50 joris 197: if ((bp = cvs_buf_load_fd(cf->fd, BUF_AUTOEXT)) == NULL)
1.45 joris 198: fatal("import_new: failed to load %s", cf->file_path);
199:
200: cvs_buf_putc(bp, '\0');
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,
1.60 joris 237: cf->file_rcs->rf_head, bp) == -1)
1.45 joris 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.45 joris 243: rcsnum_free(branch);
244: rcsnum_free(brev);
1.19 jfb 245: }
246:
1.45 joris 247: static void
248: import_update(struct cvs_file *cf)
1.19 jfb 249: {
1.62 ! joris 250: BUF *b1, *b2, *d;
! 251: char branch[16];
1.47 joris 252: RCSNUM *newrev, *rev, *brev;
253:
1.45 joris 254: cvs_log(LP_TRACE, "import_update(%s)", cf->file_path);
1.47 joris 255:
256: rev = rcs_translate_tag(import_branch, cf->file_rcs);
257:
258: if ((brev = rcsnum_parse(import_branch)) == NULL)
259: fatal("import_update: rcsnum_parse failed");
260:
261: if (rev != NULL) {
1.61 niallo 262: if ((b1 = rcs_rev_getbuf(cf->file_rcs, rev)) == NULL)
1.47 joris 263: fatal("import_update: failed to grab revision");
264:
1.50 joris 265: if ((b2 = cvs_buf_load_fd(cf->fd, BUF_AUTOEXT)) == NULL)
1.47 joris 266: fatal("import_update: failed to load %s",
267: cf->file_path);
268:
269: if (cvs_buf_differ(b1, b2) == 0) {
270: import_tag(cf, brev, rev);
271: cvs_printf("U %s/%s\n", import_repository,
272: cf->file_path);
273: rcsnum_free(rev);
274: rcsnum_free(brev);
275: rcs_write(cf->file_rcs);
276: return;
277: }
278: }
279:
280: if (cf->file_rcs->rf_branch != NULL)
281: rcsnum_tostr(cf->file_rcs->rf_branch, branch, sizeof(branch));
282:
283: if (rev != NULL) {
284: d = import_get_rcsdiff(cf, rev);
285: newrev = rcsnum_inc(rev);
286: } else {
287: d = import_get_rcsdiff(cf, rcs_head_get(cf->file_rcs));
288: newrev = rcsnum_brtorev(brev);
289: }
290:
291: if (rcs_rev_add(cf->file_rcs, newrev, logmsg, -1, NULL) == -1)
292: fatal("import_update: failed to add new revision");
293:
294: if (rcs_deltatext_set(cf->file_rcs, newrev, d) == -1)
295: fatal("import_update: failed to set deltatext");
1.48 joris 296:
1.62 ! joris 297: cvs_buf_free(d);
1.47 joris 298: import_tag(cf, brev, newrev);
299:
300: if (cf->file_rcs->rf_branch == NULL || cf->file_rcs->rf_inattic == 1 ||
301: strcmp(branch, import_branch)) {
302: import_conflicts++;
303: cvs_printf("C %s/%s\n", import_repository, cf->file_path);
304: } else {
305: cvs_printf("U %s/%s\n", import_repository, cf->file_path);
306: }
307:
308: if (rev != NULL)
309: rcsnum_free(rev);
310: rcsnum_free(brev);
311:
312: rcs_write(cf->file_rcs);
313: }
314:
315: static void
316: import_tag(struct cvs_file *cf, RCSNUM *branch, RCSNUM *newrev)
317: {
318: char b[16];
319:
1.48 joris 320: if (cvs_noexec != 1) {
321: rcsnum_tostr(branch, b, sizeof(b));
322: rcs_sym_add(cf->file_rcs, vendor_tag, branch);
1.47 joris 323:
1.48 joris 324: rcsnum_tostr(newrev, b, sizeof(b));
325: rcs_sym_add(cf->file_rcs, release_tag, newrev);
326: }
1.47 joris 327: }
328:
1.62 ! joris 329: static BUF *
1.47 joris 330: import_get_rcsdiff(struct cvs_file *cf, RCSNUM *rev)
331: {
1.62 ! joris 332: char *p1, *p2;
1.61 niallo 333: BUF *b1, *b2;
1.47 joris 334:
1.50 joris 335: if ((b1 = cvs_buf_load_fd(cf->fd, BUF_AUTOEXT)) == NULL)
1.47 joris 336: fatal("import_get_rcsdiff: failed loading %s", cf->file_path);
337:
1.61 niallo 338: b2 = cvs_buf_alloc(128, BUF_AUTOEXT);
1.47 joris 339:
1.48 joris 340: if (cvs_noexec != 1) {
341: (void)xasprintf(&p1, "%s/diff1.XXXXXXXXXX", cvs_tmpdir);
1.54 ray 342: cvs_buf_write_stmp(b1, p1, NULL);
1.48 joris 343: cvs_buf_free(b1);
344:
345: (void)xasprintf(&p2, "%s/diff2.XXXXXXXXXX", cvs_tmpdir);
1.61 niallo 346: rcs_rev_write_stmp(cf->file_rcs, rev, p2, 0);
1.48 joris 347:
348: diff_format = D_RCSDIFF;
1.61 niallo 349: if (cvs_diffreg(p2, p1, b2) == D_ERROR)
1.48 joris 350: fatal("import_get_rcsdiff: failed to get RCS patch");
1.59 xsa 351:
352: (void)unlink(p1);
353: (void)unlink(p2);
1.55 xsa 354:
355: if (p1 != NULL)
356: xfree(p1);
357: if (p2 != NULL)
358: xfree(p2);
1.48 joris 359: }
1.47 joris 360:
1.55 xsa 361: if (b1 != NULL)
362: cvs_buf_free(b1);
363:
1.62 ! joris 364: return (b2);
1.1 krapht 365: }