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

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