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