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