rev-parse.c 2.62 KB
Newer Older
1
/*
2
 * libgit2 "rev-parse" example - shows how to parse revspecs
3
 *
4 5 6 7 8 9 10 11 12
 * Written by the libgit2 contributors
 *
 * To the extent possible under law, the author(s) have dedicated all copyright
 * and related and neighboring rights to this software to the public domain
 * worldwide. This software is distributed without any warranty.
 *
 * You should have received a copy of the CC0 Public Domain Dedication along
 * with this software. If not, see
 * <http://creativecommons.org/publicdomain/zero/1.0/>.
13
 */
Russell Belfer committed
14

15 16
#include "common.h"

17
/** Forward declarations for helpers. */
18 19 20 21 22 23
struct parse_state {
	const char *repodir;
	const char *spec;
	int not;
};
static void parse_opts(struct parse_state *ps, int argc, char *argv[]);
24
static int parse_revision(git_repository *repo, struct parse_state *ps);
25

26
int lg2_rev_parse(git_repository *repo, int argc, char *argv[])
Russell Belfer committed
27
{
28 29 30 31
	struct parse_state ps = {0};

	parse_opts(&ps, argc, argv);

32
	check_lg2(parse_revision(repo, &ps), "Parsing", NULL);
33 34

	return 0;
Russell Belfer committed
35 36 37 38 39 40 41 42 43 44 45 46
}

static void usage(const char *message, const char *arg)
{
	if (message && arg)
		fprintf(stderr, "%s: %s\n", message, arg);
	else if (message)
		fprintf(stderr, "%s\n", message);
	fprintf(stderr, "usage: rev-parse [ --option ] <args>...\n");
	exit(1);
}

47 48 49
static void parse_opts(struct parse_state *ps, int argc, char *argv[])
{
	struct args_info args = ARGS_INFO_INIT;
Russell Belfer committed
50

51 52 53 54 55 56 57 58 59 60 61 62 63 64
	for (args.pos=1; args.pos < argc; ++args.pos) {
		const char *a = argv[args.pos];

		if (a[0] != '-') {
			if (ps->spec)
				usage("Too many specs", a);
			ps->spec = a;
		} else if (!strcmp(a, "--not"))
			ps->not = !ps->not;
		else if (!match_str_arg(&ps->repodir, &args, "--git-dir"))
			usage("Cannot handle argument", a);
	}
}

65
static int parse_revision(git_repository *repo, struct parse_state *ps)
Russell Belfer committed
66 67 68 69
{
	git_revspec rs;
	char str[GIT_OID_HEXSZ + 1];

70
	check_lg2(git_revparse(&rs, repo, ps->spec), "Could not parse", ps->spec);
Russell Belfer committed
71

72
	if ((rs.flags & GIT_REVSPEC_SINGLE) != 0) {
Russell Belfer committed
73 74 75 76
		git_oid_tostr(str, sizeof(str), git_object_id(rs.from));
		printf("%s\n", str);
		git_object_free(rs.from);
	}
77
	else if ((rs.flags & GIT_REVSPEC_RANGE) != 0) {
Russell Belfer committed
78 79 80 81
		git_oid_tostr(str, sizeof(str), git_object_id(rs.to));
		printf("%s\n", str);
		git_object_free(rs.to);

82
		if ((rs.flags & GIT_REVSPEC_MERGE_BASE) != 0) {
Russell Belfer committed
83
			git_oid base;
84
			check_lg2(git_merge_base(&base, repo,
85 86
						git_object_id(rs.from), git_object_id(rs.to)),
					"Could not find merge base", ps->spec);
Russell Belfer committed
87 88 89 90 91 92 93 94 95 96

			git_oid_tostr(str, sizeof(str), &base);
			printf("%s\n", str);
		}

		git_oid_tostr(str, sizeof(str), git_object_id(rs.from));
		printf("^%s\n", str);
		git_object_free(rs.from);
	}
	else {
97
		fatal("Invalid results from git_revparse", ps->spec);
Russell Belfer committed
98 99 100 101 102
	}

	return 0;
}