Annotation of src/usr.bin/cvs/tag.c, Revision 1.52
1.52 ! xsa 1: /* $OpenBSD: tag.c,v 1.51 2006/11/09 10:08:33 xsa 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.36 xsa 18: #include "includes.h"
1.1 jfb 19:
20: #include "cvs.h"
21: #include "log.h"
1.50 xsa 22: #include "remote.h"
1.1 jfb 23:
1.52 ! xsa 24: #define T_CHECK_UPTODATE 0x01
! 25: #define T_DELETE 0x02
! 26: #define T_FORCE_MOVE 0x04
! 27:
1.45 xsa 28: void cvs_tag_local(struct cvs_file *);
29:
30: static int tag_del(struct cvs_file *);
31: static int tag_add(struct cvs_file *);
32:
1.52 ! xsa 33: static int runflags = 0;
1.45 xsa 34: static char *tag = NULL;
35: static char *tag_date = NULL;
36: static char *tag_name = NULL;
37: static char *tag_oldname = NULL;
1.7 joris 38:
1.17 jfb 39: struct cvs_cmd cvs_cmd_tag = {
1.47 joris 40: CVS_OP_TAG, 0, "tag",
1.17 jfb 41: { "ta", "freeze" },
42: "Add a symbolic tag to checked out version of files",
1.40 xsa 43: "[-bcdFflR] [-D date | -r rev] tag [file ...]",
1.17 jfb 44: "bcD:dFflRr:",
45: NULL,
1.45 xsa 46: cvs_tag
1.17 jfb 47: };
48:
1.45 xsa 49: int
50: cvs_tag(int argc, char **argv)
1.1 jfb 51: {
1.45 xsa 52: int ch, flags;
1.49 reyk 53: char *arg = ".";
1.45 xsa 54: struct cvs_recursion cr;
1.1 jfb 55:
1.45 xsa 56: flags = CR_RECURSE_DIRS;
1.1 jfb 57:
1.45 xsa 58: while ((ch = getopt(argc, argv, cvs_cmd_tag.cmd_opts)) != -1) {
1.1 jfb 59: switch (ch) {
1.52 ! xsa 60: case 'c':
! 61: runflags |= T_CHECK_UPTODATE;
! 62: break;
1.45 xsa 63: case 'D':
64: tag_date = optarg;
1.1 jfb 65: break;
66: case 'd':
1.52 ! xsa 67: runflags |= T_DELETE;
1.3 jfb 68: break;
1.46 xsa 69: case 'F':
1.52 ! xsa 70: runflags |= T_FORCE_MOVE;
1.46 xsa 71: break;
1.1 jfb 72: case 'l':
1.45 xsa 73: flags &= ~CR_RECURSE_DIRS;
1.17 jfb 74: break;
75: case 'R':
1.1 jfb 76: break;
77: case 'r':
1.14 jfb 78: tag_oldname = optarg;
1.1 jfb 79: break;
80: default:
1.45 xsa 81: fatal("%s", cvs_cmd_tag.cmd_synopsis);
1.1 jfb 82: }
83: }
84:
85: argc -= optind;
86: argv += optind;
87:
1.40 xsa 88: if (argc == 0)
1.45 xsa 89: fatal("%s", cvs_cmd_tag.cmd_synopsis);
90:
91: tag_name = argv[0];
92: argc--;
93: argv++;
1.15 jfb 94:
1.38 xsa 95: if (!rcs_sym_check(tag_name))
96: fatal("tag `%s' must not contain the characters `%s'",
1.15 jfb 97: tag_name, RCS_SYM_INVALCHAR);
1.1 jfb 98:
1.45 xsa 99: if (tag_oldname != NULL) {
1.52 ! xsa 100: if (runflags & T_DELETE)
1.45 xsa 101: tag_oldname = NULL;
102: else
103: tag = tag_oldname;
104: }
105:
106: if (tag_date != NULL) {
1.52 ! xsa 107: if (runflags & T_DELETE)
1.45 xsa 108: tag_date = NULL;
109: else
110: tag = tag_date;
111: }
112:
113: if (tag_oldname != NULL && tag_date != NULL)
114: fatal("-r and -D options are mutually exclusive");
115:
116: cr.enterdir = NULL;
117: cr.leavedir = NULL;
1.50 xsa 118:
119: if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
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.45 xsa 173: cvs_file_classify(cf, tag, 0);
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.35 joris 218: }
1.45 xsa 219: break;
220: default:
221: break;
1.1 jfb 222: }
223: }
224:
1.14 jfb 225: static int
1.45 xsa 226: tag_del(struct cvs_file *cf)
1.1 jfb 227: {
1.45 xsa 228: if (cf->file_rcs == NULL)
229: return (-1);
1.1 jfb 230:
1.45 xsa 231: if (cvs_noexec == 1)
1.35 joris 232: return (0);
1.1 jfb 233:
1.45 xsa 234: return (rcs_sym_remove(cf->file_rcs, tag_name));
1.14 jfb 235: }
236:
237: static int
1.45 xsa 238: tag_add(struct cvs_file *cf)
1.14 jfb 239: {
1.46 xsa 240: char revbuf[16], trevbuf[16];
241: RCSNUM *trev;
242: struct rcs_sym *sym;
1.14 jfb 243:
1.45 xsa 244: if (cf->file_rcs == NULL) {
1.23 xsa 245: if (verbosity > 1)
1.45 xsa 246: cvs_log(LP_NOTICE, "cannot find revision "
247: "control file for `%s'", cf->file_name);
248: return (-1);
1.14 jfb 249: }
1.1 jfb 250:
1.45 xsa 251: if (cvs_noexec == 1)
1.32 xsa 252: return (0);
1.1 jfb 253:
1.46 xsa 254: trev = rcs_sym_getrev(cf->file_rcs, tag_name);
255: if (trev != NULL) {
256: if (rcsnum_cmp(cf->file_rcsrev, trev, 0) == 0) {
257: rcsnum_free(trev);
258: return (-1);
259: }
260: (void)rcsnum_tostr(trev, trevbuf, sizeof(trevbuf));
261:
1.52 ! xsa 262: if (!(runflags & T_FORCE_MOVE)) {
1.46 xsa 263: cvs_printf("W %s : %s ", cf->file_path, tag_name);
264: cvs_printf("already exists on version %s", trevbuf);
265: cvs_printf(" : NOT MOVING tag to version %s\n", revbuf);
266:
267: return (-1);
1.52 ! xsa 268: } else if (runflags & T_FORCE_MOVE) {
1.46 xsa 269: sym = rcs_sym_get(cf->file_rcs, tag_name);
270: rcsnum_cpy(cf->file_rcsrev, sym->rs_num, 0);
271: cf->file_rcs->rf_flags &= ~RCS_SYNCED;
272:
273: return (0);
274: }
275: }
276:
1.45 xsa 277: if (rcs_sym_add(cf->file_rcs, tag_name, cf->file_rcsrev) == -1) {
278: if (rcs_errno != RCS_ERR_DUPENT) {
1.46 xsa 279: (void)rcsnum_tostr(cf->file_rcsrev, revbuf,
280: sizeof(revbuf));
1.45 xsa 281: cvs_log(LP_NOTICE,
282: "failed to set tag %s to revision %s in %s",
283: tag_name, revbuf, cf->file_rcs->rf_path);
1.39 xsa 284: }
1.45 xsa 285: return (-1);
1.1 jfb 286: }
1.17 jfb 287:
1.14 jfb 288: return (0);
1.1 jfb 289: }