xref: /openbsd-src/usr.bin/cvs/status.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: status.c,v 1.92 2009/03/21 15:33:22 joris Exp $	*/
2 /*
3  * Copyright (c) 2006 Joris Vink <joris@openbsd.org>
4  * Copyright (c) 2005-2008 Xavier Santolaria <xsa@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <string.h>
20 #include <time.h>
21 #include <unistd.h>
22 
23 #include "cvs.h"
24 #include "remote.h"
25 
26 void	cvs_status_local(struct cvs_file *);
27 
28 static int show_sym = 0;
29 
30 struct cvs_cmd cvs_cmd_status = {
31 	CVS_OP_STATUS, CVS_USE_WDIR, "status",
32 	{ "st", "stat" },
33 	"Display status information on checked out files",
34 	"[-lRv]",
35 	"lRv",
36 	NULL,
37 	cvs_status
38 };
39 
40 #define CVS_STATUS_SEP	\
41 	"==================================================================="
42 
43 const char *status_tab[] = {
44 	"Unknown",
45 	"Locally Added",
46 	"Locally Removed",
47 	"Locally Modified",
48 	"Up-to-date",
49 	"Needs Checkout",
50 	"Needs Checkout",
51 	"Needs Merge",
52 	"Needs Patch",
53 	"Entry Invalid",
54 	"Unresolved Conflict",
55 	"Classifying error",
56 };
57 
58 int
59 cvs_status(int argc, char **argv)
60 {
61 	int ch, flags;
62 	char *arg = ".";
63 	struct cvs_recursion cr;
64 
65 	flags = CR_RECURSE_DIRS;
66 
67 	while ((ch = getopt(argc, argv, cvs_cmd_status.cmd_opts)) != -1) {
68 		switch (ch) {
69 		case 'l':
70 			flags &= ~CR_RECURSE_DIRS;
71 			break;
72 		case 'R':
73 			flags |= CR_RECURSE_DIRS;
74 			break;
75 		case 'v':
76 			show_sym = 1;
77 			break;
78 		default:
79 			fatal("%s", cvs_cmd_status.cmd_synopsis);
80 		}
81 	}
82 
83 	argc -= optind;
84 	argv += optind;
85 
86 	cr.enterdir = NULL;
87 	cr.leavedir = NULL;
88 
89 	if (current_cvsroot->cr_method == CVS_METHOD_LOCAL) {
90 		flags |= CR_REPO;
91 		cr.fileproc = cvs_status_local;
92 	} else {
93 		cvs_client_connect_to_server();
94 		if (!(flags & CR_RECURSE_DIRS))
95 			cvs_client_send_request("Argument -l");
96 		if (show_sym)
97 			cvs_client_send_request("Argument -v");
98 		cr.fileproc = cvs_client_sendfile;
99 	}
100 
101 	cr.flags = flags;
102 
103 	if (argc > 0)
104 		cvs_file_run(argc, argv, &cr);
105 	else
106 		cvs_file_run(1, &arg, &cr);
107 
108 	if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
109 		cvs_client_send_files(argv, argc);
110 		cvs_client_senddir(".");
111 		cvs_client_send_request("status");
112 		cvs_client_get_responses();
113 	}
114 
115 	return (0);
116 }
117 
118 void
119 cvs_status_local(struct cvs_file *cf)
120 {
121 	size_t len;
122 	RCSNUM *head;
123 	const char *status;
124 	char buf[128], timebuf[CVS_TIME_BUFSZ], revbuf[CVS_REV_BUFSZ];
125 	struct rcs_sym *sym;
126 
127 	cvs_log(LP_TRACE, "cvs_status_local(%s)", cf->file_path);
128 
129 	cvs_file_classify(cf, cvs_directory_tag);
130 
131 	if (cf->file_type == CVS_DIR) {
132 		if (verbosity > 1)
133 			cvs_log(LP_NOTICE, "Examining %s", cf->file_path);
134 		return;
135 	}
136 
137 	if (cf->file_status == FILE_UPTODATE &&
138 	    !(cf->file_flags & FILE_ON_DISK) &&
139 	    !(cf->file_flags & FILE_USER_SUPPLIED))
140 		return;
141 
142 	if (cf->file_rcs != NULL)
143 		head = cf->file_rcsrev;
144 	else
145 		head = NULL;
146 
147 	cvs_printf("%s\n", CVS_STATUS_SEP);
148 
149 	if (cf->file_rcs != NULL && head == NULL)
150 		status = status_tab[FILE_UNKNOWN];
151 	else
152 		status = status_tab[cf->file_status];
153 
154 	if (cf->file_status == FILE_MODIFIED &&
155 	    cf->file_ent->ce_conflict != NULL)
156 		status = "File had conflicts on merge";
157 
158 	if (!(cf->file_flags & FILE_ON_DISK)) {
159 		(void)xsnprintf(buf, sizeof(buf), "no file %s\t",
160 		    cf->file_name);
161 	} else
162 		if (strlcpy(buf, cf->file_name, sizeof(buf)) >= sizeof(buf))
163 			fatal("cvs_status_local: overflow");
164 
165 	cvs_printf("File: %-17s\tStatus: %s\n\n", buf, status);
166 
167 	if (cf->file_ent == NULL) {
168 		(void)xsnprintf(buf, sizeof(buf),
169 		    "No entry for %s", cf->file_name);
170 	} else if (cf->file_ent->ce_status == CVS_ENT_ADDED) {
171 		len = strlcpy(buf, "New file!", sizeof(buf));
172 		if (len >= sizeof(buf))
173 			fatal("cvs_status_local: truncation");
174 	} else {
175 		rcsnum_tostr(cf->file_ent->ce_rev, revbuf, sizeof(revbuf));
176 
177 		if (cf->file_ent->ce_conflict == NULL) {
178 			ctime_r(&(cf->file_ent->ce_mtime), timebuf);
179 			if (timebuf[strlen(timebuf) - 1] == '\n')
180 				timebuf[strlen(timebuf) - 1] = '\0';
181 		} else {
182 			len = strlcpy(timebuf, cf->file_ent->ce_conflict,
183 			    sizeof(timebuf));
184 			if (len >= sizeof(timebuf))
185 				fatal("cvs_status_local: truncation");
186 		}
187 
188 		(void)strlcpy(buf, revbuf, sizeof(buf));
189 		if (cvs_server_active == 0) {
190 			(void)strlcat(buf, "\t", sizeof(buf));
191 			(void)strlcat(buf, timebuf, sizeof(buf));
192 		}
193 	}
194 
195 	cvs_printf("   Working revision:\t%s\n", buf);
196 
197 	buf[0] = '\0';
198 	if (cf->file_rcs == NULL) {
199 		len = strlcat(buf, "No revision control file", sizeof(buf));
200 		if (len >= sizeof(buf))
201 			fatal("cvs_status_local: truncation");
202 	} else if (head == NULL) {
203 		len = strlcat(buf, "No head revision", sizeof(buf));
204 		if (len >= sizeof(buf))
205 			fatal("cvs_status_local: truncation");
206 	} else {
207 		rcsnum_tostr(head, revbuf, sizeof(revbuf));
208 		(void)xsnprintf(buf, sizeof(buf), "%s\t%s", revbuf,
209 		    cf->file_rpath);
210 	}
211 
212 	cvs_printf("   Repository revision:\t%s\n", buf);
213 
214 	if (cf->file_ent != NULL) {
215 		if (cf->file_ent->ce_tag != NULL)
216 			cvs_printf("   Sticky Tag:\t\t%s\n",
217 			    cf->file_ent->ce_tag);
218 		else if (verbosity > 0)
219 			cvs_printf("   Sticky Tag:\t\t(none)\n");
220 
221 		if (cf->file_ent->ce_date != -1) {
222 			struct tm datetm;
223 			char datetmp[CVS_TIME_BUFSZ];
224 
225 			gmtime_r(&(cf->file_ent->ce_date), &datetm);
226                         (void)strftime(datetmp, sizeof(datetmp),
227 			    CVS_DATE_FMT, &datetm);
228 
229 			cvs_printf("   Sticky Date:\t\t%s\n", datetmp);
230 		} else if (verbosity > 0)
231 			cvs_printf("   Sticky Date:\t\t(none)\n");
232 
233 		if (cf->file_ent->ce_opts != NULL)
234 			cvs_printf("   Sticky Options:\t%s\n",
235 			    cf->file_ent->ce_opts);
236 		else if (verbosity > 0)
237 			cvs_printf("   Sticky Options:\t(none)\n");
238 	}
239 
240 	if (cf->file_rcs != NULL && show_sym == 1) {
241 		cvs_printf("\n");
242 		cvs_printf("   Existing Tags:\n");
243 
244 		if (!TAILQ_EMPTY(&(cf->file_rcs->rf_symbols))) {
245 			TAILQ_FOREACH(sym,
246 			    &(cf->file_rcs->rf_symbols), rs_list) {
247 				(void)rcsnum_tostr(sym->rs_num, revbuf,
248 				    sizeof(revbuf));
249 
250 				cvs_printf("\t%-25s\t(%s: %s)\n", sym->rs_name,
251 				    RCSNUM_ISBRANCH(sym->rs_num) ? "branch" :
252 				    "revision", revbuf);
253 			 }
254 		} else
255 			cvs_printf("\tNo Tags Exist\n");
256 	}
257 
258 	cvs_printf("\n");
259 }
260