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