summaryrefslogtreecommitdiff
path: root/usr.bin/cvs/release.c
blob: 69f179835267f8a0936c6ad56ce371d6d17a6950 (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
/*	$OpenBSD: release.c,v 1.36 2007/09/25 10:56:04 chl Exp $	*/
/*-
 * Copyright (c) 2005-2007 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 <sys/stat.h>

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

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

void	cvs_release_local(struct cvs_file *);

static void	release_check_files(struct cvs_file *);

static int	dflag = 0;
static int	files_altered = 0;

struct cvs_cmd cvs_cmd_release = {
	CVS_OP_RELEASE, 0, "release",
	{ "re", "rel" },
	"Indicate that a Module is no longer in use",
	"[-d] dir...",
	"d",
	NULL,
	cvs_release
};

int
cvs_release(int argc, char **argv)
{
	int ch;
	int flags;
	struct cvs_recursion cr;

	flags = CR_REPO;

	while ((ch = getopt(argc, argv, cvs_cmd_release.cmd_opts)) != -1) {
		switch (ch) {
		case 'd':
			dflag = 1;
			break;
		default:
			fatal("%s", cvs_cmd_release.cmd_synopsis);
		}
	}

	argc -= optind;
	argv += optind;

	if (argc == 0)
		fatal("%s", cvs_cmd_release.cmd_synopsis);

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

	if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
		cvs_client_connect_to_server();
		cr.fileproc = cvs_client_sendfile;

		if (dflag == 1)
			cvs_client_send_request("Argument -d");
	} else
		cr.fileproc = cvs_release_local;

	cr.flags = flags;

	cvs_file_run(argc, argv, &cr);

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

	return (0);
}

void
cvs_release_local(struct cvs_file *cf)
{
	struct stat st;
	struct cvs_recursion cr;
	char *wdir, cwd[MAXPATHLEN];
	char *arg = ".";
	int saved_noexec;

	if (cf->file_type == CVS_FILE)
		return;

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

	cvs_file_classify(cf, cvs_directory_tag);

	if (cvs_server_active == 1) {
		cvs_history_add(CVS_HISTORY_RELEASE, cf, NULL);
		return;
	}

	if ((wdir = getcwd(cwd, sizeof(cwd))) == NULL)
		fatal("getcwd failed");

	if (cf->file_type == CVS_DIR) {
		if (!strcmp(cf->file_name, "."))
			return;

		/* chdir before updating the directory. */
		cvs_chdir(cf->file_path, 0);

		if (stat(CVS_PATH_CVSDIR, &st) == -1 || !S_ISDIR(st.st_mode)) {
			cvs_log(LP_ERR, "no repository directory: %s",
			    cf->file_path);
			return;
		}
	}

	saved_noexec = cvs_noexec;
	cvs_noexec = 1;

	cr.enterdir = NULL;
	cr.leavedir = NULL;
	cr.fileproc = cvs_update_local;
	cr.flags = CR_REPO | CR_RECURSE_DIRS;

	cvs_file_run(1, &arg, &cr);

	cvs_noexec = saved_noexec;

	cr.enterdir = NULL;
	cr.leavedir = NULL;
	cr.fileproc = release_check_files;
	cr.flags = CR_RECURSE_DIRS;

	cvs_file_run(1, &arg, &cr);

	(void)printf("You have [%d] altered files in this repository.\n",
	    files_altered);
	(void)printf("Are you sure you want to release %sdirectory `%s': ",
		(dflag == 1) ? "(and delete) " : "", cf->file_path);

	if (cvs_yesno() == -1) {
		(void)fprintf(stderr,
		    "** `%s' aborted by user choice.\n", cvs_command);

		/* change back to original working dir */
		cvs_chdir(wdir, 0);

		return;
	}

	/* change back to original working dir */
	cvs_chdir(wdir, 0);

	if (dflag == 1) {
		if (cvs_rmdir(cf->file_path) != 0)
			fatal("cvs_release_local: cvs_rmdir failed");
	}
}

static void
release_check_files(struct cvs_file *cf)
{
	cvs_log(LP_TRACE, "release_check_files(%s)", cf->file_path);

	cvs_file_classify(cf, cvs_directory_tag);

	if (cf->file_status == FILE_MERGE ||
	    cf->file_status == FILE_ADDED ||
	    cf->file_status == FILE_PATCH ||
	    cf->file_status == FILE_CONFLICT)
		files_altered++;
	return;
}