[BACK]Return to import.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / cvs

Annotation of src/usr.bin/cvs/import.c, Revision 1.50

1.50    ! joris       1: /*     $OpenBSD: import.c,v 1.49 2006/06/11 17:50:11 joris 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: #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.47      joris      30: static void import_tag(struct cvs_file *, RCSNUM *, RCSNUM *);
                     31: static char *import_get_rcsdiff(struct cvs_file *, RCSNUM *);
1.3       jfb        32:
1.45      joris      33: #define IMPORT_DEFAULT_BRANCH  "1.1.1"
1.19      jfb        34:
1.45      joris      35: static char *import_branch = IMPORT_DEFAULT_BRANCH;
                     36: static char *logmsg = NULL;
                     37: static char *vendor_tag = NULL;
                     38: static char *release_tag = NULL;
1.19      jfb        39:
1.45      joris      40: char *import_repository = NULL;
1.47      joris      41: int import_conflicts = 0;
1.3       jfb        42:
1.15      jfb        43: struct cvs_cmd cvs_cmd_import = {
                     44:        CVS_OP_IMPORT, CVS_REQ_IMPORT, "import",
                     45:        { "im", "imp" },
                     46:        "Import sources into CVS, using vendor branches",
1.45      joris      47:        "[-b vendor branch id] [-m message] repository vendor-tag release-tags",
                     48:        "b:m:",
1.15      jfb        49:        NULL,
1.45      joris      50:        cvs_import
1.7       joris      51: };
1.1       krapht     52:
1.45      joris      53: int
                     54: cvs_import(int argc, char **argv)
1.1       krapht     55: {
1.45      joris      56:        int ch, l;
                     57:        char repo[MAXPATHLEN], *arg = ".";
                     58:        struct cvs_recursion cr;
1.1       krapht     59:
1.45      joris      60:        while ((ch = getopt(argc, argv, cvs_cmd_import.cmd_opts)) != -1) {
1.1       krapht     61:                switch (ch) {
                     62:                case 'b':
1.45      joris      63:                        import_branch = optarg;
1.1       krapht     64:                        break;
                     65:                case 'm':
1.45      joris      66:                        logmsg = optarg;
1.1       krapht     67:                        break;
                     68:                default:
1.45      joris      69:                        fatal("%s", cvs_cmd_import.cmd_synopsis);
                     70:                        break;
1.1       krapht     71:                }
                     72:        }
                     73:
                     74:        argc -= optind;
                     75:        argv += optind;
1.19      jfb        76:
1.45      joris      77:        if (argc < 3)
                     78:                fatal("%s", cvs_cmd_import.cmd_synopsis);
1.1       krapht     79:
1.45      joris      80:        if (logmsg == NULL)
                     81:                fatal("please specify a logmessage using -m for now");
1.15      jfb        82:
1.45      joris      83:        import_repository = argv[0];
                     84:        vendor_tag = argv[1];
                     85:        release_tag = argv[2];
                     86:
                     87:        l = snprintf(repo, sizeof(repo), "%s/%s", current_cvsroot->cr_dir,
                     88:            import_repository);
                     89:        if (l == -1 || l >= (int)sizeof(repo))
                     90:                fatal("cvs_import: overflow");
                     91:
1.48      joris      92:        if (cvs_noexec != 1) {
                     93:                if (mkdir(repo, 0755) == -1 && errno != EEXIST)
                     94:                        fatal("cvs_import: %s: %s", repo, strerror(errno));
                     95:        }
1.45      joris      96:
                     97:        cr.enterdir = NULL;
                     98:        cr.leavedir = NULL;
                     99:        cr.remote = NULL;
                    100:        cr.local = cvs_import_local;
                    101:        cr.flags = CR_RECURSE_DIRS;
                    102:        cvs_file_run(1, &arg, &cr);
1.1       krapht    103:
1.47      joris     104:        if (import_conflicts != 0) {
                    105:                cvs_printf("\n%d conflicts created by this import.\n\n",
                    106:                    import_conflicts);
                    107:                cvs_printf("Use the following command to help the merge:\n");
                    108:                cvs_printf("\topencvs checkout ");
                    109:                cvs_printf("-j%s:yesterday -j%s %s\n\n", vendor_tag,
                    110:                    vendor_tag, import_repository);
                    111:        } else {
                    112:                cvs_printf("\nNo conflicts created by this import.\n\n");
                    113:        }
                    114:
1.7       joris     115:        return (0);
                    116: }
1.1       krapht    117:
1.45      joris     118: void
                    119: cvs_import_local(struct cvs_file *cf)
1.7       joris     120: {
1.45      joris     121:        int l;
                    122:        int isnew;
                    123:        struct stat st;
                    124:        char repo[MAXPATHLEN];
1.15      jfb       125:
1.45      joris     126:        cvs_log(LP_TRACE, "cvs_import_local(%s)", cf->file_path);
1.1       krapht    127:
1.46      joris     128:        cvs_file_classify(cf, NULL, 0);
1.1       krapht    129:
1.45      joris     130:        if (cf->file_type == CVS_DIR) {
                    131:                if (!strcmp(cf->file_path, "."))
                    132:                        return;
1.19      jfb       133:
1.45      joris     134:                if (verbosity > 1)
                    135:                        cvs_log(LP_NOTICE, "Importing %s", cf->file_path);
1.19      jfb       136:
1.48      joris     137:                if (cvs_noexec == 1)
                    138:                        return;
1.19      jfb       139:
1.45      joris     140:                if (mkdir(cf->file_rpath, 0755) == -1 && errno != EEXIST)
                    141:                        fatal("cvs_import_local: %s: %s", cf->file_rpath,
                    142:                            strerror(errno));
1.15      jfb       143:
1.45      joris     144:                return;
1.15      jfb       145:        }
                    146:
1.45      joris     147:        isnew = 1;
                    148:        l = snprintf(repo, sizeof(repo), "%s/%s/%s/%s%s",
                    149:            current_cvsroot->cr_dir, cf->file_wd, CVS_PATH_ATTIC,
                    150:            cf->file_name, RCS_FILE_EXT);
                    151:        if (l == -1 || l >= (int)sizeof(repo))
                    152:                fatal("import_new: overflow");
1.34      joris     153:
1.45      joris     154:        if (cf->file_rcs != NULL || stat(repo, &st) != -1)
                    155:                isnew = 0;
1.15      jfb       156:
1.45      joris     157:        if (isnew == 1)
                    158:                import_new(cf);
                    159:        else
                    160:                import_update(cf);
1.15      jfb       161: }
                    162:
1.45      joris     163: static void
                    164: import_new(struct cvs_file *cf)
1.15      jfb       165: {
1.45      joris     166:        BUF *bp;
                    167:        char *content;
                    168:        struct rcs_branch *brp;
1.41      joris     169:        struct rcs_delta *rdp;
1.45      joris     170:        RCSNUM *branch, *brev;
                    171:
                    172:        cvs_log(LP_TRACE, "import_new(%s)", cf->file_name);
                    173:
1.48      joris     174:        if (cvs_noexec == 1) {
                    175:                cvs_printf("N %s/%s\n", import_repository, cf->file_path);
                    176:                return;
                    177:        }
                    178:
1.45      joris     179:        if ((branch = rcsnum_parse(import_branch)) == NULL)
                    180:                fatal("import_new: failed to parse branch");
                    181:
1.50    ! joris     182:        if ((bp = cvs_buf_load_fd(cf->fd, BUF_AUTOEXT)) == NULL)
1.45      joris     183:                fatal("import_new: failed to load %s", cf->file_path);
                    184:
                    185:        cvs_buf_putc(bp, '\0');
                    186:        content = cvs_buf_release(bp);
1.15      jfb       187:
1.45      joris     188:        if ((brev = rcsnum_brtorev(branch)) == NULL)
                    189:                fatal("import_new: failed to get first branch revision");
1.24      xsa       190:
1.45      joris     191:        cf->repo_fd = open(cf->file_rpath, O_CREAT|O_TRUNC|O_WRONLY);
                    192:        if (cf->repo_fd < 0)
                    193:                fatal("import_new: %s: %s", cf->file_rpath, strerror(errno));
1.1       krapht    194:
1.45      joris     195:        cf->file_rcs = rcs_open(cf->file_rpath, cf->repo_fd, RCS_CREATE, 0444);
                    196:        if (cf->file_rcs == NULL)
                    197:                fatal("import_new: failed to create RCS file for %s",
                    198:                    cf->file_path);
1.1       krapht    199:
1.45      joris     200:        rcs_branch_set(cf->file_rcs, branch);
1.18      jfb       201:
1.45      joris     202:        if (rcs_sym_add(cf->file_rcs, vendor_tag, branch) == -1)
                    203:                fatal("import_new: failed to add release tag");
1.18      jfb       204:
1.47      joris     205:        if (rcs_sym_add(cf->file_rcs, release_tag, brev) == -1)
1.45      joris     206:                fatal("import_new: failed to add vendor tag");
1.17      jfb       207:
1.45      joris     208:        if (rcs_rev_add(cf->file_rcs, brev, logmsg, -1, NULL) == -1)
                    209:                fatal("import_new: failed to create first branch revision");
1.1       krapht    210:
1.45      joris     211:        if (rcs_rev_add(cf->file_rcs, RCS_HEAD_REV, logmsg, -1, NULL) == -1)
                    212:                fatal("import_new: failed to create first revision");
1.20      jfb       213:
1.45      joris     214:        if ((rdp = rcs_findrev(cf->file_rcs, cf->file_rcs->rf_head)) == NULL)
                    215:                fatal("import_new: cannot find newly added revision");
1.41      joris     216:
1.42      ray       217:        brp = xmalloc(sizeof(*brp));
1.41      joris     218:        brp->rb_num = rcsnum_alloc();
                    219:        rcsnum_cpy(brev, brp->rb_num, 0);
                    220:        TAILQ_INSERT_TAIL(&(rdp->rd_branches), brp, rb_list);
1.30      joris     221:
1.45      joris     222:        if (rcs_deltatext_set(cf->file_rcs,
                    223:            cf->file_rcs->rf_head, content) == -1)
                    224:                fatal("import_new: failed to set deltatext");
1.30      joris     225:
1.45      joris     226:        rcs_write(cf->file_rcs);
1.47      joris     227:        cvs_printf("N %s/%s\n", import_repository, cf->file_path);
1.30      joris     228:
1.47      joris     229:        xfree(content);
1.45      joris     230:        rcsnum_free(branch);
                    231:        rcsnum_free(brev);
1.19      jfb       232: }
                    233:
1.45      joris     234: static void
                    235: import_update(struct cvs_file *cf)
1.19      jfb       236: {
1.47      joris     237:        BUF *b1, *b2;
1.49      joris     238:        char *d, branch[16];
1.47      joris     239:        RCSNUM *newrev, *rev, *brev;
                    240:
1.45      joris     241:        cvs_log(LP_TRACE, "import_update(%s)", cf->file_path);
1.47      joris     242:
                    243:        rev = rcs_translate_tag(import_branch, cf->file_rcs);
                    244:
                    245:        if ((brev = rcsnum_parse(import_branch)) == NULL)
                    246:                fatal("import_update: rcsnum_parse failed");
                    247:
                    248:        if (rev != NULL) {
                    249:                if ((b1 = rcs_getrev(cf->file_rcs, rev)) == NULL)
                    250:                        fatal("import_update: failed to grab revision");
                    251:
1.50    ! joris     252:                if ((b2 = cvs_buf_load_fd(cf->fd, BUF_AUTOEXT)) == NULL)
1.47      joris     253:                        fatal("import_update: failed to load %s",
                    254:                            cf->file_path);
                    255:
                    256:                if (cvs_buf_differ(b1, b2) == 0) {
                    257:                        import_tag(cf, brev, rev);
                    258:                        cvs_printf("U %s/%s\n", import_repository,
                    259:                            cf->file_path);
                    260:                        rcsnum_free(rev);
                    261:                        rcsnum_free(brev);
                    262:                        rcs_write(cf->file_rcs);
                    263:                        return;
                    264:                }
                    265:        }
                    266:
                    267:        if (cf->file_rcs->rf_branch != NULL)
                    268:                rcsnum_tostr(cf->file_rcs->rf_branch, branch, sizeof(branch));
                    269:
                    270:        if (rev != NULL) {
                    271:                d = import_get_rcsdiff(cf, rev);
                    272:                newrev = rcsnum_inc(rev);
                    273:        } else {
                    274:                d = import_get_rcsdiff(cf, rcs_head_get(cf->file_rcs));
                    275:                newrev = rcsnum_brtorev(brev);
                    276:        }
                    277:
                    278:        if (rcs_rev_add(cf->file_rcs, newrev, logmsg, -1, NULL) == -1)
                    279:                fatal("import_update: failed to add new revision");
                    280:
                    281:        if (rcs_deltatext_set(cf->file_rcs, newrev, d) == -1)
                    282:                fatal("import_update: failed to set deltatext");
1.48      joris     283:
1.47      joris     284:        xfree(d);
                    285:
                    286:        import_tag(cf, brev, newrev);
                    287:
                    288:        if (cf->file_rcs->rf_branch == NULL || cf->file_rcs->rf_inattic == 1 ||
                    289:            strcmp(branch, import_branch)) {
                    290:                import_conflicts++;
                    291:                cvs_printf("C %s/%s\n", import_repository, cf->file_path);
                    292:        } else {
                    293:                cvs_printf("U %s/%s\n", import_repository, cf->file_path);
                    294:        }
                    295:
                    296:        if (rev != NULL)
                    297:                rcsnum_free(rev);
                    298:        rcsnum_free(brev);
                    299:
                    300:        rcs_write(cf->file_rcs);
                    301: }
                    302:
                    303: static void
                    304: import_tag(struct cvs_file *cf, RCSNUM *branch, RCSNUM *newrev)
                    305: {
                    306:        char b[16];
                    307:
1.48      joris     308:        if (cvs_noexec != 1) {
                    309:                rcsnum_tostr(branch, b, sizeof(b));
                    310:                rcs_sym_add(cf->file_rcs, vendor_tag, branch);
1.47      joris     311:
1.48      joris     312:                rcsnum_tostr(newrev, b, sizeof(b));
                    313:                rcs_sym_add(cf->file_rcs, release_tag, newrev);
                    314:        }
1.47      joris     315: }
                    316:
                    317: static char *
                    318: import_get_rcsdiff(struct cvs_file *cf, RCSNUM *rev)
                    319: {
                    320:        char *delta, *p1, *p2;
                    321:        BUF *b1, *b2, *b3;
                    322:
1.50    ! joris     323:        if ((b1 = cvs_buf_load_fd(cf->fd, BUF_AUTOEXT)) == NULL)
1.47      joris     324:                fatal("import_get_rcsdiff: failed loading %s", cf->file_path);
                    325:
                    326:        if ((b2 = rcs_getrev(cf->file_rcs, rev)) == NULL)
                    327:                fatal("import_get_rcsdiff: failed loading revision");
                    328:
                    329:        b3 = cvs_buf_alloc(128, BUF_AUTOEXT);
                    330:
1.48      joris     331:        if (cvs_noexec != 1) {
                    332:                (void)xasprintf(&p1, "%s/diff1.XXXXXXXXXX", cvs_tmpdir);
                    333:                cvs_buf_write_stmp(b1, p1, 0600, NULL);
                    334:                cvs_buf_free(b1);
                    335:
                    336:                (void)xasprintf(&p2, "%s/diff2.XXXXXXXXXX", cvs_tmpdir);
                    337:                cvs_buf_write_stmp(b2, p2, 0600, NULL);
                    338:                cvs_buf_free(b2);
                    339:
                    340:                diff_format = D_RCSDIFF;
                    341:                if (cvs_diffreg(p2, p1, b3) == D_ERROR)
                    342:                        fatal("import_get_rcsdiff: failed to get RCS patch");
                    343:        }
1.47      joris     344:
                    345:        cvs_buf_putc(b3, '\0');
                    346:        delta = cvs_buf_release(b3);
                    347:        return (delta);
1.1       krapht    348: }