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