Blame examples/tag.c

Packit Service 20376f
/*
Packit Service 20376f
 * libgit2 "tag" example - shows how to list, create and delete tags
Packit Service 20376f
 *
Packit Service 20376f
 * Written by the libgit2 contributors
Packit Service 20376f
 *
Packit Service 20376f
 * To the extent possible under law, the author(s) have dedicated all copyright
Packit Service 20376f
 * and related and neighboring rights to this software to the public domain
Packit Service 20376f
 * worldwide. This software is distributed without any warranty.
Packit Service 20376f
 *
Packit Service 20376f
 * You should have received a copy of the CC0 Public Domain Dedication along
Packit Service 20376f
 * with this software. If not, see
Packit Service 20376f
 * <http://creativecommons.org/publicdomain/zero/1.0/>.
Packit Service 20376f
 */
Packit Service 20376f
Packit Service 20376f
#include "common.h"
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * The following example partially reimplements the `git tag` command
Packit Service 20376f
 * and some of its options.
Packit Service 20376f
 *
Packit Service 20376f
 * These commands should work:
Packit Service 20376f
Packit Service 20376f
 * - Tag name listing (`tag`)
Packit Service 20376f
 * - Filtered tag listing with messages (`tag -n3 -l "v0.1*"`)
Packit Service 20376f
 * - Lightweight tag creation (`tag test v0.18.0`)
Packit Service 20376f
 * - Tag creation (`tag -a -m "Test message" test v0.18.0`)
Packit Service 20376f
 * - Tag deletion (`tag -d test`)
Packit Service 20376f
 *
Packit Service 20376f
 * The command line parsing logic is simplified and doesn't handle
Packit Service 20376f
 * all of the use cases.
Packit Service 20376f
 */
Packit Service 20376f
Packit Service 20376f
/** tag_options represents the parsed command line options */
Packit Service 20376f
typedef struct {
Packit Service 20376f
	const char *message;
Packit Service 20376f
	const char *pattern;
Packit Service 20376f
	const char *tag_name;
Packit Service 20376f
	const char *target;
Packit Service 20376f
	int num_lines;
Packit Service 20376f
	int force;
Packit Service 20376f
} tag_options;
Packit Service 20376f
Packit Service 20376f
/** tag_state represents the current program state for dragging around */
Packit Service 20376f
typedef struct {
Packit Service 20376f
	git_repository *repo;
Packit Service 20376f
	tag_options *opts;
Packit Service 20376f
} tag_state;
Packit Service 20376f
Packit Service 20376f
/** An action to execute based on the command line arguments */
Packit Service 20376f
typedef void (*tag_action)(tag_state *state);
Packit Service 20376f
typedef struct args_info args_info;
Packit Service 20376f
Packit Service 20376f
static void check(int result, const char *message)
Packit Service 20376f
{
Packit Service 20376f
	if (result) fatal(message, NULL);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/** Tag listing: Print individual message lines */
Packit Service 20376f
static void print_list_lines(const char *message, const tag_state *state)
Packit Service 20376f
{
Packit Service 20376f
	const char *msg = message;
Packit Service 20376f
	int num = state->opts->num_lines - 1;
Packit Service 20376f
Packit Service 20376f
	if (!msg) return;
Packit Service 20376f
Packit Service 20376f
	/** first line - headline */
Packit Service 20376f
	while(*msg && *msg != '\n') printf("%c", *msg++);
Packit Service 20376f
Packit Service 20376f
	/** skip over new lines */
Packit Service 20376f
	while(*msg && *msg == '\n') msg++;
Packit Service 20376f
Packit Service 20376f
	printf("\n");
Packit Service 20376f
Packit Service 20376f
	/** print just headline? */
Packit Service 20376f
	if (num == 0) return;
Packit Service 20376f
	if (*msg && msg[1]) printf("\n");
Packit Service 20376f
Packit Service 20376f
	/** print individual commit/tag lines */
Packit Service 20376f
	while (*msg && num-- >= 2) {
Packit Service 20376f
		printf("    ");
Packit Service 20376f
Packit Service 20376f
		while (*msg && *msg != '\n') printf("%c", *msg++);
Packit Service 20376f
Packit Service 20376f
		/** handle consecutive new lines */
Packit Service 20376f
		if (*msg && *msg == '\n' && msg[1] == '\n') {
Packit Service 20376f
			num--;
Packit Service 20376f
			printf("\n");
Packit Service 20376f
		}
Packit Service 20376f
		while(*msg && *msg == '\n') msg++;
Packit Service 20376f
Packit Service 20376f
		printf("\n");
Packit Service 20376f
	}
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/** Tag listing: Print an actual tag object */
Packit Service 20376f
static void print_tag(git_tag *tag, const tag_state *state)
Packit Service 20376f
{
Packit Service 20376f
	printf("%-16s", git_tag_name(tag));
Packit Service 20376f
Packit Service 20376f
	if (state->opts->num_lines) {
Packit Service 20376f
		const char *msg = git_tag_message(tag);
Packit Service 20376f
		print_list_lines(msg, state);
Packit Service 20376f
	} else {
Packit Service 20376f
		printf("\n");
Packit Service 20376f
	}
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/** Tag listing: Print a commit (target of a lightweight tag) */
Packit Service 20376f
static void print_commit(git_commit *commit, const char *name,
Packit Service 20376f
		const tag_state *state)
Packit Service 20376f
{
Packit Service 20376f
	printf("%-16s", name);
Packit Service 20376f
Packit Service 20376f
	if (state->opts->num_lines) {
Packit Service 20376f
		const char *msg = git_commit_message(commit);
Packit Service 20376f
		print_list_lines(msg, state);
Packit Service 20376f
	} else {
Packit Service 20376f
		printf("\n");
Packit Service 20376f
	}
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/** Tag listing: Fallback, should not happen */
Packit Service 20376f
static void print_name(const char *name)
Packit Service 20376f
{
Packit Service 20376f
	printf("%s\n", name);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/** Tag listing: Lookup tags based on ref name and dispatch to print */
Packit Service 20376f
static int each_tag(const char *name, tag_state *state)
Packit Service 20376f
{
Packit Service 20376f
	git_repository *repo = state->repo;
Packit Service 20376f
	git_object *obj;
Packit Service 20376f
Packit Service 20376f
	check_lg2(git_revparse_single(&obj, repo, name),
Packit Service 20376f
			"Failed to lookup rev", name);
Packit Service 20376f
Packit Service 20376f
	switch (git_object_type(obj)) {
Packit Service 20376f
		case GIT_OBJ_TAG:
Packit Service 20376f
			print_tag((git_tag *) obj, state);
Packit Service 20376f
			break;
Packit Service 20376f
		case GIT_OBJ_COMMIT:
Packit Service 20376f
			print_commit((git_commit *) obj, name, state);
Packit Service 20376f
			break;
Packit Service 20376f
		default:
Packit Service 20376f
			print_name(name);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	git_object_free(obj);
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void action_list_tags(tag_state *state)
Packit Service 20376f
{
Packit Service 20376f
	const char *pattern = state->opts->pattern;
Packit Service 20376f
	git_strarray tag_names = {0};
Packit Service 20376f
	size_t i;
Packit Service 20376f
Packit Service 20376f
	check_lg2(git_tag_list_match(&tag_names, pattern ? pattern : "*", state->repo),
Packit Service 20376f
			"Unable to get list of tags", NULL);
Packit Service 20376f
Packit Service 20376f
	for(i = 0; i < tag_names.count; i++) {
Packit Service 20376f
		each_tag(tag_names.strings[i], state);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	git_strarray_free(&tag_names);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void action_delete_tag(tag_state *state)
Packit Service 20376f
{
Packit Service 20376f
	tag_options *opts = state->opts;
Packit Service 20376f
	git_object *obj;
Packit Service 20376f
	git_buf abbrev_oid = {0};
Packit Service 20376f
Packit Service 20376f
	check(!opts->tag_name, "Name required");
Packit Service 20376f
Packit Service 20376f
	check_lg2(git_revparse_single(&obj, state->repo, opts->tag_name),
Packit Service 20376f
			"Failed to lookup rev", opts->tag_name);
Packit Service 20376f
Packit Service 20376f
	check_lg2(git_object_short_id(&abbrev_oid, obj),
Packit Service 20376f
			"Unable to get abbreviated OID", opts->tag_name);
Packit Service 20376f
Packit Service 20376f
	check_lg2(git_tag_delete(state->repo, opts->tag_name),
Packit Service 20376f
			"Unable to delete tag", opts->tag_name);
Packit Service 20376f
Packit Service 20376f
	printf("Deleted tag '%s' (was %s)\n", opts->tag_name, abbrev_oid.ptr);
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&abbrev_oid);
Packit Service 20376f
	git_object_free(obj);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void action_create_lighweight_tag(tag_state *state)
Packit Service 20376f
{
Packit Service 20376f
	git_repository *repo = state->repo;
Packit Service 20376f
	tag_options *opts = state->opts;
Packit Service 20376f
	git_oid oid;
Packit Service 20376f
	git_object *target;
Packit Service 20376f
Packit Service 20376f
	check(!opts->tag_name, "Name required");
Packit Service 20376f
Packit Service 20376f
	if (!opts->target) opts->target = "HEAD";
Packit Service 20376f
Packit Service 20376f
	check(!opts->target, "Target required");
Packit Service 20376f
Packit Service 20376f
	check_lg2(git_revparse_single(&target, repo, opts->target),
Packit Service 20376f
			"Unable to resolve spec", opts->target);
Packit Service 20376f
Packit Service 20376f
	check_lg2(git_tag_create_lightweight(&oid, repo, opts->tag_name,
Packit Service 20376f
				target, opts->force), "Unable to create tag", NULL);
Packit Service 20376f
Packit Service 20376f
	git_object_free(target);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void action_create_tag(tag_state *state)
Packit Service 20376f
{
Packit Service 20376f
	git_repository *repo = state->repo;
Packit Service 20376f
	tag_options *opts = state->opts;
Packit Service 20376f
	git_signature *tagger;
Packit Service 20376f
	git_oid oid;
Packit Service 20376f
	git_object *target;
Packit Service 20376f
Packit Service 20376f
	check(!opts->tag_name, "Name required");
Packit Service 20376f
	check(!opts->message, "Message required");
Packit Service 20376f
Packit Service 20376f
	if (!opts->target) opts->target = "HEAD";
Packit Service 20376f
Packit Service 20376f
	check_lg2(git_revparse_single(&target, repo, opts->target),
Packit Service 20376f
			"Unable to resolve spec", opts->target);
Packit Service 20376f
Packit Service 20376f
	check_lg2(git_signature_default(&tagger, repo),
Packit Service 20376f
			"Unable to create signature", NULL);
Packit Service 20376f
Packit Service 20376f
	check_lg2(git_tag_create(&oid, repo, opts->tag_name,
Packit Service 20376f
				target, tagger, opts->message, opts->force), "Unable to create tag", NULL);
Packit Service 20376f
Packit Service 20376f
	git_object_free(target);
Packit Service 20376f
	git_signature_free(tagger);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void print_usage(void)
Packit Service 20376f
{
Packit Service 20376f
	fprintf(stderr, "usage: see `git help tag`\n");
Packit Service 20376f
	exit(1);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/** Parse command line arguments and choose action to run when done */
Packit Service 20376f
static void parse_options(tag_action *action, tag_options *opts, int argc, char **argv)
Packit Service 20376f
{
Packit Service 20376f
	args_info args = ARGS_INFO_INIT;
Packit Service 20376f
	*action = &action_list_tags;
Packit Service 20376f
Packit Service 20376f
	for (args.pos = 1; args.pos < argc; ++args.pos) {
Packit Service 20376f
		const char *curr = argv[args.pos];
Packit Service 20376f
Packit Service 20376f
		if (curr[0] != '-') {
Packit Service 20376f
			if (!opts->tag_name)
Packit Service 20376f
				opts->tag_name = curr;
Packit Service 20376f
			else if (!opts->target)
Packit Service 20376f
				opts->target = curr;
Packit Service 20376f
			else
Packit Service 20376f
				print_usage();
Packit Service 20376f
Packit Service 20376f
			if (*action != &action_create_tag)
Packit Service 20376f
				*action = &action_create_lighweight_tag;
Packit Service 20376f
		} else if (!strcmp(curr, "-n")) {
Packit Service 20376f
			opts->num_lines = 1;
Packit Service 20376f
			*action = &action_list_tags;
Packit Service 20376f
		} else if (!strcmp(curr, "-a")) {
Packit Service 20376f
			*action = &action_create_tag;
Packit Service 20376f
		} else if (!strcmp(curr, "-f")) {
Packit Service 20376f
			opts->force = 1;
Packit Service 20376f
		} else if (match_int_arg(&opts->num_lines, &args, "-n", 0)) {
Packit Service 20376f
			*action = &action_list_tags;
Packit Service 20376f
		} else if (match_str_arg(&opts->pattern, &args, "-l")) {
Packit Service 20376f
			*action = &action_list_tags;
Packit Service 20376f
		} else if (match_str_arg(&opts->tag_name, &args, "-d")) {
Packit Service 20376f
			*action = &action_delete_tag;
Packit Service 20376f
		} else if (match_str_arg(&opts->message, &args, "-m")) {
Packit Service 20376f
			*action = &action_create_tag;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/** Initialize tag_options struct */
Packit Service 20376f
static void tag_options_init(tag_options *opts)
Packit Service 20376f
{
Packit Service 20376f
	memset(opts, 0, sizeof(*opts));
Packit Service 20376f
Packit Service 20376f
	opts->message   = NULL;
Packit Service 20376f
	opts->pattern   = NULL;
Packit Service 20376f
	opts->tag_name  = NULL;
Packit Service 20376f
	opts->target    = NULL;
Packit Service 20376f
	opts->num_lines = 0;
Packit Service 20376f
	opts->force     = 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int main(int argc, char **argv)
Packit Service 20376f
{
Packit Service 20376f
	git_repository *repo;
Packit Service 20376f
	tag_options opts;
Packit Service 20376f
	tag_action action;
Packit Service 20376f
	tag_state state;
Packit Service 20376f
Packit Service 20376f
	git_libgit2_init();
Packit Service 20376f
Packit Service 20376f
	check_lg2(git_repository_open_ext(&repo, ".", 0, NULL),
Packit Service 20376f
			"Could not open repository", NULL);
Packit Service 20376f
Packit Service 20376f
	tag_options_init(&opts);
Packit Service 20376f
	parse_options(&action, &opts, argc, argv);
Packit Service 20376f
Packit Service 20376f
	state.repo = repo;
Packit Service 20376f
	state.opts = &opt;;
Packit Service 20376f
	action(&state);
Packit Service 20376f
Packit Service 20376f
	git_repository_free(repo);
Packit Service 20376f
	git_libgit2_shutdown();
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}