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

1.57    ! xsa         1: /*     $OpenBSD: tag.c,v 1.56 2007/06/18 17:54:13 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.55      otto       18: #include <unistd.h>
1.1       jfb        19:
                     20: #include "cvs.h"
1.50      xsa        21: #include "remote.h"
1.1       jfb        22:
1.52      xsa        23: #define T_CHECK_UPTODATE       0x01
                     24: #define T_DELETE               0x02
                     25: #define T_FORCE_MOVE           0x04
                     26:
1.45      xsa        27: void   cvs_tag_local(struct cvs_file *);
                     28:
                     29: static int tag_del(struct cvs_file *);
                     30: static int tag_add(struct cvs_file *);
                     31:
1.52      xsa        32: static int      runflags = 0;
1.45      xsa        33: static char    *tag = NULL;
                     34: static char    *tag_date = NULL;
                     35: static char    *tag_name = NULL;
                     36: static char    *tag_oldname = NULL;
1.7       joris      37:
1.17      jfb        38: struct cvs_cmd cvs_cmd_tag = {
1.47      joris      39:        CVS_OP_TAG, 0, "tag",
1.17      jfb        40:        { "ta", "freeze" },
                     41:        "Add a symbolic tag to checked out version of files",
1.40      xsa        42:        "[-bcdFflR] [-D date | -r rev] tag [file ...]",
1.17      jfb        43:        "bcD:dFflRr:",
                     44:        NULL,
1.45      xsa        45:        cvs_tag
1.17      jfb        46: };
                     47:
1.45      xsa        48: int
                     49: cvs_tag(int argc, char **argv)
1.1       jfb        50: {
1.45      xsa        51:        int ch, flags;
1.49      reyk       52:        char *arg = ".";
1.45      xsa        53:        struct cvs_recursion cr;
1.1       jfb        54:
1.45      xsa        55:        flags = CR_RECURSE_DIRS;
1.1       jfb        56:
1.45      xsa        57:        while ((ch = getopt(argc, argv, cvs_cmd_tag.cmd_opts)) != -1) {
1.1       jfb        58:                switch (ch) {
1.52      xsa        59:                case 'c':
                     60:                        runflags |= T_CHECK_UPTODATE;
                     61:                        break;
1.45      xsa        62:                case 'D':
                     63:                        tag_date = optarg;
1.1       jfb        64:                        break;
                     65:                case 'd':
1.52      xsa        66:                        runflags |= T_DELETE;
1.3       jfb        67:                        break;
1.46      xsa        68:                case 'F':
1.52      xsa        69:                        runflags |= T_FORCE_MOVE;
1.46      xsa        70:                        break;
1.1       jfb        71:                case 'l':
1.45      xsa        72:                        flags &= ~CR_RECURSE_DIRS;
1.17      jfb        73:                        break;
                     74:                case 'R':
1.1       jfb        75:                        break;
                     76:                case 'r':
1.14      jfb        77:                        tag_oldname = optarg;
1.1       jfb        78:                        break;
                     79:                default:
1.45      xsa        80:                        fatal("%s", cvs_cmd_tag.cmd_synopsis);
1.1       jfb        81:                }
                     82:        }
                     83:
                     84:        argc -= optind;
                     85:        argv += optind;
                     86:
1.40      xsa        87:        if (argc == 0)
1.45      xsa        88:                fatal("%s", cvs_cmd_tag.cmd_synopsis);
                     89:
                     90:        tag_name = argv[0];
                     91:        argc--;
                     92:        argv++;
1.15      jfb        93:
1.38      xsa        94:        if (!rcs_sym_check(tag_name))
                     95:                fatal("tag `%s' must not contain the characters `%s'",
1.15      jfb        96:                    tag_name, RCS_SYM_INVALCHAR);
1.1       jfb        97:
1.45      xsa        98:        if (tag_oldname != NULL) {
1.52      xsa        99:                if (runflags & T_DELETE)
1.45      xsa       100:                        tag_oldname = NULL;
                    101:                else
                    102:                        tag = tag_oldname;
                    103:        }
                    104:
                    105:        if (tag_date != NULL) {
1.52      xsa       106:                if (runflags & T_DELETE)
1.45      xsa       107:                        tag_date = NULL;
                    108:                else
                    109:                        tag = tag_date;
                    110:        }
                    111:
                    112:        if (tag_oldname != NULL && tag_date != NULL)
                    113:                fatal("-r and -D options are mutually exclusive");
                    114:
                    115:        cr.enterdir = NULL;
                    116:        cr.leavedir = NULL;
1.50      xsa       117:
                    118:        if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
1.53      joris     119:                cvs_client_connect_to_server();
1.50      xsa       120:                cr.fileproc = cvs_client_sendfile;
                    121:
1.52      xsa       122:                if (runflags & T_CHECK_UPTODATE)
                    123:                        cvs_client_send_request("Argument -c");
                    124:
                    125:                if (runflags & T_DELETE)
1.50      xsa       126:                        cvs_client_send_request("Argument -d");
                    127:
1.52      xsa       128:                if (runflags & T_FORCE_MOVE)
1.50      xsa       129:                        cvs_client_send_request("Argument -F");
                    130:
                    131:                if (!(flags & CR_RECURSE_DIRS))
                    132:                        cvs_client_send_request("Argument -l");
                    133:
                    134:                if (tag_oldname != NULL)
                    135:                        cvs_client_send_request("Argument -r%s", tag_oldname);
                    136:
                    137:                cvs_client_send_request("Argument %s", tag_name);
                    138:        } else {
                    139:                cr.fileproc = cvs_tag_local;
                    140:        }
                    141:
1.45      xsa       142:        cr.flags = flags;
1.1       jfb       143:
1.49      reyk      144:        if (argc > 0)
                    145:                cvs_file_run(argc, argv, &cr);
                    146:        else
                    147:                cvs_file_run(1, &arg, &cr);
1.50      xsa       148:
                    149:        if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
                    150:                cvs_client_send_files(argv, argc);
                    151:                cvs_client_senddir(".");
                    152:                cvs_client_send_request("tag");
                    153:                cvs_client_get_responses();
                    154:        }
1.3       jfb       155:
1.7       joris     156:        return (0);
                    157: }
                    158:
1.45      xsa       159: void
                    160: cvs_tag_local(struct cvs_file *cf)
1.7       joris     161: {
1.45      xsa       162:        cvs_log(LP_TRACE, "cvs_tag_local(%s)", cf->file_path);
1.17      jfb       163:
1.45      xsa       164:        if (cf->file_type == CVS_DIR) {
                    165:                if (verbosity > 1) {
                    166:                        cvs_log(LP_NOTICE, "%s %s",
1.52      xsa       167:                            (runflags & T_DELETE) ? "Untagging" : "Tagging",
1.45      xsa       168:                            cf->file_path);
                    169:                }
                    170:                return;
                    171:        }
1.40      xsa       172:
1.54      joris     173:        cvs_file_classify(cf, tag);
1.7       joris     174:
1.52      xsa       175:        if (runflags & T_CHECK_UPTODATE) {
                    176:                if (cf->file_status != FILE_UPTODATE &&
                    177:                    cf->file_status != FILE_CHECKOUT &&
                    178:                    cf->file_status != FILE_PATCH) {
                    179:                        cvs_log(LP_NOTICE,
                    180:                            "%s is locally modified", cf->file_path);
                    181:                        return;
                    182:                }
                    183:        }
                    184:
                    185:        if (runflags & T_DELETE) {
1.45      xsa       186:                if (tag_del(cf) == 0) {
                    187:                        if (verbosity > 0)
                    188:                                cvs_printf("D %s\n", cf->file_path);
1.21      xsa       189:
1.45      xsa       190:                        rcs_write(cf->file_rcs);
                    191:                }
                    192:                return;
                    193:        }
1.21      xsa       194:
1.45      xsa       195:        switch(cf->file_status) {
                    196:        case FILE_ADDED:
                    197:                if (verbosity > 1) {
                    198:                        cvs_log(LP_NOTICE,
                    199:                            "couldn't tag added but un-commited file `%s'",
                    200:                            cf->file_path);
1.35      joris     201:                }
1.45      xsa       202:                return;
                    203:        case FILE_REMOVED:
                    204:                if (verbosity > 1) {
                    205:                        cvs_log(LP_NOTICE,
                    206:                            "skipping removed but un-commited file `%s'",
                    207:                            cf->file_path);
                    208:                }
                    209:                return;
                    210:        case FILE_CHECKOUT:
                    211:        case FILE_MODIFIED:
                    212:        case FILE_UPTODATE:
                    213:                if (tag_add(cf) == 0) {
                    214:                        if (verbosity > 0)
                    215:                                cvs_printf("T %s\n", cf->file_path);
1.1       jfb       216:
1.45      xsa       217:                        rcs_write(cf->file_rcs);
1.56      joris     218:                        cvs_history_add(CVS_HISTORY_TAG, cf, tag_name);
1.35      joris     219:                }
1.45      xsa       220:                break;
                    221:        default:
                    222:                break;
1.1       jfb       223:        }
                    224: }
                    225:
1.14      jfb       226: static int
1.45      xsa       227: tag_del(struct cvs_file *cf)
1.1       jfb       228: {
1.45      xsa       229:        if (cf->file_rcs == NULL)
                    230:                return (-1);
1.1       jfb       231:
1.45      xsa       232:        if (cvs_noexec == 1)
1.35      joris     233:                return (0);
1.1       jfb       234:
1.45      xsa       235:        return (rcs_sym_remove(cf->file_rcs, tag_name));
1.14      jfb       236: }
                    237:
                    238: static int
1.45      xsa       239: tag_add(struct cvs_file *cf)
1.14      jfb       240: {
1.57    ! xsa       241:        char revbuf[CVS_REV_BUFSZ], trevbuf[CVS_REV_BUFSZ];
1.46      xsa       242:        RCSNUM *trev;
                    243:        struct rcs_sym *sym;
1.14      jfb       244:
1.45      xsa       245:        if (cf->file_rcs == NULL) {
1.23      xsa       246:                if (verbosity > 1)
1.45      xsa       247:                        cvs_log(LP_NOTICE, "cannot find revision "
                    248:                            "control file for `%s'", cf->file_name);
                    249:                return (-1);
1.14      jfb       250:        }
1.1       jfb       251:
1.45      xsa       252:        if (cvs_noexec == 1)
1.32      xsa       253:                return (0);
1.1       jfb       254:
1.46      xsa       255:        trev = rcs_sym_getrev(cf->file_rcs, tag_name);
                    256:        if (trev != NULL) {
                    257:                if (rcsnum_cmp(cf->file_rcsrev, trev, 0) == 0) {
                    258:                        rcsnum_free(trev);
                    259:                        return (-1);
                    260:                }
                    261:                (void)rcsnum_tostr(trev, trevbuf, sizeof(trevbuf));
                    262:
1.52      xsa       263:                if (!(runflags & T_FORCE_MOVE)) {
1.46      xsa       264:                        cvs_printf("W %s : %s ", cf->file_path, tag_name);
                    265:                        cvs_printf("already exists on version %s", trevbuf);
                    266:                        cvs_printf(" : NOT MOVING tag to version %s\n", revbuf);
                    267:
                    268:                        return (-1);
1.52      xsa       269:                } else if (runflags & T_FORCE_MOVE) {
1.46      xsa       270:                        sym = rcs_sym_get(cf->file_rcs, tag_name);
                    271:                        rcsnum_cpy(cf->file_rcsrev, sym->rs_num, 0);
                    272:                        cf->file_rcs->rf_flags &= ~RCS_SYNCED;
                    273:
                    274:                        return (0);
                    275:                }
                    276:        }
                    277:
1.45      xsa       278:        if (rcs_sym_add(cf->file_rcs, tag_name, cf->file_rcsrev) == -1) {
                    279:                if (rcs_errno != RCS_ERR_DUPENT) {
1.46      xsa       280:                        (void)rcsnum_tostr(cf->file_rcsrev, revbuf,
                    281:                            sizeof(revbuf));
1.45      xsa       282:                        cvs_log(LP_NOTICE,
                    283:                            "failed to set tag %s to revision %s in %s",
                    284:                            tag_name, revbuf, cf->file_rcs->rf_path);
1.39      xsa       285:                }
1.45      xsa       286:                return (-1);
1.1       jfb       287:        }
1.17      jfb       288:
1.14      jfb       289:        return (0);
1.1       jfb       290: }