GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/cvs/tag.c Lines: 103 228 45.2 %
Date: 2017-11-07 Branches: 58 150 38.7 %

Line Branch Exec Source
1
/*	$OpenBSD: tag.c,v 1.87 2017/06/01 08:08:24 joris Exp $	*/
2
/*
3
 * Copyright (c) 2006 Xavier Santolaria <xsa@openbsd.org>
4
 *
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.
8
 *
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.
16
 */
17
18
#include <errno.h>
19
#include <stdlib.h>
20
#include <string.h>
21
#include <unistd.h>
22
23
#include "cvs.h"
24
#include "remote.h"
25
26
#define T_CHECK_UPTODATE	0x01
27
#define T_DELETE		0x02
28
#define T_FORCE_MOVE		0x04
29
#define T_BRANCH		0x08
30
31
void	cvs_tag_check_files(struct cvs_file *);
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
37
struct file_info_list	files_info;
38
39
static int	runflags = 0;
40
static int	tag_errors = 0;
41
static char	*tag = NULL;
42
static char	*tag_date = NULL;
43
static char	*tag_name = NULL;
44
static char	*tag_oldname = NULL;
45
46
struct cvs_cmd cvs_cmd_rtag = {
47
	CVS_OP_RTAG, CVS_LOCK_REPO, "rtag",
48
	{ "rt", "rfreeze" },
49
	"Add a symbolic tag to a module",
50
	"[-bcdFflR] [-D date | -r rev] symbolic_tag module ...",
51
	"bcD:dFflRr:",
52
	NULL,
53
	cvs_tag
54
};
55
56
struct cvs_cmd cvs_cmd_tag = {
57
	CVS_OP_TAG, CVS_USE_WDIR | CVS_LOCK_REPO, "tag",
58
	{ "ta", "freeze" },
59
	"Add a symbolic tag to checked out version of files",
60
	"[-bcdFflR] [-D date | -r rev] symbolic_tag [file ...]",
61
	"bcD:dFflRr:",
62
	NULL,
63
	cvs_tag
64
};
65
66
int
67
cvs_tag(int argc, char **argv)
68
{
69
	int ch, flags, i;
70
6
	char repo[PATH_MAX];
71
3
	char *arg = ".";
72
3
	struct cvs_recursion cr;
73
	struct trigger_list *line_list;
74
75
	flags = CR_RECURSE_DIRS;
76
77
13
	while ((ch = getopt(argc, argv, cvs_cmdop == CVS_OP_TAG ?
78
5
	    cvs_cmd_tag.cmd_opts : cvs_cmd_rtag.cmd_opts)) != -1) {
79


2
		switch (ch) {
80
		case 'b':
81
2
			runflags |= T_BRANCH;
82
2
			break;
83
		case 'c':
84
			runflags |= T_CHECK_UPTODATE;
85
			break;
86
		case 'D':
87
			tag_date = optarg;
88
			break;
89
		case 'd':
90
			runflags |= T_DELETE;
91
			break;
92
		case 'F':
93
			runflags |= T_FORCE_MOVE;
94
			break;
95
		case 'l':
96
			flags &= ~CR_RECURSE_DIRS;
97
			break;
98
		case 'R':
99
			flags |= CR_RECURSE_DIRS;
100
			break;
101
		case 'r':
102
			tag_oldname = optarg;
103
			break;
104
		default:
105
			fatal("%s", cvs_cmdop == CVS_OP_TAG ?
106
			    cvs_cmd_tag.cmd_synopsis :
107
			    cvs_cmd_rtag.cmd_synopsis);
108
		}
109
	}
110
111
3
	argc -= optind;
112
3
	argv += optind;
113
114
3
	if (cvs_cmdop == CVS_OP_RTAG) {
115
1
		flags |= CR_REPO;
116
117
1
		if (argc < 2)
118
			fatal("%s", cvs_cmd_rtag.cmd_synopsis);
119
120
4
		for (i = 1; i < argc; i++) {
121
1
			if (argv[i][0] == '/') {
122
				fatal("Absolute path name is invalid: %s",
123
				    argv[i]);
124
			}
125
		}
126
2
	} else if (cvs_cmdop == CVS_OP_TAG && argc == 0) {
127
		fatal("%s", cvs_cmd_tag.cmd_synopsis);
128
	}
129
130
3
	tag_name = argv[0];
131
3
	argc--;
132
3
	argv++;
133
134
3
	if (!rcs_sym_check(tag_name)) {
135
		fatal("tag `%s' must not contain the characters `%s'",
136
		    tag_name, RCS_SYM_INVALCHAR);
137
	}
138
139
3
	if (tag_oldname != NULL) {
140
		if (runflags & T_DELETE)
141
			tag_oldname = NULL;
142
		else
143
			tag = tag_oldname;
144
	}
145
146
3
	if (tag_date != NULL) {
147
		if (runflags & T_DELETE)
148
			tag_date = NULL;
149
		else
150
			tag = tag_date;
151
	}
152
153
3
	if (tag_oldname != NULL && tag_date != NULL)
154
		fatal("-r and -D options are mutually exclusive");
155
156
3
	cr.enterdir = NULL;
157
3
	cr.leavedir = NULL;
158
159
3
	if (cvsroot_is_remote()) {
160
		cvs_client_connect_to_server();
161
		cr.fileproc = cvs_client_sendfile;
162
163
		if (argc > 0)
164
			cvs_file_run(argc, argv, &cr);
165
		else
166
			cvs_file_run(1, &arg, &cr);
167
168
		if (runflags & T_BRANCH)
169
			cvs_client_send_request("Argument -b");
170
171
		if (runflags & T_CHECK_UPTODATE)
172
			cvs_client_send_request("Argument -c");
173
174
		if (runflags & T_DELETE)
175
			cvs_client_send_request("Argument -d");
176
177
		if (runflags & T_FORCE_MOVE)
178
			cvs_client_send_request("Argument -F");
179
180
		if (!(flags & CR_RECURSE_DIRS))
181
			cvs_client_send_request("Argument -l");
182
183
		if (tag_date != NULL)
184
			cvs_client_send_request("Argument -D%s", tag_date);
185
186
		if (tag_oldname != NULL)
187
			cvs_client_send_request("Argument -r%s", tag_oldname);
188
189
		cvs_client_send_request("Argument %s", tag_name);
190
		cvs_client_send_files(argv, argc);
191
		cvs_client_senddir(".");
192
		cvs_client_send_request((cvs_cmdop == CVS_OP_RTAG) ?
193
		    "rtag" : "tag");
194
		cvs_client_get_responses();
195
196
		return (0);
197
	}
198
199

4
	if (cvs_cmdop == CVS_OP_RTAG && chdir(current_cvsroot->cr_dir) == -1)
200
		fatal("cvs_tag: %s", strerror(errno));
201
202
3
	TAILQ_INIT(&files_info);
203
3
	cvs_get_repository_name(".", repo, PATH_MAX);
204
3
	line_list = cvs_trigger_getlines(CVS_PATH_TAGINFO, repo);
205
206
3
	cr.flags = flags;
207
3
	cr.fileproc = cvs_tag_check_files;
208
209
3
	if (argc > 0)
210
1
		cvs_file_run(argc, argv, &cr);
211
	else
212
2
		cvs_file_run(1, &arg, &cr);
213
214
3
	if (tag_errors)
215
		fatal("correct the above errors first!");
216
217
3
	if (line_list != NULL) {
218
		if (cvs_trigger_handle(CVS_TRIGGER_TAGINFO, repo, NULL,
219
		    line_list, &files_info))
220
			fatal("Pre-tag check failed");
221
		cvs_trigger_freelist(line_list);
222
	}
223
224
3
	cr.fileproc = cvs_tag_local;
225
226
3
	if (argc > 0)
227
1
		cvs_file_run(argc, argv, &cr);
228
	else
229
2
		cvs_file_run(1, &arg, &cr);
230
231
3
	if (line_list != NULL)
232
		cvs_trigger_freeinfo(&files_info);
233
234
3
	return (0);
235
3
}
236
237
void
238
cvs_tag_check_files(struct cvs_file *cf)
239
{
240
	RCSNUM *srev = NULL, *rev = NULL;
241
24
	char rbuf[CVS_REV_BUFSZ];
242
	struct file_info *fi;
243
244
12
	cvs_log(LP_TRACE, "cvs_tag_check_files(%s)", cf->file_path);
245
246
12
	cvs_file_classify(cf, tag);
247
248

19
	if (cf->file_type == CVS_DIR || cf->file_status == FILE_UNKNOWN)
249
5
		return;
250
251
7
	if (runflags & T_CHECK_UPTODATE) {
252
		if (cf->file_status != FILE_UPTODATE &&
253
		    cf->file_status != FILE_CHECKOUT &&
254
		    cf->file_status != FILE_PATCH) {
255
			tag_errors++;
256
			cvs_log(LP_NOTICE,
257
			    "%s is locally modified", cf->file_path);
258
			return;
259
		}
260
	}
261
262
7
	switch (cf->file_status) {
263
	case FILE_ADDED:
264
	case FILE_REMOVED:
265
		return;
266
	default:
267
		break;
268
	}
269
270
7
	if (cvs_cmdop == CVS_OP_TAG) {
271
4
		if (cf->file_ent == NULL)
272
			return;
273
4
		srev = cf->file_ent->ce_rev;
274
4
	} else
275
3
		srev = cf->file_rcsrev;
276
277
7
	rcsnum_tostr(srev, rbuf, sizeof(rbuf));
278
7
	fi = xcalloc(1, sizeof(*fi));
279
7
	fi->nrevstr = xstrdup(rbuf);
280
7
	fi->file_path = xstrdup(cf->file_path);
281
282
7
	if (tag_oldname != NULL)
283
		fi->tag_old = xstrdup(tag_oldname);
284
7
	else if (tag_date != NULL)
285
		fi->tag_old = xstrdup(tag_date);
286
287
7
	if ((rev = rcs_sym_getrev(cf->file_rcs, tag_name)) != NULL) {
288
		if (!rcsnum_differ(srev, rev))
289
			goto bad;
290
		rcsnum_tostr(rev, rbuf, sizeof(rbuf));
291
		fi->crevstr = xstrdup(rbuf);
292
		free(rev);
293
7
	} else if (runflags & T_DELETE)
294
		goto bad;
295
296
7
	fi->tag_new = xstrdup(tag_name);
297
298
7
	if (runflags & T_BRANCH)
299
6
		fi->tag_type = 'T';
300
1
	else if (runflags & T_DELETE)
301
		fi->tag_type = '?';
302
	else
303
		fi->tag_type = 'N';
304
305
7
	if (runflags & T_FORCE_MOVE)
306
		fi->tag_op = "mov";
307
7
	else if (runflags & T_DELETE)
308
		fi->tag_op = "del";
309
	else
310
		fi->tag_op = "add";
311
312
7
	TAILQ_INSERT_TAIL(&files_info, fi, flist);
313
7
	return;
314
315
bad:
316
	free(fi->file_path);
317
	free(fi->crevstr);
318
	free(fi->nrevstr);
319
	free(fi->tag_new);
320
	free(fi->tag_old);
321
	free(rev);
322
	free(fi);
323
12
}
324
325
void
326
cvs_tag_local(struct cvs_file *cf)
327
{
328
24
	cvs_log(LP_TRACE, "cvs_tag_local(%s)", cf->file_path);
329
330
12
	cvs_file_classify(cf, tag);
331
332
12
	if (cf->file_type == CVS_DIR) {
333
5
		if (verbosity > 1) {
334
			cvs_log(LP_NOTICE, "%s %s",
335
			    (runflags & T_DELETE) ? "Untagging" : "Tagging",
336
			    cf->file_path);
337
		}
338
		return;
339
	}
340
341
7
	if (runflags & T_DELETE) {
342
		if (tag_del(cf) == 0) {
343
			if (verbosity > 0)
344
				cvs_printf("D %s\n", cf->file_path);
345
		}
346
		return;
347
	}
348
349

16
	switch (cf->file_status) {
350
	case FILE_ADDED:
351
		if (verbosity > 1) {
352
			cvs_log(LP_NOTICE,
353
			    "couldn't tag added but un-committed file `%s'",
354
			    cf->file_path);
355
		}
356
		break;
357
	case FILE_REMOVED:
358
		if (verbosity > 1) {
359
			cvs_log(LP_NOTICE,
360
			    "skipping removed but un-committed file `%s'",
361
			    cf->file_path);
362
		}
363
		break;
364
	case FILE_CHECKOUT:
365
	case FILE_MODIFIED:
366
	case FILE_PATCH:
367
	case FILE_UPTODATE:
368
7
		if (tag_add(cf) == 0) {
369
7
			if (verbosity > 0)
370
				cvs_printf("T %s\n", cf->file_path);
371
7
			cvs_history_add(CVS_HISTORY_TAG, cf, tag_name);
372
7
		}
373
		break;
374
	default:
375
		break;
376
	}
377
12
}
378
379
static int
380
tag_del(struct cvs_file *cf)
381
{
382
	if (cf->file_rcs == NULL)
383
		return (-1);
384
385
	if (cvs_noexec == 1)
386
		return (0);
387
388
	return (rcs_sym_remove(cf->file_rcs, tag_name));
389
}
390
391
static int
392
tag_add(struct cvs_file *cf)
393
{
394
	int ret;
395
14
	char revbuf[CVS_REV_BUFSZ], trevbuf[CVS_REV_BUFSZ];
396
	RCSNUM *srev, *trev;
397
	struct rcs_sym *sym;
398
399
7
	if (cf->file_rcs == NULL) {
400
		if (verbosity > 1)
401
			cvs_log(LP_NOTICE, "cannot find revision "
402
			    "control file for `%s'", cf->file_name);
403
		return (-1);
404
	}
405
406
7
	if (cvs_cmdop == CVS_OP_TAG) {
407
4
		if (cf->file_ent == NULL)
408
			return (-1);
409
4
		srev = cf->file_ent->ce_rev;
410
4
	} else
411
3
		srev = cf->file_rcsrev;
412
413
7
	if (cvs_noexec == 1)
414
		return (0);
415
416
7
	(void)rcsnum_tostr(srev, revbuf, sizeof(revbuf));
417
418
7
	trev = rcs_sym_getrev(cf->file_rcs, tag_name);
419
7
	if (trev != NULL) {
420
		if (rcsnum_cmp(srev, trev, 0) == 0) {
421
			free(trev);
422
			return (-1);
423
		}
424
		(void)rcsnum_tostr(trev, trevbuf, sizeof(trevbuf));
425
		free(trev);
426
427
		if (!(runflags & T_FORCE_MOVE)) {
428
			cvs_printf("W %s : %s ", cf->file_path, tag_name);
429
			cvs_printf("already exists on version %s", trevbuf);
430
			cvs_printf(" : NOT MOVING tag to version %s\n", revbuf);
431
432
			return (-1);
433
		} else {
434
			sym = rcs_sym_get(cf->file_rcs, tag_name);
435
			rcsnum_cpy(srev, sym->rs_num, 0);
436
			cf->file_rcs->rf_flags &= ~RCS_SYNCED;
437
438
			return (0);
439
		}
440
	}
441
442
7
	if (runflags & T_BRANCH) {
443
6
		if ((trev = rcs_branch_new(cf->file_rcs, srev)) == NULL)
444
			fatal("Cannot create a new branch");
445
	} else {
446
1
		trev = rcsnum_alloc();
447
1
		rcsnum_cpy(srev, trev, 0);
448
	}
449
450
7
	if ((ret = rcs_sym_add(cf->file_rcs, tag_name, trev)) != 0) {
451
		if (ret != 1) {
452
			cvs_log(LP_NOTICE,
453
			    "failed to set tag %s to revision %s in %s",
454
			    tag_name, revbuf, cf->file_rcs->rf_path);
455
		}
456
		free(trev);
457
		return (-1);
458
	}
459
460
7
	free(trev);
461
7
	return (0);
462
7
}