Annotation of src/usr.bin/cvs/tag.c, Revision 1.77
1.77 ! tobias 1: /* $OpenBSD: tag.c,v 1.76 2008/06/20 14:04:29 tobias 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:
229: cvs_trigger_freeinfo(&files_info);
1.7 joris 230: return (0);
1.74 joris 231: }
232:
233: void
234: cvs_tag_check_files(struct cvs_file *cf)
235: {
236: RCSNUM *srev = NULL, *rev = NULL;
237: char rbuf[CVS_REV_BUFSZ];
238: struct file_info *fi;
239:
240: cvs_log(LP_TRACE, "cvs_tag_check_files(%s)", cf->file_path);
241:
242: cvs_file_classify(cf, tag);
243:
244: if (cf->file_type == CVS_DIR)
245: return;
246:
247: if (runflags & T_CHECK_UPTODATE) {
248: if (cf->file_status != FILE_UPTODATE &&
249: cf->file_status != FILE_CHECKOUT &&
250: cf->file_status != FILE_PATCH) {
251: return;
252: }
253: }
254:
255: switch (cf->file_status) {
256: case FILE_ADDED:
257: case FILE_REMOVED:
258: return;
259: default:
260: break;
261: }
262:
263: if (cvs_cmdop == CVS_OP_TAG) {
264: if (cf->file_ent == NULL)
265: return;
266: srev = cf->file_ent->ce_rev;
267: } else
268: srev = cf->file_rcsrev;
269:
270: rcsnum_tostr(srev, rbuf, sizeof(rbuf));
1.77 ! tobias 271: fi = xcalloc(1, sizeof(*fi));
! 272: fi->nrevstr = xstrdup(rbuf);
1.74 joris 273: fi->file_path = xstrdup(cf->file_path);
274:
275: if (tag_oldname != NULL)
276: fi->tag_old = xstrdup(tag_oldname);
277: else if (tag_date != NULL)
278: fi->tag_old = xstrdup(tag_date);
279:
280: if ((rev = rcs_sym_getrev(cf->file_rcs, tag_name)) != NULL) {
281: if (!rcsnum_differ(srev, rev))
282: goto bad;
283: rcsnum_tostr(rev, rbuf, sizeof(rbuf));
284: fi->crevstr = xstrdup(rbuf);
285: } else if (runflags & T_DELETE)
286: goto bad;
287:
288: fi->tag_new = xstrdup(tag_name);
289:
290: if (runflags & T_BRANCH)
291: fi->tag_type = 'T';
292: else if (runflags & T_DELETE)
293: fi->tag_type = '?';
294: else
295: fi->tag_type = 'N';
296:
297: if (runflags & T_FORCE_MOVE)
298: fi->tag_op = "mov";
299: else if (runflags & T_DELETE)
300: fi->tag_op = "del";
301: else
302: fi->tag_op = "add";
303:
304: TAILQ_INSERT_TAIL(&files_info, fi, flist);
305: return;
306:
307: bad:
308: if (fi->file_path != NULL)
309: xfree(fi->file_path);
310: if (fi->crevstr != NULL)
311: xfree(fi->crevstr);
312: if (fi->nrevstr != NULL)
313: xfree(fi->nrevstr);
314: if (fi->tag_new != NULL)
315: xfree(fi->tag_new);
316: if (fi->tag_old != NULL)
317: xfree(fi->tag_old);
318: if (rev != NULL)
319: rcsnum_free(rev);
320: xfree(fi);
1.7 joris 321: }
322:
1.45 xsa 323: void
324: cvs_tag_local(struct cvs_file *cf)
1.7 joris 325: {
1.45 xsa 326: cvs_log(LP_TRACE, "cvs_tag_local(%s)", cf->file_path);
1.17 jfb 327:
1.60 tobias 328: cvs_file_classify(cf, tag);
329:
1.45 xsa 330: if (cf->file_type == CVS_DIR) {
331: if (verbosity > 1) {
332: cvs_log(LP_NOTICE, "%s %s",
1.52 xsa 333: (runflags & T_DELETE) ? "Untagging" : "Tagging",
1.45 xsa 334: cf->file_path);
335: }
336: return;
337: }
1.40 xsa 338:
1.52 xsa 339: if (runflags & T_CHECK_UPTODATE) {
340: if (cf->file_status != FILE_UPTODATE &&
341: cf->file_status != FILE_CHECKOUT &&
342: cf->file_status != FILE_PATCH) {
343: cvs_log(LP_NOTICE,
344: "%s is locally modified", cf->file_path);
345: return;
346: }
347: }
348:
349: if (runflags & T_DELETE) {
1.45 xsa 350: if (tag_del(cf) == 0) {
351: if (verbosity > 0)
352: cvs_printf("D %s\n", cf->file_path);
1.21 xsa 353:
1.45 xsa 354: rcs_write(cf->file_rcs);
355: }
356: return;
357: }
1.21 xsa 358:
1.69 deraadt 359: switch (cf->file_status) {
1.45 xsa 360: case FILE_ADDED:
361: if (verbosity > 1) {
362: cvs_log(LP_NOTICE,
363: "couldn't tag added but un-commited file `%s'",
364: cf->file_path);
1.35 joris 365: }
1.45 xsa 366: return;
367: case FILE_REMOVED:
368: if (verbosity > 1) {
369: cvs_log(LP_NOTICE,
370: "skipping removed but un-commited file `%s'",
371: cf->file_path);
372: }
373: return;
374: case FILE_CHECKOUT:
375: case FILE_MODIFIED:
1.60 tobias 376: case FILE_PATCH:
1.45 xsa 377: case FILE_UPTODATE:
378: if (tag_add(cf) == 0) {
379: if (verbosity > 0)
380: cvs_printf("T %s\n", cf->file_path);
1.1 jfb 381:
1.45 xsa 382: rcs_write(cf->file_rcs);
1.56 joris 383: cvs_history_add(CVS_HISTORY_TAG, cf, tag_name);
1.35 joris 384: }
1.45 xsa 385: break;
386: default:
387: break;
1.1 jfb 388: }
389: }
390:
1.14 jfb 391: static int
1.45 xsa 392: tag_del(struct cvs_file *cf)
1.1 jfb 393: {
1.45 xsa 394: if (cf->file_rcs == NULL)
395: return (-1);
1.1 jfb 396:
1.45 xsa 397: if (cvs_noexec == 1)
1.35 joris 398: return (0);
1.1 jfb 399:
1.45 xsa 400: return (rcs_sym_remove(cf->file_rcs, tag_name));
1.14 jfb 401: }
402:
403: static int
1.45 xsa 404: tag_add(struct cvs_file *cf)
1.14 jfb 405: {
1.68 joris 406: int ret;
1.57 xsa 407: char revbuf[CVS_REV_BUFSZ], trevbuf[CVS_REV_BUFSZ];
1.60 tobias 408: RCSNUM *srev, *trev;
1.46 xsa 409: struct rcs_sym *sym;
1.14 jfb 410:
1.45 xsa 411: if (cf->file_rcs == NULL) {
1.23 xsa 412: if (verbosity > 1)
1.45 xsa 413: cvs_log(LP_NOTICE, "cannot find revision "
414: "control file for `%s'", cf->file_name);
415: return (-1);
1.14 jfb 416: }
1.1 jfb 417:
1.60 tobias 418: if (cvs_cmdop == CVS_OP_TAG) {
419: if (cf->file_ent == NULL)
420: return (-1);
421: srev = cf->file_ent->ce_rev;
422: } else
423: srev = cf->file_rcsrev;
424:
1.45 xsa 425: if (cvs_noexec == 1)
1.32 xsa 426: return (0);
1.1 jfb 427:
1.60 tobias 428: (void)rcsnum_tostr(srev, revbuf, sizeof(revbuf));
1.58 tobias 429:
1.46 xsa 430: trev = rcs_sym_getrev(cf->file_rcs, tag_name);
431: if (trev != NULL) {
1.60 tobias 432: if (rcsnum_cmp(srev, trev, 0) == 0) {
1.46 xsa 433: rcsnum_free(trev);
434: return (-1);
435: }
436: (void)rcsnum_tostr(trev, trevbuf, sizeof(trevbuf));
1.72 joris 437: rcsnum_free(trev);
1.46 xsa 438:
1.52 xsa 439: if (!(runflags & T_FORCE_MOVE)) {
1.46 xsa 440: cvs_printf("W %s : %s ", cf->file_path, tag_name);
441: cvs_printf("already exists on version %s", trevbuf);
442: cvs_printf(" : NOT MOVING tag to version %s\n", revbuf);
443:
444: return (-1);
1.67 joris 445: } else {
1.46 xsa 446: sym = rcs_sym_get(cf->file_rcs, tag_name);
1.60 tobias 447: rcsnum_cpy(srev, sym->rs_num, 0);
1.46 xsa 448: cf->file_rcs->rf_flags &= ~RCS_SYNCED;
449:
450: return (0);
451: }
452: }
453:
1.59 tobias 454: if (runflags & T_BRANCH) {
1.75 tobias 455: if ((trev = rcs_branch_new(cf->file_rcs, srev)) == NULL)
1.59 tobias 456: fatal("Cannot create a new branch");
457: } else {
458: trev = rcsnum_alloc();
1.60 tobias 459: rcsnum_cpy(srev, trev, 0);
1.59 tobias 460: }
461:
1.68 joris 462: if ((ret = rcs_sym_add(cf->file_rcs, tag_name, trev)) != 0) {
463: if (ret != 1) {
1.45 xsa 464: cvs_log(LP_NOTICE,
465: "failed to set tag %s to revision %s in %s",
466: tag_name, revbuf, cf->file_rcs->rf_path);
1.39 xsa 467: }
1.59 tobias 468: rcsnum_free(trev);
1.45 xsa 469: return (-1);
1.1 jfb 470: }
1.17 jfb 471:
1.59 tobias 472: rcsnum_free(trev);
1.14 jfb 473: return (0);
1.1 jfb 474: }