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