Annotation of src/usr.bin/cvs/tag.c, Revision 1.6
1.6 ! tedu 1: /* $OpenBSD: tag.c,v 1.5 2005/02/28 20:18:02 joris Exp $ */
1.1 jfb 2: /*
3: * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
1.5 joris 4: * Copyright (c) 2004 Joris Vink <joris@openbsd.org>
1.1 jfb 5: * All rights reserved.
6: *
7: * Redistribution and use in source and binary forms, with or without
8: * modification, are permitted provided that the following conditions
9: * are met:
10: *
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. The name of the author may not be used to endorse or promote products
14: * derived from this software without specific prior written permission.
15: *
16: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
17: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
18: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
19: * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26: */
27:
28: #include <sys/types.h>
29: #include <sys/stat.h>
30:
31: #include <errno.h>
32: #include <stdio.h>
33: #include <fcntl.h>
34: #include <stdlib.h>
35: #include <unistd.h>
36: #include <string.h>
37: #include <sysexits.h>
38:
39: #include "cvs.h"
40: #include "log.h"
41: #include "proto.h"
42:
43:
44: int cvs_tag_file (CVSFILE *, void *);
45:
46:
47: /*
48: * cvs_tag()
49: *
50: * Handler for the `cvs tag' command.
51: * Returns 0 on success, or one of the known exit codes on error.
52: */
53: int
54: cvs_tag(int argc, char **argv)
55: {
56: int ch, flags;
57: struct cvsroot *root;
1.3 jfb 58: char *tag, *old_tag, *date;
1.1 jfb 59: int branch, delete;
60:
1.3 jfb 61: date = old_tag = NULL;
1.1 jfb 62: branch = delete = 0;
63: flags = CF_SORT|CF_IGNORE|CF_RECURSE;
64:
1.3 jfb 65: while ((ch = getopt(argc, argv, "bdD:lr:")) != -1) {
1.1 jfb 66: switch (ch) {
67: case 'b':
68: branch = 1;
69: break;
70: case 'd':
71: delete = 1;
72: break;
1.3 jfb 73: case 'D':
74: date = optarg;
75: break;
1.1 jfb 76: case 'l':
77: flags &= ~CF_RECURSE;
78: break;
79: case 'r':
80: old_tag = optarg;
81: break;
82: default:
83: return (EX_USAGE);
84: }
85: }
86:
87: argc -= optind;
88: argv += optind;
89:
90: if (argc == 0) {
91: return (EX_USAGE);
92: } else {
93: tag = argv[0];
94: argc--;
95: argv++;
96: }
97:
98: if (branch && delete) {
99: cvs_log(LP_WARN, "ignoring -b with -d options");
100: branch = 0;
101: }
102:
103: if (delete && old_tag)
104: old_tag = NULL;
105:
1.3 jfb 106: if (delete && date)
107: date = NULL;
108:
109: if (old_tag != NULL && date != NULL) {
110: cvs_log(LP_ERROR, "-r and -D options are mutually exclusive");
111: return (-1);
112: }
113:
1.1 jfb 114: if (argc == 0)
115: cvs_files = cvs_file_get(".", flags);
116: else
117: cvs_files = cvs_file_getspec(argv, argc, 0);
118: if (cvs_files == NULL)
119: return (EX_DATAERR);
120:
121: root = CVS_DIR_ROOT(cvs_files);
122: if (root == NULL) {
123: cvs_log(LP_ERR,
124: "No CVSROOT specified! Please use the `-d' option");
125: cvs_log(LP_ERR,
126: "or set the CVSROOT environment variable.");
127: return (EX_USAGE);
128: }
129:
130: if (root->cr_method != CVS_METHOD_LOCAL) {
1.2 jfb 131: if (cvs_connect(root) < 0)
132: return (EX_PROTOCOL);
133: if (branch && (cvs_sendarg(root, "-b", 0) < 0))
134: return (EX_PROTOCOL);
135: if (delete && (cvs_sendarg(root, "-d", 0) < 0))
136: return (EX_PROTOCOL);
1.1 jfb 137: if (old_tag) {
1.3 jfb 138: if ((cvs_sendarg(root, "-r", 0) < 0) ||
139: (cvs_sendarg(root, old_tag, 0) < 0))
140: return (EX_PROTOCOL);
141: }
142: if (date) {
143: if ((cvs_sendarg(root, "-D", 0) < 0) ||
144: (cvs_sendarg(root, date, 0) < 0))
145: return (EX_PROTOCOL);
1.1 jfb 146: }
1.2 jfb 147: if (cvs_sendarg(root, tag, 0) < 0)
148: return (EX_PROTOCOL);
1.1 jfb 149: }
150:
151: cvs_file_examine(cvs_files, cvs_tag_file, NULL);
152:
153: if (root->cr_method != CVS_METHOD_LOCAL) {
1.2 jfb 154: if (cvs_senddir(root, cvs_files) < 0)
155: return (EX_PROTOCOL);
156: if (cvs_sendreq(root, CVS_REQ_TAG, NULL) < 0)
157: return (EX_PROTOCOL);
1.1 jfb 158: }
159:
160: return (0);
161: }
162:
163:
164: /*
165: * cvs_tag_file()
166: *
167: * Get the status of a single file.
168: */
169: int
170: cvs_tag_file(CVSFILE *cfp, void *arg)
171: {
172: int ret;
173: char *repo, fpath[MAXPATHLEN], rcspath[MAXPATHLEN];
174: RCSFILE *rf;
175: struct cvs_ent *entp;
176: struct cvsroot *root;
177:
178: ret = 0;
179: rf = NULL;
180: root = CVS_DIR_ROOT(cfp);
181:
182: if ((root->cr_method != CVS_METHOD_LOCAL) && (cfp->cf_type == DT_DIR)) {
183: if (cvs_senddir(root, cfp) < 0)
184: return (-1);
185: return (0);
186: }
187:
188: cvs_file_getpath(cfp, fpath, sizeof(fpath));
189: entp = cvs_ent_getent(fpath);
190:
191: if (root->cr_method != CVS_METHOD_LOCAL) {
192: if ((entp != NULL) && (cvs_sendentry(root, entp) < 0)) {
193: cvs_ent_free(entp);
194: return (-1);
195: }
196:
197: switch (cfp->cf_cvstat) {
198: case CVS_FST_UNKNOWN:
199: ret = cvs_sendreq(root, CVS_REQ_QUESTIONABLE,
200: CVS_FILE_NAME(cfp));
201: break;
202: case CVS_FST_UPTODATE:
203: ret = cvs_sendreq(root, CVS_REQ_UNCHANGED,
204: CVS_FILE_NAME(cfp));
205: break;
206: case CVS_FST_MODIFIED:
207: ret = cvs_sendreq(root, CVS_REQ_ISMODIFIED,
208: CVS_FILE_NAME(cfp));
209: default:
210: break;
211: }
212: } else {
213: if (cfp->cf_cvstat == CVS_FST_UNKNOWN) {
214: cvs_log(LP_WARN, "I know nothing about %s", fpath);
215: return (0);
216: }
217:
218: if (cfp->cf_parent != NULL)
219: repo = cfp->cf_parent->cf_ddat->cd_repo;
220: else
221: repo = NULL;
222:
223: snprintf(rcspath, sizeof(rcspath), "%s/%s/%s%s",
224: root->cr_dir, repo, CVS_FILE_NAME(cfp), RCS_FILE_EXT);
225:
1.4 jfb 226: rf = rcs_open(rcspath, RCS_READ);
1.1 jfb 227: if (rf == NULL) {
1.6 ! tedu 228: if (entp != NULL)
! 229: cvs_ent_free(entp);
1.1 jfb 230: return (-1);
231: }
232:
233: rcs_close(rf);
234: }
235:
236: if (entp != NULL)
237: cvs_ent_free(entp);
238:
239: return (ret);
240: }