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

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