summaryrefslogtreecommitdiff
path: root/cvs2git.c
blob: ab0590872e113531aba897e587a08a8dbacf84d9 (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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
 * cvs2git
 *
 * Copyright (C) Linus Torvalds 2005
 */

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

static int verbose = 0;

/*
 * This is a really stupid program that takes cvsps output, and
 * generates a a long _shell_script_ that will create the GIT archive
 * from it. 
 *
 * You've been warned. I told you it was stupid.
 *
 * NOTE NOTE NOTE! In order to do branches correctly, this needs
 * the fixed cvsps that has the "Ancestor branch" tag output.
 * Hopefully David Mansfield will update his distribution soon
 * enough (he's the one who wrote the patch, so at least we don't
 * have to figt maintainer issues ;)
 *
 * Usage:
 *
 *	TZ=UTC cvsps -A |
 *		git-cvs2git --cvsroot=[root] --module=[module] > script
 *
 * Creates a shell script that will generate the .git archive of
 * the names CVS repository.
 *
 *	TZ=UTC cvsps -s 1234- -A |
 *		git-cvs2git -u --cvsroot=[root] --module=[module] > script
 *
 * Creates a shell script that will update the .git archive with
 * CVS changes from patchset 1234 until the last one.
 *
 * IMPORTANT NOTE ABOUT "cvsps"! This requires version 2.1 or better,
 * and the "TZ=UTC" and the "-A" flag is required for sane results!
 */
enum state {
	Header,
	Log,
	Members
};

static const char *cvsroot;
static const char *cvsmodule;

static char date[100];
static char author[100];
static char branch[100];
static char ancestor[100];
static char tag[100];
static char log[32768];
static int loglen = 0;
static int initial_commit = 1;

static void lookup_author(char *n, char **name, char **email)
{
	/*
	 * FIXME!!! I'm lazy and stupid.
	 *
	 * This could be something like
	 *
	 *	printf("lookup_author '%s'\n", n);
	 *	*name = "$author_name";
	 *	*email = "$author_email";
	 *
	 * and that would allow the script to do its own
	 * lookups at run-time.
	 */
	*name = n;
	*email = n;
}

static void prepare_commit(void)
{
	char *author_name, *author_email;
	char *src_branch;

	lookup_author(author, &author_name, &author_email);

	printf("export GIT_COMMITTER_NAME=%s\n", author_name);
	printf("export GIT_COMMITTER_EMAIL=%s\n", author_email);
	printf("export GIT_COMMITTER_DATE='+0000 %s'\n", date);

	printf("export GIT_AUTHOR_NAME=%s\n", author_name);
	printf("export GIT_AUTHOR_EMAIL=%s\n", author_email);
	printf("export GIT_AUTHOR_DATE='+0000 %s'\n", date);

	if (initial_commit)
		return;

	src_branch = *ancestor ? ancestor : branch;
	if (!strcmp(src_branch, "HEAD"))
		src_branch = "master";
	printf("ln -sf refs/heads/'%s' .git/HEAD\n", src_branch);

	/*
	 * Even if cvsps claims an ancestor, we'll let the new
	 * branch name take precedence if it already exists
	 */
	if (*ancestor) {
		src_branch = branch;
		if (!strcmp(src_branch, "HEAD"))
			src_branch = "master";
		printf("[ -e .git/refs/heads/'%s' ] && ln -sf refs/heads/'%s' .git/HEAD\n",
			src_branch, src_branch);
	}

	printf("git-read-tree -m HEAD || exit 1\n");
	printf("git-checkout-cache -f -u -a\n");
}

static void commit(void)
{
	const char *cmit_parent = initial_commit ? "" : "-p HEAD";
	const char *dst_branch;
	char *space;
	int i;

	printf("tree=$(git-write-tree)\n");
	printf("cat > .cmitmsg <<EOFMSG\n");

	/* Escape $ characters, and remove control characters */
	for (i = 0; i < loglen; i++) {
		unsigned char c = log[i];

		switch (c) {
		case '$':
		case '\\':
		case '`':
			putchar('\\');
			break;
		case 0 ... 31:
			if (c == '\n' || c == '\t')
				break;
		case 128 ... 159:
			continue;
		}
		putchar(c);
	}
	printf("\nEOFMSG\n");
	printf("commit=$(cat .cmitmsg | git-commit-tree $tree %s)\n", cmit_parent);

	dst_branch = branch;
	if (!strcmp(dst_branch, "HEAD"))
		dst_branch = "master";

	printf("echo $commit > .git/refs/heads/'%s'\n", dst_branch);

	space = strchr(tag, ' ');
	if (space)
		*space = 0;
	if (strcmp(tag, "(none)"))
		printf("echo $commit > .git/refs/tags/'%s'\n", tag);

	printf("echo 'Committed (to %s):' ; cat .cmitmsg; echo\n", dst_branch);

	*date = 0;
	*author = 0;
	*branch = 0;
	*ancestor = 0;
	*tag = 0;
	loglen = 0;

	initial_commit = 0;
}

static void update_file(char *line)
{
	char *name, *version;
	char *dir;

	while (isspace(*line))
		line++;
	name = line;
	line = strchr(line, ':');
	if (!line)
		return;
	*line++ = 0;
	line = strchr(line, '>');
	if (!line)
		return;
	*line++ = 0;
	version = line;
	line = strchr(line, '(');
	if (line) {	/* "(DEAD)" */
		printf("git-update-cache --force-remove '%s'\n", name);
		return;
	}

	dir = strrchr(name, '/');
	if (dir)
		printf("mkdir -p %.*s\n", (int)(dir - name), name);

	printf("cvs -q -d %s checkout -d .git-tmp -r%s '%s/%s'\n", 
		cvsroot, version, cvsmodule, name);
	printf("mv -f .git-tmp/%s %s\n", dir ? dir+1 : name, name);
	printf("rm -rf .git-tmp\n");
	printf("git-update-cache --add -- '%s'\n", name);
}

struct hdrentry {
	const char *name;
	char *dest;
} hdrs[] = {
	{ "Date:", date },
	{ "Author:", author },
	{ "Branch:", branch },
	{ "Ancestor branch:", ancestor },
	{ "Tag:", tag },
	{ "Log:", NULL },
	{ NULL, NULL }
};

int main(int argc, char **argv)
{
	static char line[1000];
	enum state state = Header;
	int i;

	for (i = 1; i < argc; i++) {
		const char *arg = argv[i];
		if (!memcmp(arg, "--cvsroot=", 10)) {
			cvsroot = arg + 10;
			continue;
		}
		if (!memcmp(arg, "--module=", 9)) {
			cvsmodule = arg+9;
			continue;
		} 
		if (!strcmp(arg, "-v")) {
			verbose = 1;
			continue;
		}
		if (!strcmp(arg, "-u")) {
			initial_commit = 0;
			continue;
		}
	}


	if (!cvsroot)
		cvsroot = getenv("CVSROOT");

	if (!cvsmodule || !cvsroot) {
		fprintf(stderr, "I need a CVSROOT and module name\n");
		exit(1);
	}

	if (initial_commit) {
		printf("[ -d .git ] && exit 1\n");
		    printf("git-init-db\n");
		printf("mkdir -p .git/refs/heads\n");
		printf("mkdir -p .git/refs/tags\n");
		printf("ln -sf refs/heads/master .git/HEAD\n");
	}

	while (fgets(line, sizeof(line), stdin) != NULL) {
		int linelen = strlen(line);

		while (linelen && isspace(line[linelen-1]))
			line[--linelen] = 0;

		switch (state) {
		struct hdrentry *entry;

		case Header:
			if (verbose)
				printf("# H: %s\n", line);
			for (entry = hdrs ; entry->name ; entry++) {
				int len = strlen(entry->name);
				char *val;

				if (memcmp(entry->name, line, len))
					continue;
				if (!entry->dest) {
					state = Log;
					break;
				}
				val = line + len;
				linelen -= len;
				while (isspace(*val)) {
					val++;
					linelen--;
				}
				memcpy(entry->dest, val, linelen+1);
				break;
			}
			continue;

		case Log:
			if (verbose)
				printf("# L: %s\n", line);
			if (!strcmp(line, "Members:")) {
				while (loglen && isspace(log[loglen-1]))
					log[--loglen] = 0;
				prepare_commit();
				state = Members;
				continue;
			}
				
			if (loglen + linelen + 5 > sizeof(log))
				continue;
			memcpy(log + loglen, line, linelen);
			loglen += linelen;
			log[loglen++] = '\n';
			continue;

		case Members:
			if (verbose)
				printf("# M: %s\n", line);
			if (!linelen) {
				commit();
				state = Header;
				continue;
			}
			update_file(line);
			continue;
		}
	}
	return 0;
}