Annotation of src/usr.bin/cvs/tag.c, Revision 1.70
1.70 ! tobias 1: /* $OpenBSD: tag.c,v 1.69 2008/03/01 21:29:37 deraadt 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.45 xsa 30: void cvs_tag_local(struct cvs_file *);
31:
32: static int tag_del(struct cvs_file *);
33: static int tag_add(struct cvs_file *);
34:
1.52 xsa 35: static int runflags = 0;
1.45 xsa 36: static char *tag = NULL;
37: static char *tag_date = NULL;
38: static char *tag_name = NULL;
39: static char *tag_oldname = NULL;
1.7 joris 40:
1.60 tobias 41: struct cvs_cmd cvs_cmd_rtag = {
42: CVS_OP_RTAG, 0, "rtag",
43: { "rt", "rfreeze" },
44: "Add a symbolic tag to a module",
45: "[-bcdFflR] [-D date | -r rev] tag modules ...",
46: "bcD:dFflRr:",
47: NULL,
48: cvs_tag
49: };
50:
1.17 jfb 51: struct cvs_cmd cvs_cmd_tag = {
1.63 tobias 52: CVS_OP_TAG, CVS_USE_WDIR, "tag",
1.17 jfb 53: { "ta", "freeze" },
54: "Add a symbolic tag to checked out version of files",
1.40 xsa 55: "[-bcdFflR] [-D date | -r rev] tag [file ...]",
1.17 jfb 56: "bcD:dFflRr:",
57: NULL,
1.45 xsa 58: cvs_tag
1.17 jfb 59: };
60:
1.45 xsa 61: int
62: cvs_tag(int argc, char **argv)
1.1 jfb 63: {
1.60 tobias 64: int ch, flags, i;
1.49 reyk 65: char *arg = ".";
1.45 xsa 66: struct cvs_recursion cr;
1.1 jfb 67:
1.45 xsa 68: flags = CR_RECURSE_DIRS;
1.1 jfb 69:
1.65 tobias 70: while ((ch = getopt(argc, argv, cvs_cmdop == CVS_OP_TAG ?
71: cvs_cmd_tag.cmd_opts : cvs_cmd_rtag.cmd_opts)) != -1) {
1.1 jfb 72: switch (ch) {
1.59 tobias 73: case 'b':
74: runflags |= T_BRANCH;
75: break;
1.52 xsa 76: case 'c':
77: runflags |= T_CHECK_UPTODATE;
78: break;
1.45 xsa 79: case 'D':
80: tag_date = optarg;
1.1 jfb 81: break;
82: case 'd':
1.52 xsa 83: runflags |= T_DELETE;
1.3 jfb 84: break;
1.46 xsa 85: case 'F':
1.52 xsa 86: runflags |= T_FORCE_MOVE;
1.46 xsa 87: break;
1.1 jfb 88: case 'l':
1.45 xsa 89: flags &= ~CR_RECURSE_DIRS;
1.17 jfb 90: break;
91: case 'R':
1.62 tobias 92: flags |= CR_RECURSE_DIRS;
1.1 jfb 93: break;
94: case 'r':
1.14 jfb 95: tag_oldname = optarg;
1.1 jfb 96: break;
97: default:
1.70 ! tobias 98: fatal("%s", cvs_cmdop == CVS_OP_TAG ?
! 99: cvs_cmd_tag.cmd_synopsis :
! 100: cvs_cmd_rtag.cmd_synopsis);
1.1 jfb 101: }
102: }
103:
104: argc -= optind;
105: argv += optind;
106:
1.60 tobias 107: if (cvs_cmdop == CVS_OP_RTAG) {
1.63 tobias 108: flags |= CR_REPO;
109:
1.60 tobias 110: if (argc < 2)
111: fatal("%s", cvs_cmd_rtag.cmd_synopsis);
1.64 xsa 112:
1.66 joris 113: for (i = 1; i < argc; i++) {
114: if (argv[i][0] == '/')
115: fatal("Absolute path name is invalid: %s",
116: argv[i]);
117: }
1.60 tobias 118: } else if (cvs_cmdop == CVS_OP_TAG && argc == 0)
1.45 xsa 119: fatal("%s", cvs_cmd_tag.cmd_synopsis);
120:
121: tag_name = argv[0];
122: argc--;
123: argv++;
1.15 jfb 124:
1.38 xsa 125: if (!rcs_sym_check(tag_name))
126: fatal("tag `%s' must not contain the characters `%s'",
1.15 jfb 127: tag_name, RCS_SYM_INVALCHAR);
1.1 jfb 128:
1.45 xsa 129: if (tag_oldname != NULL) {
1.52 xsa 130: if (runflags & T_DELETE)
1.45 xsa 131: tag_oldname = NULL;
132: else
133: tag = tag_oldname;
134: }
135:
136: if (tag_date != NULL) {
1.52 xsa 137: if (runflags & T_DELETE)
1.45 xsa 138: tag_date = NULL;
139: else
140: tag = tag_date;
141: }
142:
143: if (tag_oldname != NULL && tag_date != NULL)
144: fatal("-r and -D options are mutually exclusive");
145:
146: cr.enterdir = NULL;
147: cr.leavedir = NULL;
1.50 xsa 148:
149: if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
1.53 joris 150: cvs_client_connect_to_server();
1.50 xsa 151: cr.fileproc = cvs_client_sendfile;
152:
1.59 tobias 153: if (runflags & T_BRANCH)
154: cvs_client_send_request("Argument -b");
155:
1.52 xsa 156: if (runflags & T_CHECK_UPTODATE)
157: cvs_client_send_request("Argument -c");
158:
159: if (runflags & T_DELETE)
1.50 xsa 160: cvs_client_send_request("Argument -d");
161:
1.52 xsa 162: if (runflags & T_FORCE_MOVE)
1.50 xsa 163: cvs_client_send_request("Argument -F");
164:
165: if (!(flags & CR_RECURSE_DIRS))
166: cvs_client_send_request("Argument -l");
167:
168: if (tag_oldname != NULL)
169: cvs_client_send_request("Argument -r%s", tag_oldname);
170:
171: cvs_client_send_request("Argument %s", tag_name);
172: } else {
1.60 tobias 173: if (cvs_cmdop == CVS_OP_RTAG &&
174: chdir(current_cvsroot->cr_dir) == -1)
175: fatal("cvs_tag: %s", strerror(errno));
176:
1.50 xsa 177: cr.fileproc = cvs_tag_local;
178: }
179:
1.45 xsa 180: cr.flags = flags;
1.1 jfb 181:
1.60 tobias 182: if (cvs_cmdop == CVS_OP_TAG ||
1.61 tobias 183: current_cvsroot->cr_method == CVS_METHOD_LOCAL) {
1.60 tobias 184: if (argc > 0)
185: cvs_file_run(argc, argv, &cr);
186: else
187: cvs_file_run(1, &arg, &cr);
188: }
1.50 xsa 189:
190: if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
191: cvs_client_send_files(argv, argc);
192: cvs_client_senddir(".");
1.60 tobias 193:
194: cvs_client_send_request((cvs_cmdop == CVS_OP_RTAG) ?
195: "rtag" : "tag");
196:
1.50 xsa 197: cvs_client_get_responses();
198: }
1.3 jfb 199:
1.7 joris 200: return (0);
201: }
202:
1.45 xsa 203: void
204: cvs_tag_local(struct cvs_file *cf)
1.7 joris 205: {
1.45 xsa 206: cvs_log(LP_TRACE, "cvs_tag_local(%s)", cf->file_path);
1.17 jfb 207:
1.60 tobias 208: cvs_file_classify(cf, tag);
209:
1.45 xsa 210: if (cf->file_type == CVS_DIR) {
211: if (verbosity > 1) {
212: cvs_log(LP_NOTICE, "%s %s",
1.52 xsa 213: (runflags & T_DELETE) ? "Untagging" : "Tagging",
1.45 xsa 214: cf->file_path);
215: }
216: return;
217: }
1.40 xsa 218:
1.52 xsa 219: if (runflags & T_CHECK_UPTODATE) {
220: if (cf->file_status != FILE_UPTODATE &&
221: cf->file_status != FILE_CHECKOUT &&
222: cf->file_status != FILE_PATCH) {
223: cvs_log(LP_NOTICE,
224: "%s is locally modified", cf->file_path);
225: return;
226: }
227: }
228:
229: if (runflags & T_DELETE) {
1.45 xsa 230: if (tag_del(cf) == 0) {
231: if (verbosity > 0)
232: cvs_printf("D %s\n", cf->file_path);
1.21 xsa 233:
1.45 xsa 234: rcs_write(cf->file_rcs);
235: }
236: return;
237: }
1.21 xsa 238:
1.69 deraadt 239: switch (cf->file_status) {
1.45 xsa 240: case FILE_ADDED:
241: if (verbosity > 1) {
242: cvs_log(LP_NOTICE,
243: "couldn't tag added but un-commited file `%s'",
244: cf->file_path);
1.35 joris 245: }
1.45 xsa 246: return;
247: case FILE_REMOVED:
248: if (verbosity > 1) {
249: cvs_log(LP_NOTICE,
250: "skipping removed but un-commited file `%s'",
251: cf->file_path);
252: }
253: return;
254: case FILE_CHECKOUT:
255: case FILE_MODIFIED:
1.60 tobias 256: case FILE_PATCH:
1.45 xsa 257: case FILE_UPTODATE:
258: if (tag_add(cf) == 0) {
259: if (verbosity > 0)
260: cvs_printf("T %s\n", cf->file_path);
1.1 jfb 261:
1.45 xsa 262: rcs_write(cf->file_rcs);
1.56 joris 263: cvs_history_add(CVS_HISTORY_TAG, cf, tag_name);
1.35 joris 264: }
1.45 xsa 265: break;
266: default:
267: break;
1.1 jfb 268: }
269: }
270:
1.14 jfb 271: static int
1.45 xsa 272: tag_del(struct cvs_file *cf)
1.1 jfb 273: {
1.45 xsa 274: if (cf->file_rcs == NULL)
275: return (-1);
1.1 jfb 276:
1.45 xsa 277: if (cvs_noexec == 1)
1.35 joris 278: return (0);
1.1 jfb 279:
1.45 xsa 280: return (rcs_sym_remove(cf->file_rcs, tag_name));
1.14 jfb 281: }
282:
283: static int
1.45 xsa 284: tag_add(struct cvs_file *cf)
1.14 jfb 285: {
1.68 joris 286: int ret;
1.57 xsa 287: char revbuf[CVS_REV_BUFSZ], trevbuf[CVS_REV_BUFSZ];
1.60 tobias 288: RCSNUM *srev, *trev;
1.46 xsa 289: struct rcs_sym *sym;
1.14 jfb 290:
1.45 xsa 291: if (cf->file_rcs == NULL) {
1.23 xsa 292: if (verbosity > 1)
1.45 xsa 293: cvs_log(LP_NOTICE, "cannot find revision "
294: "control file for `%s'", cf->file_name);
295: return (-1);
1.14 jfb 296: }
1.1 jfb 297:
1.60 tobias 298: if (cvs_cmdop == CVS_OP_TAG) {
299: if (cf->file_ent == NULL)
300: return (-1);
301: srev = cf->file_ent->ce_rev;
302: } else
303: srev = cf->file_rcsrev;
304:
1.45 xsa 305: if (cvs_noexec == 1)
1.32 xsa 306: return (0);
1.1 jfb 307:
1.60 tobias 308: (void)rcsnum_tostr(srev, revbuf, sizeof(revbuf));
1.58 tobias 309:
1.46 xsa 310: trev = rcs_sym_getrev(cf->file_rcs, tag_name);
311: if (trev != NULL) {
1.60 tobias 312: if (rcsnum_cmp(srev, trev, 0) == 0) {
1.46 xsa 313: rcsnum_free(trev);
314: return (-1);
315: }
316: (void)rcsnum_tostr(trev, trevbuf, sizeof(trevbuf));
317:
1.52 xsa 318: if (!(runflags & T_FORCE_MOVE)) {
1.46 xsa 319: cvs_printf("W %s : %s ", cf->file_path, tag_name);
320: cvs_printf("already exists on version %s", trevbuf);
321: cvs_printf(" : NOT MOVING tag to version %s\n", revbuf);
322:
323: return (-1);
1.67 joris 324: } else {
1.46 xsa 325: sym = rcs_sym_get(cf->file_rcs, tag_name);
1.60 tobias 326: rcsnum_cpy(srev, sym->rs_num, 0);
1.46 xsa 327: cf->file_rcs->rf_flags &= ~RCS_SYNCED;
328:
329: return (0);
330: }
331: }
332:
1.59 tobias 333: if (runflags & T_BRANCH) {
1.60 tobias 334: if ((trev = rcsnum_new_branch(srev)) == NULL)
1.59 tobias 335: fatal("Cannot create a new branch");
336:
337: for (;;) {
338: TAILQ_FOREACH(sym, &(cf->file_rcs->rf_symbols), rs_list)
339: if (!rcsnum_cmp(sym->rs_num, trev, 0))
340: break;
341:
342: if (sym != NULL) {
343: if (rcsnum_inc(trev) == NULL)
344: fatal("New revision too high");
345: if (rcsnum_inc(trev) == NULL)
346: fatal("New revision too high");
347: } else
348: break;
349: }
350: } else {
351: trev = rcsnum_alloc();
1.60 tobias 352: rcsnum_cpy(srev, trev, 0);
1.59 tobias 353: }
354:
1.68 joris 355: if ((ret = rcs_sym_add(cf->file_rcs, tag_name, trev)) != 0) {
356: if (ret != 1) {
1.45 xsa 357: cvs_log(LP_NOTICE,
358: "failed to set tag %s to revision %s in %s",
359: tag_name, revbuf, cf->file_rcs->rf_path);
1.39 xsa 360: }
1.59 tobias 361: rcsnum_free(trev);
1.45 xsa 362: return (-1);
1.1 jfb 363: }
1.17 jfb 364:
1.59 tobias 365: rcsnum_free(trev);
1.14 jfb 366: return (0);
1.1 jfb 367: }