Annotation of src/usr.bin/cvs/import.c, Revision 1.45
1.45 ! joris 1: /* $OpenBSD$ */
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: #include "proto.h"
24:
1.45 ! joris 25: int cvs_import(int, char **);
! 26: void cvs_import_local(struct cvs_file *);
1.3 jfb 27:
1.45 ! joris 28: static void import_new(struct cvs_file *);
! 29: static void import_update(struct cvs_file *);
1.3 jfb 30:
1.45 ! joris 31: #define IMPORT_DEFAULT_BRANCH "1.1.1"
1.19 jfb 32:
1.45 ! joris 33: static char *import_branch = IMPORT_DEFAULT_BRANCH;
! 34: static char *logmsg = NULL;
! 35: static char *vendor_tag = NULL;
! 36: static char *release_tag = NULL;
1.19 jfb 37:
1.45 ! joris 38: char *import_repository = NULL;
1.3 jfb 39:
1.15 jfb 40: struct cvs_cmd cvs_cmd_import = {
41: CVS_OP_IMPORT, CVS_REQ_IMPORT, "import",
42: { "im", "imp" },
43: "Import sources into CVS, using vendor branches",
1.45 ! joris 44: "[-b vendor branch id] [-m message] repository vendor-tag release-tags",
! 45: "b:m:",
1.15 jfb 46: NULL,
1.45 ! joris 47: cvs_import
1.7 joris 48: };
1.1 krapht 49:
1.45 ! joris 50: int
! 51: cvs_import(int argc, char **argv)
1.1 krapht 52: {
1.45 ! joris 53: int ch, l;
! 54: char repo[MAXPATHLEN], *arg = ".";
! 55: struct cvs_recursion cr;
1.1 krapht 56:
1.45 ! joris 57: while ((ch = getopt(argc, argv, cvs_cmd_import.cmd_opts)) != -1) {
1.1 krapht 58: switch (ch) {
59: case 'b':
1.45 ! joris 60: import_branch = optarg;
1.1 krapht 61: break;
62: case 'm':
1.45 ! joris 63: logmsg = optarg;
1.1 krapht 64: break;
65: default:
1.45 ! joris 66: fatal("%s", cvs_cmd_import.cmd_synopsis);
! 67: break;
1.1 krapht 68: }
69: }
70:
71: argc -= optind;
72: argv += optind;
1.19 jfb 73:
1.45 ! joris 74: if (argc < 3)
! 75: fatal("%s", cvs_cmd_import.cmd_synopsis);
1.1 krapht 76:
1.45 ! joris 77: if (logmsg == NULL)
! 78: fatal("please specify a logmessage using -m for now");
1.15 jfb 79:
1.45 ! joris 80: import_repository = argv[0];
! 81: vendor_tag = argv[1];
! 82: release_tag = argv[2];
! 83:
! 84: l = snprintf(repo, sizeof(repo), "%s/%s", current_cvsroot->cr_dir,
! 85: import_repository);
! 86: if (l == -1 || l >= (int)sizeof(repo))
! 87: fatal("cvs_import: overflow");
! 88:
! 89: if (mkdir(repo, 0755) == -1 && errno != EEXIST)
! 90: fatal("cvs_import: %s: %s", repo, strerror(errno));
! 91:
! 92: cr.enterdir = NULL;
! 93: cr.leavedir = NULL;
! 94: cr.remote = NULL;
! 95: cr.local = cvs_import_local;
! 96: cr.flags = CR_RECURSE_DIRS;
! 97: cvs_file_run(1, &arg, &cr);
1.1 krapht 98:
1.7 joris 99: return (0);
100: }
1.1 krapht 101:
1.45 ! joris 102: void
! 103: cvs_import_local(struct cvs_file *cf)
1.7 joris 104: {
1.45 ! joris 105: int l;
! 106: int isnew;
! 107: struct stat st;
! 108: char repo[MAXPATHLEN];
1.15 jfb 109:
1.45 ! joris 110: cvs_log(LP_TRACE, "cvs_import_local(%s)", cf->file_path);
1.1 krapht 111:
1.45 ! joris 112: cvs_file_classify(cf, 0);
1.1 krapht 113:
1.45 ! joris 114: if (cf->file_type == CVS_DIR) {
! 115: if (!strcmp(cf->file_path, "."))
! 116: return;
1.19 jfb 117:
1.45 ! joris 118: if (verbosity > 1)
! 119: cvs_log(LP_NOTICE, "Importing %s", cf->file_path);
1.19 jfb 120:
121:
1.45 ! joris 122: if (mkdir(cf->file_rpath, 0755) == -1 && errno != EEXIST)
! 123: fatal("cvs_import_local: %s: %s", cf->file_rpath,
! 124: strerror(errno));
1.15 jfb 125:
1.45 ! joris 126: return;
1.15 jfb 127: }
128:
1.45 ! joris 129: isnew = 1;
! 130: l = snprintf(repo, sizeof(repo), "%s/%s/%s/%s%s",
! 131: current_cvsroot->cr_dir, cf->file_wd, CVS_PATH_ATTIC,
! 132: cf->file_name, RCS_FILE_EXT);
! 133: if (l == -1 || l >= (int)sizeof(repo))
! 134: fatal("import_new: overflow");
1.34 joris 135:
1.45 ! joris 136: if (cf->file_rcs != NULL || stat(repo, &st) != -1)
! 137: isnew = 0;
1.15 jfb 138:
1.45 ! joris 139: if (isnew == 1)
! 140: import_new(cf);
! 141: else
! 142: import_update(cf);
1.15 jfb 143: }
144:
1.45 ! joris 145: static void
! 146: import_new(struct cvs_file *cf)
1.15 jfb 147: {
1.45 ! joris 148: BUF *bp;
! 149: char *content;
! 150: struct rcs_branch *brp;
1.41 joris 151: struct rcs_delta *rdp;
1.45 ! joris 152: RCSNUM *branch, *brev;
! 153:
! 154: cvs_log(LP_TRACE, "import_new(%s)", cf->file_name);
! 155:
! 156: if ((branch = rcsnum_parse(import_branch)) == NULL)
! 157: fatal("import_new: failed to parse branch");
! 158:
! 159: if ((bp = cvs_buf_load(cf->file_path, BUF_AUTOEXT)) == NULL)
! 160: fatal("import_new: failed to load %s", cf->file_path);
! 161:
! 162: cvs_buf_putc(bp, '\0');
! 163: content = cvs_buf_release(bp);
1.15 jfb 164:
1.45 ! joris 165: if ((brev = rcsnum_brtorev(branch)) == NULL)
! 166: fatal("import_new: failed to get first branch revision");
1.24 xsa 167:
1.45 ! joris 168: cf->repo_fd = open(cf->file_rpath, O_CREAT|O_TRUNC|O_WRONLY);
! 169: if (cf->repo_fd < 0)
! 170: fatal("import_new: %s: %s", cf->file_rpath, strerror(errno));
1.1 krapht 171:
1.45 ! joris 172: cf->file_rcs = rcs_open(cf->file_rpath, cf->repo_fd, RCS_CREATE, 0444);
! 173: if (cf->file_rcs == NULL)
! 174: fatal("import_new: failed to create RCS file for %s",
! 175: cf->file_path);
1.1 krapht 176:
1.45 ! joris 177: rcs_branch_set(cf->file_rcs, branch);
1.18 jfb 178:
1.45 ! joris 179: if (rcs_sym_add(cf->file_rcs, vendor_tag, branch) == -1)
! 180: fatal("import_new: failed to add release tag");
1.18 jfb 181:
1.45 ! joris 182: if (rcs_sym_add(cf->file_rcs, release_tag, branch) == -1)
! 183: fatal("import_new: failed to add vendor tag");
1.17 jfb 184:
1.45 ! joris 185: if (rcs_rev_add(cf->file_rcs, brev, logmsg, -1, NULL) == -1)
! 186: fatal("import_new: failed to create first branch revision");
1.1 krapht 187:
1.45 ! joris 188: if (rcs_rev_add(cf->file_rcs, RCS_HEAD_REV, logmsg, -1, NULL) == -1)
! 189: fatal("import_new: failed to create first revision");
1.20 jfb 190:
1.45 ! joris 191: if ((rdp = rcs_findrev(cf->file_rcs, cf->file_rcs->rf_head)) == NULL)
! 192: fatal("import_new: cannot find newly added revision");
1.41 joris 193:
1.42 ray 194: brp = xmalloc(sizeof(*brp));
1.41 joris 195: brp->rb_num = rcsnum_alloc();
196: rcsnum_cpy(brev, brp->rb_num, 0);
197: TAILQ_INSERT_TAIL(&(rdp->rd_branches), brp, rb_list);
1.30 joris 198:
1.45 ! joris 199: if (rcs_deltatext_set(cf->file_rcs,
! 200: cf->file_rcs->rf_head, content) == -1)
! 201: fatal("import_new: failed to set deltatext");
1.30 joris 202:
1.45 ! joris 203: rcs_write(cf->file_rcs);
! 204: cvs_printf("N %s\n", cf->file_path);
1.30 joris 205:
1.45 ! joris 206: rcsnum_free(branch);
! 207: rcsnum_free(brev);
1.19 jfb 208: }
209:
1.45 ! joris 210: static void
! 211: import_update(struct cvs_file *cf)
1.19 jfb 212: {
1.45 ! joris 213: cvs_log(LP_TRACE, "import_update(%s)", cf->file_path);
1.1 krapht 214: }