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

Annotation of src/usr.bin/cvs/tag.c, Revision 1.79

1.79    ! joris       1: /*     $OpenBSD: tag.c,v 1.78 2009/03/22 18:41:25 tobias Exp $ */
1.1       jfb         2: /*
1.45      xsa         3:  * Copyright (c) 2006 Xavier Santolaria <xsa@openbsd.org>
1.1       jfb         4:  *
1.45      xsa         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       jfb         8:  *
1.45      xsa         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       jfb        16:  */
                     17:
1.60      tobias     18: #include <errno.h>
                     19: #include <string.h>
1.55      otto       20: #include <unistd.h>
1.1       jfb        21:
                     22: #include "cvs.h"
1.50      xsa        23: #include "remote.h"
1.1       jfb        24:
1.52      xsa        25: #define T_CHECK_UPTODATE       0x01
                     26: #define T_DELETE               0x02
                     27: #define T_FORCE_MOVE           0x04
1.59      tobias     28: #define T_BRANCH               0x08
1.52      xsa        29:
1.74      joris      30: void   cvs_tag_check_files(struct cvs_file *);
1.45      xsa        31: void   cvs_tag_local(struct cvs_file *);
                     32:
                     33: static int tag_del(struct cvs_file *);
                     34: static int tag_add(struct cvs_file *);
                     35:
1.74      joris      36: struct file_info_list  files_info;
                     37:
1.52      xsa        38: static int      runflags = 0;
1.45      xsa        39: static char    *tag = NULL;
                     40: static char    *tag_date = NULL;
                     41: static char    *tag_name = NULL;
                     42: static char    *tag_oldname = NULL;
1.7       joris      43:
1.60      tobias     44: struct cvs_cmd cvs_cmd_rtag = {
1.71      joris      45:        CVS_OP_RTAG, CVS_LOCK_REPO, "rtag",
1.60      tobias     46:        { "rt", "rfreeze" },
                     47:        "Add a symbolic tag to a module",
1.73      reyk       48:        "[-bcdFflR] [-D date | -r rev] symbolic_tag module ...",
1.60      tobias     49:        "bcD:dFflRr:",
                     50:        NULL,
                     51:        cvs_tag
                     52: };
                     53:
1.17      jfb        54: struct cvs_cmd cvs_cmd_tag = {
1.71      joris      55:        CVS_OP_TAG, CVS_USE_WDIR | CVS_LOCK_REPO, "tag",
1.17      jfb        56:        { "ta", "freeze" },
                     57:        "Add a symbolic tag to checked out version of files",
1.73      reyk       58:        "[-bcdFflR] [-D date | -r rev] symbolic_tag [file ...]",
1.17      jfb        59:        "bcD:dFflRr:",
                     60:        NULL,
1.45      xsa        61:        cvs_tag
1.17      jfb        62: };
                     63:
1.45      xsa        64: int
                     65: cvs_tag(int argc, char **argv)
1.1       jfb        66: {
1.60      tobias     67:        int ch, flags, i;
1.74      joris      68:        char repo[MAXPATHLEN];
1.49      reyk       69:        char *arg = ".";
1.45      xsa        70:        struct cvs_recursion cr;
1.74      joris      71:        struct trigger_list *line_list;
1.1       jfb        72:
1.45      xsa        73:        flags = CR_RECURSE_DIRS;
1.1       jfb        74:
1.65      tobias     75:        while ((ch = getopt(argc, argv, cvs_cmdop == CVS_OP_TAG ?
                     76:            cvs_cmd_tag.cmd_opts : cvs_cmd_rtag.cmd_opts)) != -1) {
1.1       jfb        77:                switch (ch) {
1.59      tobias     78:                case 'b':
                     79:                        runflags |= T_BRANCH;
                     80:                        break;
1.52      xsa        81:                case 'c':
                     82:                        runflags |= T_CHECK_UPTODATE;
                     83:                        break;
1.45      xsa        84:                case 'D':
                     85:                        tag_date = optarg;
1.1       jfb        86:                        break;
                     87:                case 'd':
1.52      xsa        88:                        runflags |= T_DELETE;
1.3       jfb        89:                        break;
1.46      xsa        90:                case 'F':
1.52      xsa        91:                        runflags |= T_FORCE_MOVE;
1.46      xsa        92:                        break;
1.1       jfb        93:                case 'l':
1.45      xsa        94:                        flags &= ~CR_RECURSE_DIRS;
1.17      jfb        95:                        break;
                     96:                case 'R':
1.62      tobias     97:                        flags |= CR_RECURSE_DIRS;
1.1       jfb        98:                        break;
                     99:                case 'r':
1.14      jfb       100:                        tag_oldname = optarg;
1.1       jfb       101:                        break;
                    102:                default:
1.70      tobias    103:                        fatal("%s", cvs_cmdop == CVS_OP_TAG ?
                    104:                            cvs_cmd_tag.cmd_synopsis :
                    105:                            cvs_cmd_rtag.cmd_synopsis);
1.1       jfb       106:                }
                    107:        }
                    108:
                    109:        argc -= optind;
                    110:        argv += optind;
                    111:
1.60      tobias    112:        if (cvs_cmdop == CVS_OP_RTAG) {
1.63      tobias    113:                flags |= CR_REPO;
                    114:
1.60      tobias    115:                if (argc < 2)
                    116:                        fatal("%s", cvs_cmd_rtag.cmd_synopsis);
1.64      xsa       117:
1.66      joris     118:                for (i = 1; i < argc; i++) {
                    119:                        if (argv[i][0] == '/')
                    120:                                fatal("Absolute path name is invalid: %s",
                    121:                                    argv[i]);
                    122:                }
1.60      tobias    123:         } else if (cvs_cmdop == CVS_OP_TAG && argc == 0)
1.45      xsa       124:                fatal("%s", cvs_cmd_tag.cmd_synopsis);
                    125:
                    126:        tag_name = argv[0];
                    127:        argc--;
                    128:        argv++;
1.15      jfb       129:
1.38      xsa       130:        if (!rcs_sym_check(tag_name))
                    131:                fatal("tag `%s' must not contain the characters `%s'",
1.15      jfb       132:                    tag_name, RCS_SYM_INVALCHAR);
1.1       jfb       133:
1.45      xsa       134:        if (tag_oldname != NULL) {
1.52      xsa       135:                if (runflags & T_DELETE)
1.45      xsa       136:                        tag_oldname = NULL;
                    137:                else
                    138:                        tag = tag_oldname;
                    139:        }
                    140:
                    141:        if (tag_date != NULL) {
1.52      xsa       142:                if (runflags & T_DELETE)
1.45      xsa       143:                        tag_date = NULL;
                    144:                else
                    145:                        tag = tag_date;
                    146:        }
                    147:
                    148:        if (tag_oldname != NULL && tag_date != NULL)
                    149:                fatal("-r and -D options are mutually exclusive");
                    150:
                    151:        cr.enterdir = NULL;
                    152:        cr.leavedir = NULL;
1.50      xsa       153:
                    154:        if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
1.53      joris     155:                cvs_client_connect_to_server();
1.50      xsa       156:                cr.fileproc = cvs_client_sendfile;
                    157:
1.59      tobias    158:                if (runflags & T_BRANCH)
                    159:                        cvs_client_send_request("Argument -b");
                    160:
1.52      xsa       161:                if (runflags & T_CHECK_UPTODATE)
                    162:                        cvs_client_send_request("Argument -c");
                    163:
                    164:                if (runflags & T_DELETE)
1.50      xsa       165:                        cvs_client_send_request("Argument -d");
                    166:
1.52      xsa       167:                if (runflags & T_FORCE_MOVE)
1.50      xsa       168:                        cvs_client_send_request("Argument -F");
                    169:
                    170:                if (!(flags & CR_RECURSE_DIRS))
                    171:                        cvs_client_send_request("Argument -l");
1.76      tobias    172:
                    173:                if (tag_date != NULL)
                    174:                        cvs_client_send_request("Argument -D%s", tag_date);
1.50      xsa       175:
                    176:                if (tag_oldname != NULL)
                    177:                        cvs_client_send_request("Argument -r%s", tag_oldname);
                    178:
                    179:                cvs_client_send_request("Argument %s", tag_name);
                    180:        } else {
1.60      tobias    181:                if (cvs_cmdop == CVS_OP_RTAG &&
                    182:                    chdir(current_cvsroot->cr_dir) == -1)
                    183:                        fatal("cvs_tag: %s", strerror(errno));
                    184:
1.50      xsa       185:        }
                    186:
1.45      xsa       187:        cr.flags = flags;
1.1       jfb       188:
1.74      joris     189:        cvs_get_repository_name(".", repo, MAXPATHLEN);
                    190:        line_list = cvs_trigger_getlines(CVS_PATH_TAGINFO, repo);
                    191:        if (line_list != NULL) {
                    192:                TAILQ_INIT(&files_info);
                    193:                cr.fileproc = cvs_tag_check_files;
                    194:                if (argc > 0)
                    195:                        cvs_file_run(argc, argv, &cr);
                    196:                else
                    197:                        cvs_file_run(1, &arg, &cr);
                    198:
                    199:                if (cvs_trigger_handle(CVS_TRIGGER_TAGINFO, repo, NULL,
                    200:                    line_list, &files_info)) {
                    201:                        cvs_log(LP_ERR, "Pre-tag check failed");
                    202:                        cvs_trigger_freelist(line_list);
                    203:                        goto bad;
                    204:                }
                    205:                cvs_trigger_freelist(line_list);
                    206:        }
                    207:
                    208:        cr.fileproc = cvs_tag_local;
                    209:
1.60      tobias    210:        if (cvs_cmdop == CVS_OP_TAG ||
1.61      tobias    211:            current_cvsroot->cr_method == CVS_METHOD_LOCAL) {
1.60      tobias    212:                if (argc > 0)
                    213:                        cvs_file_run(argc, argv, &cr);
                    214:                else
                    215:                        cvs_file_run(1, &arg, &cr);
                    216:        }
1.50      xsa       217:
                    218:        if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
                    219:                cvs_client_send_files(argv, argc);
                    220:                cvs_client_senddir(".");
1.60      tobias    221:
                    222:                cvs_client_send_request((cvs_cmdop == CVS_OP_RTAG) ?
                    223:                    "rtag" : "tag");
                    224:
1.50      xsa       225:                cvs_client_get_responses();
                    226:        }
1.3       jfb       227:
1.74      joris     228: bad:
1.79    ! joris     229:        if (line_list != NULL)
        !           230:                cvs_trigger_freeinfo(&files_info);
        !           231:
1.7       joris     232:        return (0);
1.74      joris     233: }
                    234:
                    235: void
                    236: cvs_tag_check_files(struct cvs_file *cf)
                    237: {
                    238:        RCSNUM *srev = NULL, *rev = NULL;
                    239:        char rbuf[CVS_REV_BUFSZ];
                    240:        struct file_info *fi;
                    241:
                    242:        cvs_log(LP_TRACE, "cvs_tag_check_files(%s)", cf->file_path);
                    243:
                    244:        cvs_file_classify(cf, tag);
                    245:
                    246:        if (cf->file_type == CVS_DIR)
                    247:                return;
                    248:
                    249:        if (runflags & T_CHECK_UPTODATE) {
                    250:                if (cf->file_status != FILE_UPTODATE &&
                    251:                    cf->file_status != FILE_CHECKOUT &&
                    252:                    cf->file_status != FILE_PATCH) {
                    253:                        return;
                    254:                }
                    255:        }
                    256:
                    257:        switch (cf->file_status) {
                    258:        case FILE_ADDED:
                    259:        case FILE_REMOVED:
                    260:                return;
                    261:        default:
                    262:                break;
                    263:        }
                    264:
                    265:        if (cvs_cmdop == CVS_OP_TAG) {
                    266:                if (cf->file_ent == NULL)
                    267:                        return;
                    268:                srev = cf->file_ent->ce_rev;
                    269:        } else
                    270:                srev = cf->file_rcsrev;
                    271:
                    272:        rcsnum_tostr(srev, rbuf, sizeof(rbuf));
1.77      tobias    273:        fi = xcalloc(1, sizeof(*fi));
                    274:        fi->nrevstr = xstrdup(rbuf);
1.74      joris     275:        fi->file_path = xstrdup(cf->file_path);
                    276:
                    277:        if (tag_oldname != NULL)
                    278:                fi->tag_old = xstrdup(tag_oldname);
                    279:        else if (tag_date != NULL)
                    280:                fi->tag_old = xstrdup(tag_date);
                    281:
                    282:        if ((rev = rcs_sym_getrev(cf->file_rcs, tag_name)) != NULL) {
                    283:                if (!rcsnum_differ(srev, rev))
                    284:                        goto bad;
                    285:                rcsnum_tostr(rev, rbuf, sizeof(rbuf));
                    286:                fi->crevstr = xstrdup(rbuf);
                    287:        } else if (runflags & T_DELETE)
                    288:                goto bad;
                    289:
                    290:        fi->tag_new = xstrdup(tag_name);
                    291:
                    292:        if (runflags & T_BRANCH)
                    293:                fi->tag_type = 'T';
                    294:        else if (runflags & T_DELETE)
                    295:                fi->tag_type = '?';
                    296:        else
                    297:                fi->tag_type = 'N';
                    298:
                    299:        if (runflags & T_FORCE_MOVE)
                    300:                fi->tag_op = "mov";
                    301:        else if (runflags & T_DELETE)
                    302:                fi->tag_op = "del";
                    303:        else
                    304:                fi->tag_op = "add";
                    305:
                    306:        TAILQ_INSERT_TAIL(&files_info, fi, flist);
                    307:        return;
                    308:
                    309: bad:
                    310:        if (fi->file_path != NULL)
                    311:                xfree(fi->file_path);
                    312:        if (fi->crevstr != NULL)
                    313:                xfree(fi->crevstr);
                    314:        if (fi->nrevstr != NULL)
                    315:                xfree(fi->nrevstr);
                    316:        if (fi->tag_new != NULL)
                    317:                xfree(fi->tag_new);
                    318:        if (fi->tag_old != NULL)
                    319:                xfree(fi->tag_old);
                    320:        if (rev != NULL)
                    321:                rcsnum_free(rev);
                    322:        xfree(fi);
1.7       joris     323: }
                    324:
1.45      xsa       325: void
                    326: cvs_tag_local(struct cvs_file *cf)
1.7       joris     327: {
1.45      xsa       328:        cvs_log(LP_TRACE, "cvs_tag_local(%s)", cf->file_path);
1.17      jfb       329:
1.60      tobias    330:        cvs_file_classify(cf, tag);
                    331:
1.45      xsa       332:        if (cf->file_type == CVS_DIR) {
                    333:                if (verbosity > 1) {
                    334:                        cvs_log(LP_NOTICE, "%s %s",
1.52      xsa       335:                            (runflags & T_DELETE) ? "Untagging" : "Tagging",
1.45      xsa       336:                            cf->file_path);
                    337:                }
                    338:                return;
                    339:        }
1.40      xsa       340:
1.52      xsa       341:        if (runflags & T_CHECK_UPTODATE) {
                    342:                if (cf->file_status != FILE_UPTODATE &&
                    343:                    cf->file_status != FILE_CHECKOUT &&
                    344:                    cf->file_status != FILE_PATCH) {
                    345:                        cvs_log(LP_NOTICE,
                    346:                            "%s is locally modified", cf->file_path);
                    347:                        return;
                    348:                }
                    349:        }
                    350:
                    351:        if (runflags & T_DELETE) {
1.45      xsa       352:                if (tag_del(cf) == 0) {
                    353:                        if (verbosity > 0)
                    354:                                cvs_printf("D %s\n", cf->file_path);
                    355:                }
                    356:                return;
                    357:        }
1.21      xsa       358:
1.69      deraadt   359:        switch (cf->file_status) {
1.45      xsa       360:        case FILE_ADDED:
                    361:                if (verbosity > 1) {
                    362:                        cvs_log(LP_NOTICE,
                    363:                            "couldn't tag added but un-commited file `%s'",
                    364:                            cf->file_path);
1.35      joris     365:                }
1.78      tobias    366:                break;
1.45      xsa       367:        case FILE_REMOVED:
                    368:                if (verbosity > 1) {
                    369:                        cvs_log(LP_NOTICE,
                    370:                            "skipping removed but un-commited file `%s'",
                    371:                            cf->file_path);
                    372:                }
1.78      tobias    373:                break;
1.45      xsa       374:        case FILE_CHECKOUT:
                    375:        case FILE_MODIFIED:
1.60      tobias    376:        case FILE_PATCH:
1.45      xsa       377:        case FILE_UPTODATE:
                    378:                if (tag_add(cf) == 0) {
                    379:                        if (verbosity > 0)
                    380:                                cvs_printf("T %s\n", cf->file_path);
1.56      joris     381:                        cvs_history_add(CVS_HISTORY_TAG, cf, tag_name);
1.35      joris     382:                }
1.45      xsa       383:                break;
                    384:        default:
                    385:                break;
1.1       jfb       386:        }
                    387: }
                    388:
1.14      jfb       389: static int
1.45      xsa       390: tag_del(struct cvs_file *cf)
1.1       jfb       391: {
1.45      xsa       392:        if (cf->file_rcs == NULL)
                    393:                return (-1);
1.1       jfb       394:
1.45      xsa       395:        if (cvs_noexec == 1)
1.35      joris     396:                return (0);
1.1       jfb       397:
1.45      xsa       398:        return (rcs_sym_remove(cf->file_rcs, tag_name));
1.14      jfb       399: }
                    400:
                    401: static int
1.45      xsa       402: tag_add(struct cvs_file *cf)
1.14      jfb       403: {
1.68      joris     404:        int ret;
1.57      xsa       405:        char revbuf[CVS_REV_BUFSZ], trevbuf[CVS_REV_BUFSZ];
1.60      tobias    406:        RCSNUM *srev, *trev;
1.46      xsa       407:        struct rcs_sym *sym;
1.14      jfb       408:
1.45      xsa       409:        if (cf->file_rcs == NULL) {
1.23      xsa       410:                if (verbosity > 1)
1.45      xsa       411:                        cvs_log(LP_NOTICE, "cannot find revision "
                    412:                            "control file for `%s'", cf->file_name);
                    413:                return (-1);
1.14      jfb       414:        }
1.1       jfb       415:
1.60      tobias    416:        if (cvs_cmdop == CVS_OP_TAG) {
                    417:                if (cf->file_ent == NULL)
                    418:                        return (-1);
                    419:                srev = cf->file_ent->ce_rev;
                    420:        } else
                    421:                srev = cf->file_rcsrev;
                    422:
1.45      xsa       423:        if (cvs_noexec == 1)
1.32      xsa       424:                return (0);
1.1       jfb       425:
1.60      tobias    426:        (void)rcsnum_tostr(srev, revbuf, sizeof(revbuf));
1.58      tobias    427:
1.46      xsa       428:        trev = rcs_sym_getrev(cf->file_rcs, tag_name);
                    429:        if (trev != NULL) {
1.60      tobias    430:                if (rcsnum_cmp(srev, trev, 0) == 0) {
1.46      xsa       431:                        rcsnum_free(trev);
                    432:                        return (-1);
                    433:                }
                    434:                (void)rcsnum_tostr(trev, trevbuf, sizeof(trevbuf));
1.72      joris     435:                rcsnum_free(trev);
1.46      xsa       436:
1.52      xsa       437:                if (!(runflags & T_FORCE_MOVE)) {
1.46      xsa       438:                        cvs_printf("W %s : %s ", cf->file_path, tag_name);
                    439:                        cvs_printf("already exists on version %s", trevbuf);
                    440:                        cvs_printf(" : NOT MOVING tag to version %s\n", revbuf);
                    441:
                    442:                        return (-1);
1.67      joris     443:                } else {
1.46      xsa       444:                        sym = rcs_sym_get(cf->file_rcs, tag_name);
1.60      tobias    445:                        rcsnum_cpy(srev, sym->rs_num, 0);
1.46      xsa       446:                        cf->file_rcs->rf_flags &= ~RCS_SYNCED;
                    447:
                    448:                        return (0);
                    449:                }
                    450:        }
                    451:
1.59      tobias    452:        if (runflags & T_BRANCH) {
1.75      tobias    453:                if ((trev = rcs_branch_new(cf->file_rcs, srev)) == NULL)
1.59      tobias    454:                        fatal("Cannot create a new branch");
                    455:        } else {
                    456:                trev = rcsnum_alloc();
1.60      tobias    457:                rcsnum_cpy(srev, trev, 0);
1.59      tobias    458:        }
                    459:
1.68      joris     460:        if ((ret = rcs_sym_add(cf->file_rcs, tag_name, trev)) != 0) {
                    461:                if (ret != 1) {
1.45      xsa       462:                        cvs_log(LP_NOTICE,
                    463:                            "failed to set tag %s to revision %s in %s",
                    464:                            tag_name, revbuf, cf->file_rcs->rf_path);
1.39      xsa       465:                }
1.59      tobias    466:                rcsnum_free(trev);
1.45      xsa       467:                return (-1);
1.1       jfb       468:        }
1.17      jfb       469:
1.59      tobias    470:        rcsnum_free(trev);
1.14      jfb       471:        return (0);
1.1       jfb       472: }