summaryrefslogtreecommitdiff
path: root/usr.bin/cvs/status.c
blob: ed212edba887f134176430ac0f3ff4777cf4c490 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*	$OpenBSD: status.c,v 1.99 2017/05/28 16:58:54 joris Exp $	*/
/*
 * Copyright (c) 2006 Joris Vink <joris@openbsd.org>
 * Copyright (c) 2005-2008 Xavier Santolaria <xsa@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

#include "cvs.h"
#include "remote.h"

void	cvs_status_local(struct cvs_file *);

static int show_sym = 0;

struct cvs_cmd cvs_cmd_status = {
	CVS_OP_STATUS, CVS_USE_WDIR, "status",
	{ "st", "stat" },
	"Display status information on checked out files",
	"[-lRv]",
	"lRv",
	NULL,
	cvs_status
};

#define CVS_STATUS_SEP	\
	"==================================================================="

const char *status_tab[] = {
	"Unknown",
	"Locally Added",
	"Locally Removed",
	"Locally Modified",
	"Up-to-date",
	"Needs Checkout",
	"Needs Checkout",
	"Needs Merge",
	"Needs Patch",
	"Entry Invalid",
	"Unresolved Conflict",
	"Classifying error",
};

int
cvs_status(int argc, char **argv)
{
	int ch, flags;
	char *arg = ".";
	struct cvs_recursion cr;

	flags = CR_RECURSE_DIRS;

	while ((ch = getopt(argc, argv, cvs_cmd_status.cmd_opts)) != -1) {
		switch (ch) {
		case 'l':
			flags &= ~CR_RECURSE_DIRS;
			break;
		case 'R':
			flags |= CR_RECURSE_DIRS;
			break;
		case 'v':
			show_sym = 1;
			break;
		default:
			fatal("%s", cvs_cmd_status.cmd_synopsis);
		}
	}

	argc -= optind;
	argv += optind;

	cr.enterdir = NULL;
	cr.leavedir = NULL;

	if (current_cvsroot->cr_method == CVS_METHOD_LOCAL) {
		flags |= CR_REPO;
		cr.fileproc = cvs_status_local;
	} else {
		cvs_client_connect_to_server();
		if (!(flags & CR_RECURSE_DIRS))
			cvs_client_send_request("Argument -l");
		if (show_sym)
			cvs_client_send_request("Argument -v");
		cr.fileproc = cvs_client_sendfile;
	}

	cr.flags = flags;

	if (argc > 0)
		cvs_file_run(argc, argv, &cr);
	else
		cvs_file_run(1, &arg, &cr);

	if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
		cvs_client_send_files(argv, argc);
		cvs_client_senddir(".");
		cvs_client_send_request("status");
		cvs_client_get_responses();
	}

	return (0);
}

void
cvs_status_local(struct cvs_file *cf)
{
	size_t len;
	RCSNUM *head, *brev;
	const char *status;
	struct rcs_delta *rdp;
	char buf[PATH_MAX + CVS_REV_BUFSZ + 128];
	char timebuf[CVS_TIME_BUFSZ], revbuf[CVS_REV_BUFSZ];
	struct rcs_sym *sym;

	cvs_log(LP_TRACE, "cvs_status_local(%s)", cf->file_path);

	cvs_file_classify(cf, cvs_directory_tag);

	if (cf->file_type == CVS_DIR) {
		if (verbosity > 1)
			cvs_log(LP_NOTICE, "Examining %s", cf->file_path);
		return;
	}

	if (cf->file_status == FILE_UPTODATE &&
	    !(cf->file_flags & FILE_ON_DISK) &&
	    !(cf->file_flags & FILE_USER_SUPPLIED))
		return;

	if (cf->file_rcs != NULL)
		head = cf->file_rcsrev;
	else
		head = NULL;

	cvs_printf("%s\n", CVS_STATUS_SEP);

	if (cf->file_rcs != NULL && head == NULL)
		status = status_tab[FILE_UNKNOWN];
	else
		status = status_tab[cf->file_status];

	if (cf->file_status == FILE_MODIFIED &&
	    cf->file_ent->ce_conflict != NULL)
		status = "File had conflicts on merge";

	if (!(cf->file_flags & FILE_ON_DISK)) {
		(void)xsnprintf(buf, sizeof(buf), "no file %s\t",
		    cf->file_name);
	} else
		if (strlcpy(buf, cf->file_name, sizeof(buf)) >= sizeof(buf))
			fatal("cvs_status_local: overflow");

	cvs_printf("File: %-17s\tStatus: %s\n\n", buf, status);

	if (cf->file_ent == NULL) {
		(void)xsnprintf(buf, sizeof(buf),
		    "No entry for %s", cf->file_name);
	} else if (cf->file_ent->ce_status == CVS_ENT_ADDED) {
		len = strlcpy(buf, "New file!", sizeof(buf));
		if (len >= sizeof(buf))
			fatal("cvs_status_local: truncation");
	} else {
		rcsnum_tostr(cf->file_ent->ce_rev, revbuf, sizeof(revbuf));

		if (cf->file_ent->ce_conflict == NULL) {
			if (cvs_server_active == 0) {
				(void)strlcpy(timebuf, cf->file_ent->ce_time,
				    sizeof(timebuf));
			} else {
				timebuf[0] = '\0';
			}
		} else {
			len = strlcpy(timebuf, cf->file_ent->ce_conflict,
			    sizeof(timebuf));
			if (len >= sizeof(timebuf))
				fatal("cvs_status_local: truncation");
		}

		(void)strlcpy(buf, revbuf, sizeof(buf));
		if (cvs_server_active == 0) {
			(void)strlcat(buf, "\t", sizeof(buf));
			(void)strlcat(buf, timebuf, sizeof(buf));
		}
	}

	cvs_printf("   Working revision:\t%s\n", buf);

	buf[0] = '\0';
	if (cf->file_rcs == NULL) {
		len = strlcat(buf, "No revision control file", sizeof(buf));
		if (len >= sizeof(buf))
			fatal("cvs_status_local: truncation");
	} else if (head == NULL) {
		len = strlcat(buf, "No head revision", sizeof(buf));
		if (len >= sizeof(buf))
			fatal("cvs_status_local: truncation");
	} else {
		rcsnum_tostr(head, revbuf, sizeof(revbuf));
		(void)xsnprintf(buf, sizeof(buf), "%s\t%s", revbuf,
		    cf->file_rpath);
	}

	cvs_printf("   Repository revision:\t%s\n", buf);

	if (cf->file_rcs != NULL && head != NULL) {
		rdp = rcs_findrev(cf->file_rcs, head);
		if (rdp == NULL) {
			fatal("cvs_status_local: No head revision delta");
		}

		cvs_printf("   Commit Identifier:\t%s\n",
		    (rdp->rd_commitid != NULL) ? rdp->rd_commitid : "(none)");
	}

	if (cf->file_ent != NULL) {
		if (cf->file_ent->ce_tag != NULL) {
			if ((brev = rcs_sym_getrev(cf->file_rcs,
			    cf->file_ent->ce_tag)) == NULL) {
				(void)strlcpy(buf, "- MISSING from RCS file!",
				    sizeof(buf));
			} else {
				rcsnum_tostr(brev, revbuf, sizeof(revbuf));
				if (RCSNUM_ISBRANCH(brev)) {
					xsnprintf(buf, sizeof(buf),
					    "(branch: %s)", revbuf);
				} else {
					xsnprintf(buf, sizeof(buf),
					    "(revision: %s)", revbuf);
				}
				free(brev);
			}

			cvs_printf("   Sticky Tag:\t\t%s %s\n",
			    cf->file_ent->ce_tag, buf);
		} else if (verbosity > 0) {
			cvs_printf("   Sticky Tag:\t\t(none)\n");
		}

		if (cf->file_ent->ce_date != -1) {
			struct tm datetm;
			char datetmp[CVS_TIME_BUFSZ];

			gmtime_r(&(cf->file_ent->ce_date), &datetm);
                        (void)strftime(datetmp, sizeof(datetmp),
			    CVS_DATE_FMT, &datetm);

			cvs_printf("   Sticky Date:\t\t%s\n", datetmp);
		} else if (verbosity > 0)
			cvs_printf("   Sticky Date:\t\t(none)\n");

		if (cf->file_ent->ce_opts != NULL)
			cvs_printf("   Sticky Options:\t%s\n",
			    cf->file_ent->ce_opts);
		else if (verbosity > 0)
			cvs_printf("   Sticky Options:\t(none)\n");
	}

	if (cf->file_rcs != NULL && show_sym == 1) {
		cvs_printf("\n");
		cvs_printf("   Existing Tags:\n");

		if (!TAILQ_EMPTY(&(cf->file_rcs->rf_symbols))) {
			TAILQ_FOREACH(sym,
			    &(cf->file_rcs->rf_symbols), rs_list) {
				(void)rcsnum_tostr(sym->rs_num, revbuf,
				    sizeof(revbuf));

				cvs_printf("\t%-25s\t(%s: %s)\n", sym->rs_name,
				    RCSNUM_ISBRANCH(sym->rs_num) ? "branch" :
				    "revision", revbuf);
			 }
		} else
			cvs_printf("\tNo Tags Exist\n");
	}

	cvs_printf("\n");
}