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