peek-remote.c 1.3 KB
Newer Older
1 2 3 4 5 6 7 8
#include "cache.h"
#include "refs.h"
#include "pkt-line.h"

static const char peek_remote_usage[] =
"git-peek-remote [--exec=upload-pack] [host:]directory";
static const char *exec = "git-upload-pack";

L
Linus Torvalds 已提交
9
static int peek_remote(int fd[2], unsigned flags)
10 11 12
{
	struct ref *ref;

L
Linus Torvalds 已提交
13
	get_remote_heads(fd[0], &ref, 0, NULL, flags);
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
	packet_flush(fd[1]);

	while (ref) {
		printf("%s	%s\n", sha1_to_hex(ref->old_sha1), ref->name);
		ref = ref->next;
	}
	return 0;
}

int main(int argc, char **argv)
{
	int i, ret;
	char *dest = NULL;
	int fd[2];
	pid_t pid;
29
	int nongit = 0;
L
Linus Torvalds 已提交
30
	unsigned flags = 0;
31 32

	setup_git_directory_gently(&nongit);
33 34 35 36 37

	for (i = 1; i < argc; i++) {
		char *arg = argv[i];

		if (*arg == '-') {
L
Linus Torvalds 已提交
38
			if (!strncmp("--exec=", arg, 7)) {
39
				exec = arg + 7;
L
Linus Torvalds 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
				continue;
			}
			if (!strcmp("--tags", arg)) {
				flags |= REF_TAGS;
				continue;
			}
			if (!strcmp("--heads", arg)) {
				flags |= REF_HEADS;
				continue;
			}
			if (!strcmp("--refs", arg)) {
				flags |= REF_NORMAL;
				continue;
			}
			usage(peek_remote_usage);
55 56 57 58
		}
		dest = arg;
		break;
	}
L
Linus Torvalds 已提交
59

60 61 62 63 64 65
	if (!dest || i != argc - 1)
		usage(peek_remote_usage);

	pid = git_connect(fd, dest, exec);
	if (pid < 0)
		return 1;
L
Linus Torvalds 已提交
66
	ret = peek_remote(fd, flags);
67 68
	close(fd[0]);
	close(fd[1]);
69 70
	ret |= finish_connect(pid);
	return !!ret;
71
}