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

1.75    ! tobias      1: /*     $OpenBSD: tag.c,v 1.74 2008/06/10 01:00:34 joris 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");
                    172:
                    173:                if (tag_oldname != NULL)
                    174:                        cvs_client_send_request("Argument -r%s", tag_oldname);
                    175:
                    176:                cvs_client_send_request("Argument %s", tag_name);
                    177:        } else {
1.60      tobias    178:                if (cvs_cmdop == CVS_OP_RTAG &&
                    179:                    chdir(current_cvsroot->cr_dir) == -1)
                    180:                        fatal("cvs_tag: %s", strerror(errno));
                    181:
1.50      xsa       182:        }
                    183:
1.45      xsa       184:        cr.flags = flags;
1.1       jfb       185:
1.74      joris     186:        cvs_get_repository_name(".", repo, MAXPATHLEN);
                    187:        line_list = cvs_trigger_getlines(CVS_PATH_TAGINFO, repo);
                    188:        if (line_list != NULL) {
                    189:                TAILQ_INIT(&files_info);
                    190:                cr.fileproc = cvs_tag_check_files;
                    191:                if (argc > 0)
                    192:                        cvs_file_run(argc, argv, &cr);
                    193:                else
                    194:                        cvs_file_run(1, &arg, &cr);
                    195:
                    196:                if (cvs_trigger_handle(CVS_TRIGGER_TAGINFO, repo, NULL,
                    197:                    line_list, &files_info)) {
                    198:                        cvs_log(LP_ERR, "Pre-tag check failed");
                    199:                        cvs_trigger_freelist(line_list);
                    200:                        goto bad;
                    201:                }
                    202:                cvs_trigger_freelist(line_list);
                    203:        }
                    204:
                    205:        cr.fileproc = cvs_tag_local;
                    206:
1.60      tobias    207:        if (cvs_cmdop == CVS_OP_TAG ||
1.61      tobias    208:            current_cvsroot->cr_method == CVS_METHOD_LOCAL) {
1.60      tobias    209:                if (argc > 0)
                    210:                        cvs_file_run(argc, argv, &cr);
                    211:                else
                    212:                        cvs_file_run(1, &arg, &cr);
                    213:        }
1.50      xsa       214:
                    215:        if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
                    216:                cvs_client_send_files(argv, argc);
                    217:                cvs_client_senddir(".");
1.60      tobias    218:
                    219:                cvs_client_send_request((cvs_cmdop == CVS_OP_RTAG) ?
                    220:                    "rtag" : "tag");
                    221:
1.50      xsa       222:                cvs_client_get_responses();
                    223:        }
1.3       jfb       224:
1.74      joris     225: bad:
                    226:        cvs_trigger_freeinfo(&files_info);
1.7       joris     227:        return (0);
1.74      joris     228: }
                    229:
                    230: void
                    231: cvs_tag_check_files(struct cvs_file *cf)
                    232: {
                    233:        RCSNUM *srev = NULL, *rev = NULL;
                    234:        char rbuf[CVS_REV_BUFSZ];
                    235:        struct file_info *fi;
                    236:
                    237:        cvs_log(LP_TRACE, "cvs_tag_check_files(%s)", cf->file_path);
                    238:
                    239:        cvs_file_classify(cf, tag);
                    240:
                    241:        if (cf->file_type == CVS_DIR)
                    242:                return;
                    243:
                    244:        if (runflags & T_CHECK_UPTODATE) {
                    245:                if (cf->file_status != FILE_UPTODATE &&
                    246:                    cf->file_status != FILE_CHECKOUT &&
                    247:                    cf->file_status != FILE_PATCH) {
                    248:                        return;
                    249:                }
                    250:        }
                    251:
                    252:        switch (cf->file_status) {
                    253:        case FILE_ADDED:
                    254:        case FILE_REMOVED:
                    255:                return;
                    256:        default:
                    257:                break;
                    258:        }
                    259:
                    260:        fi = xcalloc(1, sizeof(*fi));
                    261:        fi->nrevstr = xstrdup(rbuf);
                    262:
                    263:        if (cvs_cmdop == CVS_OP_TAG) {
                    264:                if (cf->file_ent == NULL)
                    265:                        return;
                    266:                srev = cf->file_ent->ce_rev;
                    267:        } else
                    268:                srev = cf->file_rcsrev;
                    269:
                    270:        rcsnum_tostr(srev, rbuf, sizeof(rbuf));
                    271:        fi->file_path = xstrdup(cf->file_path);
                    272:
                    273:        if (tag_oldname != NULL)
                    274:                fi->tag_old = xstrdup(tag_oldname);
                    275:        else if (tag_date != NULL)
                    276:                fi->tag_old = xstrdup(tag_date);
                    277:
                    278:        if ((rev = rcs_sym_getrev(cf->file_rcs, tag_name)) != NULL) {
                    279:                if (!rcsnum_differ(srev, rev))
                    280:                        goto bad;
                    281:                rcsnum_tostr(rev, rbuf, sizeof(rbuf));
                    282:                fi->crevstr = xstrdup(rbuf);
                    283:        } else if (runflags & T_DELETE)
                    284:                goto bad;
                    285:
                    286:        fi->tag_new = xstrdup(tag_name);
                    287:
                    288:        if (runflags & T_BRANCH)
                    289:                fi->tag_type = 'T';
                    290:        else if (runflags & T_DELETE)
                    291:                fi->tag_type = '?';
                    292:        else
                    293:                fi->tag_type = 'N';
                    294:
                    295:        if (runflags & T_FORCE_MOVE)
                    296:                fi->tag_op = "mov";
                    297:        else if (runflags & T_DELETE)
                    298:                fi->tag_op = "del";
                    299:        else
                    300:                fi->tag_op = "add";
                    301:
                    302:        TAILQ_INSERT_TAIL(&files_info, fi, flist);
                    303:        return;
                    304:
                    305: bad:
                    306:        if (fi->file_path != NULL)
                    307:                xfree(fi->file_path);
                    308:        if (fi->crevstr != NULL)
                    309:                xfree(fi->crevstr);
                    310:        if (fi->nrevstr != NULL)
                    311:                xfree(fi->nrevstr);
                    312:        if (fi->tag_new != NULL)
                    313:                xfree(fi->tag_new);
                    314:        if (fi->tag_old != NULL)
                    315:                xfree(fi->tag_old);
                    316:        if (rev != NULL)
                    317:                rcsnum_free(rev);
                    318:        xfree(fi);
1.7       joris     319: }
                    320:
1.45      xsa       321: void
                    322: cvs_tag_local(struct cvs_file *cf)
1.7       joris     323: {
1.45      xsa       324:        cvs_log(LP_TRACE, "cvs_tag_local(%s)", cf->file_path);
1.17      jfb       325:
1.60      tobias    326:        cvs_file_classify(cf, tag);
                    327:
1.45      xsa       328:        if (cf->file_type == CVS_DIR) {
                    329:                if (verbosity > 1) {
                    330:                        cvs_log(LP_NOTICE, "%s %s",
1.52      xsa       331:                            (runflags & T_DELETE) ? "Untagging" : "Tagging",
1.45      xsa       332:                            cf->file_path);
                    333:                }
                    334:                return;
                    335:        }
1.40      xsa       336:
1.52      xsa       337:        if (runflags & T_CHECK_UPTODATE) {
                    338:                if (cf->file_status != FILE_UPTODATE &&
                    339:                    cf->file_status != FILE_CHECKOUT &&
                    340:                    cf->file_status != FILE_PATCH) {
                    341:                        cvs_log(LP_NOTICE,
                    342:                            "%s is locally modified", cf->file_path);
                    343:                        return;
                    344:                }
                    345:        }
                    346:
                    347:        if (runflags & T_DELETE) {
1.45      xsa       348:                if (tag_del(cf) == 0) {
                    349:                        if (verbosity > 0)
                    350:                                cvs_printf("D %s\n", cf->file_path);
1.21      xsa       351:
1.45      xsa       352:                        rcs_write(cf->file_rcs);
                    353:                }
                    354:                return;
                    355:        }
1.21      xsa       356:
1.69      deraadt   357:        switch (cf->file_status) {
1.45      xsa       358:        case FILE_ADDED:
                    359:                if (verbosity > 1) {
                    360:                        cvs_log(LP_NOTICE,
                    361:                            "couldn't tag added but un-commited file `%s'",
                    362:                            cf->file_path);
1.35      joris     363:                }
1.45      xsa       364:                return;
                    365:        case FILE_REMOVED:
                    366:                if (verbosity > 1) {
                    367:                        cvs_log(LP_NOTICE,
                    368:                            "skipping removed but un-commited file `%s'",
                    369:                            cf->file_path);
                    370:                }
                    371:                return;
                    372:        case FILE_CHECKOUT:
                    373:        case FILE_MODIFIED:
1.60      tobias    374:        case FILE_PATCH:
1.45      xsa       375:        case FILE_UPTODATE:
                    376:                if (tag_add(cf) == 0) {
                    377:                        if (verbosity > 0)
                    378:                                cvs_printf("T %s\n", cf->file_path);
1.1       jfb       379:
1.45      xsa       380:                        rcs_write(cf->file_rcs);
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: }