[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.84

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