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