Blame examples/describe.c

Packit ae9e2a
/*
Packit ae9e2a
 * libgit2 "describe" example - shows how to describe commits
Packit ae9e2a
 *
Packit ae9e2a
 * Written by the libgit2 contributors
Packit ae9e2a
 *
Packit ae9e2a
 * To the extent possible under law, the author(s) have dedicated all copyright
Packit ae9e2a
 * and related and neighboring rights to this software to the public domain
Packit ae9e2a
 * worldwide. This software is distributed without any warranty.
Packit ae9e2a
 *
Packit ae9e2a
 * You should have received a copy of the CC0 Public Domain Dedication along
Packit ae9e2a
 * with this software. If not, see
Packit ae9e2a
 * <http://creativecommons.org/publicdomain/zero/1.0/>.
Packit ae9e2a
 */
Packit ae9e2a
Packit ae9e2a
#include "common.h"
Packit ae9e2a
#include <assert.h>
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * The following example partially reimplements the `git describe` command
Packit ae9e2a
 * and some of its options.
Packit ae9e2a
 *
Packit ae9e2a
 * These commands should work:
Packit ae9e2a
Packit ae9e2a
 * - Describe HEAD with default options (`describe`)
Packit ae9e2a
 * - Describe specified revision (`describe master~2`)
Packit ae9e2a
 * - Describe specified revisions (`describe master~2 HEAD~3`)
Packit ae9e2a
 * - Describe HEAD with dirty state suffix (`describe --dirty=*`)
Packit ae9e2a
 * - Describe consider all refs (`describe --all master`)
Packit ae9e2a
 * - Describe consider lightweight tags (`describe --tags temp-tag`)
Packit ae9e2a
 * - Describe show non-default abbreviated size (`describe --abbrev=10`)
Packit ae9e2a
 * - Describe always output the long format if matches a tag (`describe --long v1.0`)
Packit ae9e2a
 * - Describe consider only tags of specified pattern (`describe --match v*-release`)
Packit ae9e2a
 * - Describe show the fallback result (`describe --always`)
Packit ae9e2a
 * - Describe follow only the first parent commit (`describe --first-parent`)
Packit ae9e2a
 *
Packit ae9e2a
 * The command line parsing logic is simplified and doesn't handle
Packit ae9e2a
 * all of the use cases.
Packit ae9e2a
 */
Packit ae9e2a
Packit ae9e2a
/** describe_options represents the parsed command line options */
Packit ae9e2a
typedef struct {
Packit ae9e2a
	const char **commits;
Packit ae9e2a
	size_t commit_count;
Packit ae9e2a
	git_describe_options describe_options;
Packit ae9e2a
	git_describe_format_options format_options;
Packit ae9e2a
} describe_options;
Packit ae9e2a
Packit ae9e2a
typedef struct args_info args_info;
Packit ae9e2a
Packit ae9e2a
static void *xrealloc(void *oldp, size_t newsz)
Packit ae9e2a
{
Packit ae9e2a
	void *p = realloc(oldp, newsz);
Packit ae9e2a
	if (p == NULL) {
Packit ae9e2a
		fprintf(stderr, "Cannot allocate memory, exiting.\n");
Packit ae9e2a
		exit(1);
Packit ae9e2a
	}
Packit ae9e2a
	return p;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
static void opts_add_commit(describe_options *opts, const char *commit)
Packit ae9e2a
{
Packit ae9e2a
	size_t sz;
Packit ae9e2a
Packit ae9e2a
	assert(opts != NULL);
Packit ae9e2a
Packit ae9e2a
	sz = ++opts->commit_count * sizeof(opts->commits[0]);
Packit ae9e2a
	opts->commits = xrealloc(opts->commits, sz);
Packit ae9e2a
	opts->commits[opts->commit_count - 1] = commit;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
static void do_describe_single(git_repository *repo, describe_options *opts, const char *rev)
Packit ae9e2a
{
Packit ae9e2a
	git_object *commit;
Packit ae9e2a
	git_describe_result *describe_result;
Packit ae9e2a
	git_buf buf = { 0 };
Packit ae9e2a
	
Packit ae9e2a
	if (rev) {
Packit ae9e2a
		check_lg2(git_revparse_single(&commit, repo, rev),
Packit ae9e2a
			"Failed to lookup rev", rev);
Packit ae9e2a
Packit ae9e2a
		check_lg2(git_describe_commit(&describe_result, commit, &opts->describe_options),
Packit ae9e2a
			"Failed to describe rev", rev);
Packit ae9e2a
	}
Packit ae9e2a
	else
Packit ae9e2a
		check_lg2(git_describe_workdir(&describe_result, repo, &opts->describe_options),
Packit ae9e2a
			"Failed to describe workdir", NULL);
Packit ae9e2a
Packit ae9e2a
	check_lg2(git_describe_format(&buf, describe_result, &opts->format_options),
Packit ae9e2a
			"Failed to format describe rev", rev);
Packit ae9e2a
Packit ae9e2a
	printf("%s\n", buf.ptr);
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
static void do_describe(git_repository *repo, describe_options *opts)
Packit ae9e2a
{
Packit ae9e2a
	if (opts->commit_count == 0)
Packit ae9e2a
		do_describe_single(repo, opts, NULL);
Packit ae9e2a
	else
Packit ae9e2a
	{
Packit ae9e2a
		size_t i;
Packit ae9e2a
		for (i = 0; i < opts->commit_count; i++)
Packit ae9e2a
			do_describe_single(repo, opts, opts->commits[i]);
Packit ae9e2a
	}
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
static void print_usage(void)
Packit ae9e2a
{
Packit ae9e2a
	fprintf(stderr, "usage: see `git help describe`\n");
Packit ae9e2a
	exit(1);
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
/** Parse command line arguments */
Packit ae9e2a
static void parse_options(describe_options *opts, int argc, char **argv)
Packit ae9e2a
{
Packit ae9e2a
	args_info args = ARGS_INFO_INIT;
Packit ae9e2a
Packit ae9e2a
	for (args.pos = 1; args.pos < argc; ++args.pos) {
Packit ae9e2a
		const char *curr = argv[args.pos];
Packit ae9e2a
Packit ae9e2a
		if (curr[0] != '-') {
Packit ae9e2a
			opts_add_commit(opts, curr);
Packit ae9e2a
		} else if (!strcmp(curr, "--all")) {
Packit ae9e2a
			opts->describe_options.describe_strategy = GIT_DESCRIBE_ALL;
Packit ae9e2a
		} else if (!strcmp(curr, "--tags")) {
Packit ae9e2a
			opts->describe_options.describe_strategy = GIT_DESCRIBE_TAGS;
Packit ae9e2a
		} else if (!strcmp(curr, "--exact-match")) {
Packit ae9e2a
			opts->describe_options.max_candidates_tags = 0;
Packit ae9e2a
		} else if (!strcmp(curr, "--long")) {
Packit ae9e2a
			opts->format_options.always_use_long_format = 1;
Packit ae9e2a
		} else if (!strcmp(curr, "--always")) {
Packit ae9e2a
			opts->describe_options.show_commit_oid_as_fallback = 1;
Packit ae9e2a
		} else if (!strcmp(curr, "--first-parent")) {
Packit ae9e2a
			opts->describe_options.only_follow_first_parent = 1;
Packit ae9e2a
		} else if (optional_str_arg(&opts->format_options.dirty_suffix, &args, "--dirty", "-dirty")) {
Packit ae9e2a
		} else if (match_int_arg((int *)&opts->format_options.abbreviated_size, &args, "--abbrev", 0)) {
Packit ae9e2a
		} else if (match_int_arg((int *)&opts->describe_options.max_candidates_tags, &args, "--candidates", 0)) {
Packit ae9e2a
		} else if (match_str_arg(&opts->describe_options.pattern, &args, "--match")) {
Packit ae9e2a
		} else {
Packit ae9e2a
			print_usage();
Packit ae9e2a
		}
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	if (opts->commit_count > 0) {
Packit ae9e2a
		if (opts->format_options.dirty_suffix)
Packit ae9e2a
			fatal("--dirty is incompatible with commit-ishes", NULL);
Packit ae9e2a
	}
Packit ae9e2a
	else {
Packit ae9e2a
		if (!opts->format_options.dirty_suffix || !opts->format_options.dirty_suffix[0]) {
Packit ae9e2a
			opts_add_commit(opts, "HEAD");
Packit ae9e2a
		}
Packit ae9e2a
	}
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
/** Initialize describe_options struct */
Packit ae9e2a
static void describe_options_init(describe_options *opts)
Packit ae9e2a
{
Packit ae9e2a
	memset(opts, 0, sizeof(*opts));
Packit ae9e2a
Packit ae9e2a
	opts->commits = NULL;
Packit ae9e2a
	opts->commit_count = 0;
Packit ae9e2a
	git_describe_init_options(&opts->describe_options, GIT_DESCRIBE_OPTIONS_VERSION);
Packit ae9e2a
	git_describe_init_format_options(&opts->format_options, GIT_DESCRIBE_FORMAT_OPTIONS_VERSION);
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
int main(int argc, char **argv)
Packit ae9e2a
{
Packit ae9e2a
	git_repository *repo;
Packit ae9e2a
	describe_options opts;
Packit ae9e2a
Packit ae9e2a
	git_libgit2_init();
Packit ae9e2a
Packit ae9e2a
	check_lg2(git_repository_open_ext(&repo, ".", 0, NULL),
Packit ae9e2a
			"Could not open repository", NULL);
Packit ae9e2a
Packit ae9e2a
	describe_options_init(&opts);
Packit ae9e2a
	parse_options(&opts, argc, argv);
Packit ae9e2a
Packit ae9e2a
	do_describe(repo, &opts);
Packit ae9e2a
Packit ae9e2a
	git_repository_free(repo);
Packit ae9e2a
	git_libgit2_shutdown();
Packit ae9e2a
Packit ae9e2a
	return 0;
Packit ae9e2a
}