Blame examples/blame.c

Packit Service 20376f
/*
Packit Service 20376f
 * libgit2 "blame" example - shows how to use the blame API
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
#ifdef _MSC_VER
Packit Service 20376f
#define snprintf sprintf_s
Packit Service 20376f
#define strcasecmp strcmpi
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * This example demonstrates how to invoke the libgit2 blame API to roughly
Packit Service 20376f
 * simulate the output of `git blame` and a few of its command line arguments.
Packit Service 20376f
 */
Packit Service 20376f
Packit Service 20376f
struct opts {
Packit Service 20376f
	char *path;
Packit Service 20376f
	char *commitspec;
Packit Service 20376f
	int C;
Packit Service 20376f
	int M;
Packit Service 20376f
	int start_line;
Packit Service 20376f
	int end_line;
Packit Service 20376f
	int F;
Packit Service 20376f
};
Packit Service 20376f
static void parse_opts(struct opts *o, int argc, char *argv[]);
Packit Service 20376f
Packit Service 20376f
int main(int argc, char *argv[])
Packit Service 20376f
{
Packit Service 20376f
	int line, break_on_null_hunk;
Packit Service 20376f
	size_t i, rawsize;
Packit Service 20376f
	char spec[1024] = {0};
Packit Service 20376f
	struct opts o = {0};
Packit Service 20376f
	const char *rawdata;
Packit Service 20376f
	git_repository *repo = NULL;
Packit Service 20376f
	git_revspec revspec = {0};
Packit Service 20376f
	git_blame_options blameopts = GIT_BLAME_OPTIONS_INIT;
Packit Service 20376f
	git_blame *blame = NULL;
Packit Service 20376f
	git_blob *blob;
Packit Service 20376f
	git_object *obj;
Packit Service 20376f
Packit Service 20376f
	git_libgit2_init();
Packit Service 20376f
Packit Service 20376f
	parse_opts(&o, argc, argv);
Packit Service 20376f
	if (o.M) blameopts.flags |= GIT_BLAME_TRACK_COPIES_SAME_COMMIT_MOVES;
Packit Service 20376f
	if (o.C) blameopts.flags |= GIT_BLAME_TRACK_COPIES_SAME_COMMIT_COPIES;
Packit Service 20376f
	if (o.F) blameopts.flags |= GIT_BLAME_FIRST_PARENT;
Packit Service 20376f
Packit Service 20376f
	/** Open the repository. */
Packit Service 20376f
	check_lg2(git_repository_open_ext(&repo, ".", 0, NULL), "Couldn't open repository", NULL);
Packit Service 20376f
Packit Service 20376f
	/**
Packit Service 20376f
	 * The commit range comes in "commitish" form. Use the rev-parse API to
Packit Service 20376f
	 * nail down the end points.
Packit Service 20376f
	 */
Packit Service 20376f
	if (o.commitspec) {
Packit Service 20376f
		check_lg2(git_revparse(&revspec, repo, o.commitspec), "Couldn't parse commit spec", NULL);
Packit Service 20376f
		if (revspec.flags & GIT_REVPARSE_SINGLE) {
Packit Service 20376f
			git_oid_cpy(&blameopts.newest_commit, git_object_id(revspec.from));
Packit Service 20376f
			git_object_free(revspec.from);
Packit Service 20376f
		} else {
Packit Service 20376f
			git_oid_cpy(&blameopts.oldest_commit, git_object_id(revspec.from));
Packit Service 20376f
			git_oid_cpy(&blameopts.newest_commit, git_object_id(revspec.to));
Packit Service 20376f
			git_object_free(revspec.from);
Packit Service 20376f
			git_object_free(revspec.to);
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/** Run the blame. */
Packit Service 20376f
	check_lg2(git_blame_file(&blame, repo, o.path, &blameopts), "Blame error", NULL);
Packit Service 20376f
Packit Service 20376f
	/**
Packit Service 20376f
	 * Get the raw data inside the blob for output. We use the
Packit Service 20376f
	 * `commitish:path/to/file.txt` format to find it.
Packit Service 20376f
	 */
Packit Service 20376f
	if (git_oid_iszero(&blameopts.newest_commit))
Packit Service 20376f
		strcpy(spec, "HEAD");
Packit Service 20376f
	else
Packit Service 20376f
		git_oid_tostr(spec, sizeof(spec), &blameopts.newest_commit);
Packit Service 20376f
	strcat(spec, ":");
Packit Service 20376f
	strcat(spec, o.path);
Packit Service 20376f
Packit Service 20376f
	check_lg2(git_revparse_single(&obj, repo, spec), "Object lookup error", NULL);
Packit Service 20376f
	check_lg2(git_blob_lookup(&blob, repo, git_object_id(obj)), "Blob lookup error", NULL);
Packit Service 20376f
	git_object_free(obj);
Packit Service 20376f
Packit Service 20376f
	rawdata = git_blob_rawcontent(blob);
Packit Service 20376f
	rawsize = git_blob_rawsize(blob);
Packit Service 20376f
Packit Service 20376f
	/** Produce the output. */
Packit Service 20376f
	line = 1;
Packit Service 20376f
	i = 0;
Packit Service 20376f
	break_on_null_hunk = 0;
Packit Service 20376f
	while (i < rawsize) {
Packit Service 20376f
		const char *eol = memchr(rawdata + i, '\n', rawsize - i);
Packit Service 20376f
		char oid[10] = {0};
Packit Service 20376f
		const git_blame_hunk *hunk = git_blame_get_hunk_byline(blame, line);
Packit Service 20376f
Packit Service 20376f
		if (break_on_null_hunk && !hunk)
Packit Service 20376f
			break;
Packit Service 20376f
Packit Service 20376f
		if (hunk) {
Packit Service 20376f
			char sig[128] = {0};
Packit Service 20376f
			break_on_null_hunk = 1;
Packit Service 20376f
			
Packit Service 20376f
			git_oid_tostr(oid, 10, &hunk->final_commit_id);
Packit Service 20376f
			snprintf(sig, 30, "%s <%s>", hunk->final_signature->name, hunk->final_signature->email);
Packit Service 20376f
Packit Service 20376f
			printf("%s ( %-30s %3d) %.*s\n",
Packit Service 20376f
					oid,
Packit Service 20376f
					sig,
Packit Service 20376f
					line,
Packit Service 20376f
					(int)(eol - rawdata - i),
Packit Service 20376f
					rawdata + i);
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		i = (int)(eol - rawdata + 1);
Packit Service 20376f
		line++;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/** Cleanup. */
Packit Service 20376f
	git_blob_free(blob);
Packit Service 20376f
	git_blame_free(blame);
Packit Service 20376f
	git_repository_free(repo);
Packit Service 20376f
Packit Service 20376f
	git_libgit2_shutdown();
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/** Tell the user how to make this thing work. */
Packit Service 20376f
static void usage(const char *msg, const char *arg)
Packit Service 20376f
{
Packit Service 20376f
	if (msg && arg)
Packit Service 20376f
		fprintf(stderr, "%s: %s\n", msg, arg);
Packit Service 20376f
	else if (msg)
Packit Service 20376f
		fprintf(stderr, "%s\n", msg);
Packit Service 20376f
	fprintf(stderr, "usage: blame [options] [<commit range>] <path>\n");
Packit Service 20376f
	fprintf(stderr, "\n");
Packit Service 20376f
	fprintf(stderr, "   <commit range>      example: `HEAD~10..HEAD`, or `1234abcd`\n");
Packit Service 20376f
	fprintf(stderr, "   -L <n,m>            process only line range n-m, counting from 1\n");
Packit Service 20376f
	fprintf(stderr, "   -M                  find line moves within and across files\n");
Packit Service 20376f
	fprintf(stderr, "   -C                  find line copies within and across files\n");
Packit Service 20376f
	fprintf(stderr, "   -F                  follow only the first parent commits\n");
Packit Service 20376f
	fprintf(stderr, "\n");
Packit Service 20376f
	exit(1);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/** Parse the arguments. */
Packit Service 20376f
static void parse_opts(struct opts *o, int argc, char *argv[])
Packit Service 20376f
{
Packit Service 20376f
	int i;
Packit Service 20376f
	char *bare_args[3] = {0};
Packit Service 20376f
Packit Service 20376f
	if (argc < 2) usage(NULL, NULL);
Packit Service 20376f
Packit Service 20376f
	for (i=1; i
Packit Service 20376f
		char *a = argv[i];
Packit Service 20376f
Packit Service 20376f
		if (a[0] != '-') {
Packit Service 20376f
			int i=0;
Packit Service 20376f
			while (bare_args[i] && i < 3) ++i;
Packit Service 20376f
			if (i >= 3)
Packit Service 20376f
				usage("Invalid argument set", NULL);
Packit Service 20376f
			bare_args[i] = a;
Packit Service 20376f
		}
Packit Service 20376f
		else if (!strcmp(a, "--"))
Packit Service 20376f
			continue;
Packit Service 20376f
		else if (!strcasecmp(a, "-M"))
Packit Service 20376f
			o->M = 1;
Packit Service 20376f
		else if (!strcasecmp(a, "-C"))
Packit Service 20376f
			o->C = 1;
Packit Service 20376f
		else if (!strcasecmp(a, "-F"))
Packit Service 20376f
			o->F = 1;
Packit Service 20376f
		else if (!strcasecmp(a, "-L")) {
Packit Service 20376f
			i++; a = argv[i];
Packit Service 20376f
			if (i >= argc) fatal("Not enough arguments to -L", NULL);
Packit Service 20376f
			check_lg2(sscanf(a, "%d,%d", &o->start_line, &o->end_line)-2, "-L format error", NULL);
Packit Service 20376f
		}
Packit Service 20376f
		else {
Packit Service 20376f
			/* commit range */
Packit Service 20376f
			if (o->commitspec) fatal("Only one commit spec allowed", NULL);
Packit Service 20376f
			o->commitspec = a;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* Handle the bare arguments */
Packit Service 20376f
	if (!bare_args[0]) usage("Please specify a path", NULL);
Packit Service 20376f
	o->path = bare_args[0];
Packit Service 20376f
	if (bare_args[1]) {
Packit Service 20376f
		/* <commitspec> <path> */
Packit Service 20376f
		o->path = bare_args[1];
Packit Service 20376f
		o->commitspec = bare_args[0];
Packit Service 20376f
	}
Packit Service 20376f
	if (bare_args[2]) {
Packit Service 20376f
		/* <oldcommit> <newcommit> <path> */
Packit Service 20376f
		char spec[128] = {0};
Packit Service 20376f
		o->path = bare_args[2];
Packit Service 20376f
		sprintf(spec, "%s..%s", bare_args[0], bare_args[1]);
Packit Service 20376f
		o->commitspec = spec;
Packit Service 20376f
	}
Packit Service 20376f
}