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

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