Commit c7c833947ec7ac446761242c02d32916661daeb7

Juan Rubén 2014-02-21T00:22:07

Add option to limit blame to first parent

diff --git a/examples/blame.c b/examples/blame.c
index 1f5db69..f04c417 100644
--- a/examples/blame.c
+++ b/examples/blame.c
@@ -31,6 +31,7 @@ struct opts {
 	int M;
 	int start_line;
 	int end_line;
+	int F;
 };
 static void parse_opts(struct opts *o, int argc, char *argv[]);
 
@@ -52,6 +53,7 @@ int main(int argc, char *argv[])
 	parse_opts(&o, argc, argv);
 	if (o.M) blameopts.flags |= GIT_BLAME_TRACK_COPIES_SAME_COMMIT_MOVES;
 	if (o.C) blameopts.flags |= GIT_BLAME_TRACK_COPIES_SAME_COMMIT_COPIES;
+	if (o.F) blameopts.flags |= GIT_BLAME_FIRST_PARENT;
 
 	/** Open the repository. */
 	check_lg2(git_repository_open_ext(&repo, ".", 0, NULL), "Couldn't open repository", NULL);
@@ -146,6 +148,7 @@ static void usage(const char *msg, const char *arg)
 	fprintf(stderr, "   -L <n,m>            process only line range n-m, counting from 1\n");
 	fprintf(stderr, "   -M                  find line moves within and across files\n");
 	fprintf(stderr, "   -C                  find line copies within and across files\n");
+	fprintf(stderr, "   -F                  only care about the first parent\n");
 	fprintf(stderr, "\n");
 	exit(1);
 }
@@ -174,6 +177,8 @@ static void parse_opts(struct opts *o, int argc, char *argv[])
 			o->M = 1;
 		else if (!strcasecmp(a, "-C"))
 			o->C = 1;
+		else if (!strcasecmp(a, "-F"))
+			o->F = 1;
 		else if (!strcasecmp(a, "-L")) {
 			i++; a = argv[i];
 			if (i >= argc) fatal("Not enough arguments to -L", NULL);
diff --git a/include/git2/blame.h b/include/git2/blame.h
index b98c6f0..873a94d 100644
--- a/include/git2/blame.h
+++ b/include/git2/blame.h
@@ -40,6 +40,8 @@ typedef enum {
 	 * commit (like `git blame -CCC`). Implies SAME_COMMIT_COPIES.
 	 * NOT IMPLEMENTED. */
 	GIT_BLAME_TRACK_COPIES_ANY_COMMIT_COPIES = (1<<3),
+
+	GIT_BLAME_FIRST_PARENT = (1<<4),
 } git_blame_flag_t;
 
 /**
diff --git a/src/blame_git.c b/src/blame_git.c
index 800f1f0..c275293 100644
--- a/src/blame_git.c
+++ b/src/blame_git.c
@@ -485,12 +485,14 @@ static void pass_blame(git_blame *blame, git_blame__origin *origin, uint32_t opt
 	git_blame__origin *sg_buf[16];
 	git_blame__origin *porigin, **sg_origin = sg_buf;
 
-	GIT_UNUSED(opt);
-
 	num_parents = git_commit_parentcount(commit);
 	if (!git_oid_cmp(git_commit_id(commit), &blame->options.oldest_commit))
 		/* Stop at oldest specified commit */
 		num_parents = 0;
+	else if (opt & GIT_BLAME_FIRST_PARENT)
+		/* Limit search to the first parent */
+		num_parents = 1;
+
 	if (!num_parents) {
 		git_oid_cpy(&blame->options.oldest_commit, git_commit_id(commit));
 		goto finish;