Blame examples/log.c

Packit ae9e2a
/*
Packit ae9e2a
 * libgit2 "log" example - shows how to walk history and get commit info
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
Packit ae9e2a
/**
Packit ae9e2a
 * This example demonstrates the libgit2 rev walker APIs to roughly
Packit ae9e2a
 * simulate the output of `git log` and a few of command line arguments.
Packit ae9e2a
 * `git log` has many many options and this only shows a few of them.
Packit ae9e2a
 *
Packit ae9e2a
 * This does not have:
Packit ae9e2a
 *
Packit ae9e2a
 * - Robust error handling
Packit ae9e2a
 * - Colorized or paginated output formatting
Packit ae9e2a
 * - Most of the `git log` options
Packit ae9e2a
 *
Packit ae9e2a
 * This does have:
Packit ae9e2a
 *
Packit ae9e2a
 * - Examples of translating command line arguments to equivalent libgit2
Packit ae9e2a
 *   revwalker configuration calls
Packit ae9e2a
 * - Simplified options to apply pathspec limits and to show basic diffs
Packit ae9e2a
 */
Packit ae9e2a
Packit ae9e2a
/** log_state represents walker being configured while handling options */
Packit ae9e2a
struct log_state {
Packit ae9e2a
	git_repository *repo;
Packit ae9e2a
	const char *repodir;
Packit ae9e2a
	git_revwalk *walker;
Packit ae9e2a
	int hide;
Packit ae9e2a
	int sorting;
Packit ae9e2a
	int revisions;
Packit ae9e2a
};
Packit ae9e2a
Packit ae9e2a
/** utility functions that are called to configure the walker */
Packit ae9e2a
static void set_sorting(struct log_state *s, unsigned int sort_mode);
Packit ae9e2a
static void push_rev(struct log_state *s, git_object *obj, int hide);
Packit ae9e2a
static int add_revision(struct log_state *s, const char *revstr);
Packit ae9e2a
Packit ae9e2a
/** log_options holds other command line options that affect log output */
Packit ae9e2a
struct log_options {
Packit ae9e2a
	int show_diff;
Packit ae9e2a
	int skip, limit;
Packit ae9e2a
	int min_parents, max_parents;
Packit ae9e2a
	git_time_t before;
Packit ae9e2a
	git_time_t after;
Packit ae9e2a
	const char *author;
Packit ae9e2a
	const char *committer;
Packit ae9e2a
	const char *grep;
Packit ae9e2a
};
Packit ae9e2a
Packit ae9e2a
/** utility functions that parse options and help with log output */
Packit ae9e2a
static int parse_options(
Packit ae9e2a
	struct log_state *s, struct log_options *opt, int argc, char **argv);
Packit ae9e2a
static void print_time(const git_time *intime, const char *prefix);
Packit ae9e2a
static void print_commit(git_commit *commit);
Packit ae9e2a
static int match_with_parent(git_commit *commit, int i, git_diff_options *);
Packit ae9e2a
Packit ae9e2a
/** utility functions for filtering */
Packit ae9e2a
static int signature_matches(const git_signature *sig, const char *filter);
Packit ae9e2a
static int log_message_matches(const git_commit *commit, const char *filter);
Packit ae9e2a
Packit ae9e2a
int main(int argc, char *argv[])
Packit ae9e2a
{
Packit ae9e2a
	int i, count = 0, printed = 0, parents, last_arg;
Packit ae9e2a
	struct log_state s;
Packit ae9e2a
	struct log_options opt;
Packit ae9e2a
	git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT;
Packit ae9e2a
	git_oid oid;
Packit ae9e2a
	git_commit *commit = NULL;
Packit ae9e2a
	git_pathspec *ps = NULL;
Packit ae9e2a
Packit ae9e2a
	git_libgit2_init();
Packit ae9e2a
Packit ae9e2a
	/** Parse arguments and set up revwalker. */
Packit ae9e2a
Packit ae9e2a
	last_arg = parse_options(&s, &opt, argc, argv);
Packit ae9e2a
Packit ae9e2a
	diffopts.pathspec.strings = &argv[last_arg];
Packit ae9e2a
	diffopts.pathspec.count	  = argc - last_arg;
Packit ae9e2a
	if (diffopts.pathspec.count > 0)
Packit ae9e2a
		check_lg2(git_pathspec_new(&ps, &diffopts.pathspec),
Packit ae9e2a
			"Building pathspec", NULL);
Packit ae9e2a
Packit ae9e2a
	if (!s.revisions)
Packit ae9e2a
		add_revision(&s, NULL);
Packit ae9e2a
Packit ae9e2a
	/** Use the revwalker to traverse the history. */
Packit ae9e2a
Packit ae9e2a
	printed = count = 0;
Packit ae9e2a
Packit ae9e2a
	for (; !git_revwalk_next(&oid, s.walker); git_commit_free(commit)) {
Packit ae9e2a
		check_lg2(git_commit_lookup(&commit, s.repo, &oid),
Packit ae9e2a
			"Failed to look up commit", NULL);
Packit ae9e2a
Packit ae9e2a
		parents = (int)git_commit_parentcount(commit);
Packit ae9e2a
		if (parents < opt.min_parents)
Packit ae9e2a
			continue;
Packit ae9e2a
		if (opt.max_parents > 0 && parents > opt.max_parents)
Packit ae9e2a
			continue;
Packit ae9e2a
Packit ae9e2a
		if (diffopts.pathspec.count > 0) {
Packit ae9e2a
			int unmatched = parents;
Packit ae9e2a
Packit ae9e2a
			if (parents == 0) {
Packit ae9e2a
				git_tree *tree;
Packit ae9e2a
				check_lg2(git_commit_tree(&tree, commit), "Get tree", NULL);
Packit ae9e2a
				if (git_pathspec_match_tree(
Packit ae9e2a
						NULL, tree, GIT_PATHSPEC_NO_MATCH_ERROR, ps) != 0)
Packit ae9e2a
					unmatched = 1;
Packit ae9e2a
				git_tree_free(tree);
Packit ae9e2a
			} else if (parents == 1) {
Packit ae9e2a
				unmatched = match_with_parent(commit, 0, &diffopts) ? 0 : 1;
Packit ae9e2a
			} else {
Packit ae9e2a
				for (i = 0; i < parents; ++i) {
Packit ae9e2a
					if (match_with_parent(commit, i, &diffopts))
Packit ae9e2a
						unmatched--;
Packit ae9e2a
				}
Packit ae9e2a
			}
Packit ae9e2a
Packit ae9e2a
			if (unmatched > 0)
Packit ae9e2a
				continue;
Packit ae9e2a
		}
Packit ae9e2a
Packit ae9e2a
		if (!signature_matches(git_commit_author(commit), opt.author))
Packit ae9e2a
			continue;
Packit ae9e2a
Packit ae9e2a
		if (!signature_matches(git_commit_committer(commit), opt.committer))
Packit ae9e2a
			continue;
Packit ae9e2a
Packit ae9e2a
		if (!log_message_matches(commit, opt.grep))
Packit ae9e2a
			continue;
Packit ae9e2a
Packit ae9e2a
		if (count++ < opt.skip)
Packit ae9e2a
			continue;
Packit ae9e2a
		if (opt.limit != -1 && printed++ >= opt.limit) {
Packit ae9e2a
			git_commit_free(commit);
Packit ae9e2a
			break;
Packit ae9e2a
		}
Packit ae9e2a
Packit ae9e2a
		print_commit(commit);
Packit ae9e2a
Packit ae9e2a
		if (opt.show_diff) {
Packit ae9e2a
			git_tree *a = NULL, *b = NULL;
Packit ae9e2a
			git_diff *diff = NULL;
Packit ae9e2a
Packit ae9e2a
			if (parents > 1)
Packit ae9e2a
				continue;
Packit ae9e2a
			check_lg2(git_commit_tree(&b, commit), "Get tree", NULL);
Packit ae9e2a
			if (parents == 1) {
Packit ae9e2a
				git_commit *parent;
Packit ae9e2a
				check_lg2(git_commit_parent(&parent, commit, 0), "Get parent", NULL);
Packit ae9e2a
				check_lg2(git_commit_tree(&a, parent), "Tree for parent", NULL);
Packit ae9e2a
				git_commit_free(parent);
Packit ae9e2a
			}
Packit ae9e2a
Packit ae9e2a
			check_lg2(git_diff_tree_to_tree(
Packit ae9e2a
				&diff, git_commit_owner(commit), a, b, &diffopts),
Packit ae9e2a
				"Diff commit with parent", NULL);
Packit ae9e2a
			check_lg2(
Packit ae9e2a
                git_diff_print(diff, GIT_DIFF_FORMAT_PATCH, diff_output, NULL),
Packit ae9e2a
				"Displaying diff", NULL);
Packit ae9e2a
Packit ae9e2a
			git_diff_free(diff);
Packit ae9e2a
			git_tree_free(a);
Packit ae9e2a
			git_tree_free(b);
Packit ae9e2a
		}
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	git_pathspec_free(ps);
Packit ae9e2a
	git_revwalk_free(s.walker);
Packit ae9e2a
	git_repository_free(s.repo);
Packit ae9e2a
	git_libgit2_shutdown();
Packit ae9e2a
Packit ae9e2a
	return 0;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
/** Determine if the given git_signature does not contain the filter text. */
Packit ae9e2a
static int signature_matches(const git_signature *sig, const char *filter) {
Packit ae9e2a
	if (filter == NULL)
Packit ae9e2a
		return 1;
Packit ae9e2a
Packit ae9e2a
	if (sig != NULL &&
Packit ae9e2a
		(strstr(sig->name, filter) != NULL ||
Packit ae9e2a
		strstr(sig->email, filter) != NULL))
Packit ae9e2a
		return 1;
Packit ae9e2a
Packit ae9e2a
	return 0;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
static int log_message_matches(const git_commit *commit, const char *filter) {
Packit ae9e2a
	const char *message = NULL;
Packit ae9e2a
Packit ae9e2a
	if (filter == NULL)
Packit ae9e2a
		return 1;
Packit ae9e2a
Packit ae9e2a
	if ((message = git_commit_message(commit)) != NULL &&
Packit ae9e2a
		strstr(message, filter) != NULL)
Packit ae9e2a
		return 1;
Packit ae9e2a
Packit ae9e2a
	return 0;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
/** Push object (for hide or show) onto revwalker. */
Packit ae9e2a
static void push_rev(struct log_state *s, git_object *obj, int hide)
Packit ae9e2a
{
Packit ae9e2a
	hide = s->hide ^ hide;
Packit ae9e2a
Packit ae9e2a
	/** Create revwalker on demand if it doesn't already exist. */
Packit ae9e2a
	if (!s->walker) {
Packit ae9e2a
		check_lg2(git_revwalk_new(&s->walker, s->repo),
Packit ae9e2a
			"Could not create revision walker", NULL);
Packit ae9e2a
		git_revwalk_sorting(s->walker, s->sorting);
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	if (!obj)
Packit ae9e2a
		check_lg2(git_revwalk_push_head(s->walker),
Packit ae9e2a
			"Could not find repository HEAD", NULL);
Packit ae9e2a
	else if (hide)
Packit ae9e2a
		check_lg2(git_revwalk_hide(s->walker, git_object_id(obj)),
Packit ae9e2a
			"Reference does not refer to a commit", NULL);
Packit ae9e2a
	else
Packit ae9e2a
		check_lg2(git_revwalk_push(s->walker, git_object_id(obj)),
Packit ae9e2a
			"Reference does not refer to a commit", NULL);
Packit ae9e2a
Packit ae9e2a
	git_object_free(obj);
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
/** Parse revision string and add revs to walker. */
Packit ae9e2a
static int add_revision(struct log_state *s, const char *revstr)
Packit ae9e2a
{
Packit ae9e2a
	git_revspec revs;
Packit ae9e2a
	int hide = 0;
Packit ae9e2a
Packit ae9e2a
	/** Open repo on demand if it isn't already open. */
Packit ae9e2a
	if (!s->repo) {
Packit ae9e2a
		if (!s->repodir) s->repodir = ".";
Packit ae9e2a
		check_lg2(git_repository_open_ext(&s->repo, s->repodir, 0, NULL),
Packit ae9e2a
			"Could not open repository", s->repodir);
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	if (!revstr) {
Packit ae9e2a
		push_rev(s, NULL, hide);
Packit ae9e2a
		return 0;
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	if (*revstr == '^') {
Packit ae9e2a
		revs.flags = GIT_REVPARSE_SINGLE;
Packit ae9e2a
		hide = !hide;
Packit ae9e2a
Packit ae9e2a
		if (git_revparse_single(&revs.from, s->repo, revstr + 1) < 0)
Packit ae9e2a
			return -1;
Packit ae9e2a
	} else if (git_revparse(&revs, s->repo, revstr) < 0)
Packit ae9e2a
		return -1;
Packit ae9e2a
Packit ae9e2a
	if ((revs.flags & GIT_REVPARSE_SINGLE) != 0)
Packit ae9e2a
		push_rev(s, revs.from, hide);
Packit ae9e2a
	else {
Packit ae9e2a
		push_rev(s, revs.to, hide);
Packit ae9e2a
Packit ae9e2a
		if ((revs.flags & GIT_REVPARSE_MERGE_BASE) != 0) {
Packit ae9e2a
			git_oid base;
Packit ae9e2a
			check_lg2(git_merge_base(&base, s->repo,
Packit ae9e2a
				git_object_id(revs.from), git_object_id(revs.to)),
Packit ae9e2a
				"Could not find merge base", revstr);
Packit ae9e2a
			check_lg2(
Packit ae9e2a
				git_object_lookup(&revs.to, s->repo, &base, GIT_OBJ_COMMIT),
Packit ae9e2a
				"Could not find merge base commit", NULL);
Packit ae9e2a
Packit ae9e2a
			push_rev(s, revs.to, hide);
Packit ae9e2a
		}
Packit ae9e2a
Packit ae9e2a
		push_rev(s, revs.from, !hide);
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	return 0;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
/** Update revwalker with sorting mode. */
Packit ae9e2a
static void set_sorting(struct log_state *s, unsigned int sort_mode)
Packit ae9e2a
{
Packit ae9e2a
	/** Open repo on demand if it isn't already open. */
Packit ae9e2a
	if (!s->repo) {
Packit ae9e2a
		if (!s->repodir) s->repodir = ".";
Packit ae9e2a
		check_lg2(git_repository_open_ext(&s->repo, s->repodir, 0, NULL),
Packit ae9e2a
			"Could not open repository", s->repodir);
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	/** Create revwalker on demand if it doesn't already exist. */
Packit ae9e2a
	if (!s->walker)
Packit ae9e2a
		check_lg2(git_revwalk_new(&s->walker, s->repo),
Packit ae9e2a
			"Could not create revision walker", NULL);
Packit ae9e2a
Packit ae9e2a
	if (sort_mode == GIT_SORT_REVERSE)
Packit ae9e2a
		s->sorting = s->sorting ^ GIT_SORT_REVERSE;
Packit ae9e2a
	else
Packit ae9e2a
		s->sorting = sort_mode | (s->sorting & GIT_SORT_REVERSE);
Packit ae9e2a
Packit ae9e2a
	git_revwalk_sorting(s->walker, s->sorting);
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
/** Helper to format a git_time value like Git. */
Packit ae9e2a
static void print_time(const git_time *intime, const char *prefix)
Packit ae9e2a
{
Packit ae9e2a
	char sign, out[32];
Packit ae9e2a
	struct tm *intm;
Packit ae9e2a
	int offset, hours, minutes;
Packit ae9e2a
	time_t t;
Packit ae9e2a
Packit ae9e2a
	offset = intime->offset;
Packit ae9e2a
	if (offset < 0) {
Packit ae9e2a
		sign = '-';
Packit ae9e2a
		offset = -offset;
Packit ae9e2a
	} else {
Packit ae9e2a
		sign = '+';
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	hours   = offset / 60;
Packit ae9e2a
	minutes = offset % 60;
Packit ae9e2a
Packit ae9e2a
	t = (time_t)intime->time + (intime->offset * 60);
Packit ae9e2a
Packit ae9e2a
	intm = gmtime(&t);
Packit ae9e2a
	strftime(out, sizeof(out), "%a %b %e %T %Y", intm);
Packit ae9e2a
Packit ae9e2a
	printf("%s%s %c%02d%02d\n", prefix, out, sign, hours, minutes);
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
/** Helper to print a commit object. */
Packit ae9e2a
static void print_commit(git_commit *commit)
Packit ae9e2a
{
Packit ae9e2a
	char buf[GIT_OID_HEXSZ + 1];
Packit ae9e2a
	int i, count;
Packit ae9e2a
	const git_signature *sig;
Packit ae9e2a
	const char *scan, *eol;
Packit ae9e2a
Packit ae9e2a
	git_oid_tostr(buf, sizeof(buf), git_commit_id(commit));
Packit ae9e2a
	printf("commit %s\n", buf);
Packit ae9e2a
Packit ae9e2a
	if ((count = (int)git_commit_parentcount(commit)) > 1) {
Packit ae9e2a
		printf("Merge:");
Packit ae9e2a
		for (i = 0; i < count; ++i) {
Packit ae9e2a
			git_oid_tostr(buf, 8, git_commit_parent_id(commit, i));
Packit ae9e2a
			printf(" %s", buf);
Packit ae9e2a
		}
Packit ae9e2a
		printf("\n");
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	if ((sig = git_commit_author(commit)) != NULL) {
Packit ae9e2a
		printf("Author: %s <%s>\n", sig->name, sig->email);
Packit ae9e2a
		print_time(&sig->when, "Date:   ");
Packit ae9e2a
	}
Packit ae9e2a
	printf("\n");
Packit ae9e2a
Packit ae9e2a
	for (scan = git_commit_message(commit); scan && *scan; ) {
Packit ae9e2a
		for (eol = scan; *eol && *eol != '\n'; ++eol) /* find eol */;
Packit ae9e2a
Packit ae9e2a
		printf("    %.*s\n", (int)(eol - scan), scan);
Packit ae9e2a
		scan = *eol ? eol + 1 : NULL;
Packit ae9e2a
	}
Packit ae9e2a
	printf("\n");
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
/** Helper to find how many files in a commit changed from its nth parent. */
Packit ae9e2a
static int match_with_parent(git_commit *commit, int i, git_diff_options *opts)
Packit ae9e2a
{
Packit ae9e2a
	git_commit *parent;
Packit ae9e2a
	git_tree *a, *b;
Packit ae9e2a
	git_diff *diff;
Packit ae9e2a
	int ndeltas;
Packit ae9e2a
Packit ae9e2a
	check_lg2(
Packit ae9e2a
		git_commit_parent(&parent, commit, (size_t)i), "Get parent", NULL);
Packit ae9e2a
	check_lg2(git_commit_tree(&a, parent), "Tree for parent", NULL);
Packit ae9e2a
	check_lg2(git_commit_tree(&b, commit), "Tree for commit", NULL);
Packit ae9e2a
	check_lg2(
Packit ae9e2a
		git_diff_tree_to_tree(&diff, git_commit_owner(commit), a, b, opts),
Packit ae9e2a
		"Checking diff between parent and commit", NULL);
Packit ae9e2a
Packit ae9e2a
	ndeltas = (int)git_diff_num_deltas(diff);
Packit ae9e2a
Packit ae9e2a
	git_diff_free(diff);
Packit ae9e2a
	git_tree_free(a);
Packit ae9e2a
	git_tree_free(b);
Packit ae9e2a
	git_commit_free(parent);
Packit ae9e2a
Packit ae9e2a
	return ndeltas > 0;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
/** Print a usage message for the program. */
Packit ae9e2a
static void usage(const char *message, const char *arg)
Packit ae9e2a
{
Packit ae9e2a
	if (message && arg)
Packit ae9e2a
		fprintf(stderr, "%s: %s\n", message, arg);
Packit ae9e2a
	else if (message)
Packit ae9e2a
		fprintf(stderr, "%s\n", message);
Packit ae9e2a
	fprintf(stderr, "usage: log [<options>]\n");
Packit ae9e2a
	exit(1);
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
/** Parse some log command line options. */
Packit ae9e2a
static int parse_options(
Packit ae9e2a
	struct log_state *s, struct log_options *opt, int argc, char **argv)
Packit ae9e2a
{
Packit ae9e2a
	struct args_info args = ARGS_INFO_INIT;
Packit ae9e2a
Packit ae9e2a
	memset(s, 0, sizeof(*s));
Packit ae9e2a
	s->sorting = GIT_SORT_TIME;
Packit ae9e2a
Packit ae9e2a
	memset(opt, 0, sizeof(*opt));
Packit ae9e2a
	opt->max_parents = -1;
Packit ae9e2a
	opt->limit = -1;
Packit ae9e2a
Packit ae9e2a
	for (args.pos = 1; args.pos < argc; ++args.pos) {
Packit ae9e2a
		const char *a = argv[args.pos];
Packit ae9e2a
Packit ae9e2a
		if (a[0] != '-') {
Packit ae9e2a
			if (!add_revision(s, a))
Packit ae9e2a
				s->revisions++;
Packit ae9e2a
			else
Packit ae9e2a
				/** Try failed revision parse as filename. */
Packit ae9e2a
				break;
Packit ae9e2a
		} else if (!strcmp(a, "--")) {
Packit ae9e2a
			++args.pos;
Packit ae9e2a
			break;
Packit ae9e2a
		}
Packit ae9e2a
		else if (!strcmp(a, "--date-order"))
Packit ae9e2a
			set_sorting(s, GIT_SORT_TIME);
Packit ae9e2a
		else if (!strcmp(a, "--topo-order"))
Packit ae9e2a
			set_sorting(s, GIT_SORT_TOPOLOGICAL);
Packit ae9e2a
		else if (!strcmp(a, "--reverse"))
Packit ae9e2a
			set_sorting(s, GIT_SORT_REVERSE);
Packit ae9e2a
		else if (match_str_arg(&opt->author, &args, "--author"))
Packit ae9e2a
			/** Found valid --author */;
Packit ae9e2a
		else if (match_str_arg(&opt->committer, &args, "--committer"))
Packit ae9e2a
			/** Found valid --committer */;
Packit ae9e2a
		else if (match_str_arg(&opt->grep, &args, "--grep"))
Packit ae9e2a
			/** Found valid --grep */;
Packit ae9e2a
		else if (match_str_arg(&s->repodir, &args, "--git-dir"))
Packit ae9e2a
			/** Found git-dir. */;
Packit ae9e2a
		else if (match_int_arg(&opt->skip, &args, "--skip", 0))
Packit ae9e2a
			/** Found valid --skip. */;
Packit ae9e2a
		else if (match_int_arg(&opt->limit, &args, "--max-count", 0))
Packit ae9e2a
			/** Found valid --max-count. */;
Packit ae9e2a
		else if (a[1] >= '0' && a[1] <= '9')
Packit ae9e2a
			is_integer(&opt->limit, a + 1, 0);
Packit ae9e2a
		else if (match_int_arg(&opt->limit, &args, "-n", 0))
Packit ae9e2a
			/** Found valid -n. */;
Packit ae9e2a
		else if (!strcmp(a, "--merges"))
Packit ae9e2a
			opt->min_parents = 2;
Packit ae9e2a
		else if (!strcmp(a, "--no-merges"))
Packit ae9e2a
			opt->max_parents = 1;
Packit ae9e2a
		else if (!strcmp(a, "--no-min-parents"))
Packit ae9e2a
			opt->min_parents = 0;
Packit ae9e2a
		else if (!strcmp(a, "--no-max-parents"))
Packit ae9e2a
			opt->max_parents = -1;
Packit ae9e2a
		else if (match_int_arg(&opt->max_parents, &args, "--max-parents=", 1))
Packit ae9e2a
			/** Found valid --max-parents. */;
Packit ae9e2a
		else if (match_int_arg(&opt->min_parents, &args, "--min-parents=", 0))
Packit ae9e2a
			/** Found valid --min_parents. */;
Packit ae9e2a
		else if (!strcmp(a, "-p") || !strcmp(a, "-u") || !strcmp(a, "--patch"))
Packit ae9e2a
			opt->show_diff = 1;
Packit ae9e2a
		else
Packit ae9e2a
			usage("Unsupported argument", a);
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	return args.pos;
Packit ae9e2a
}
Packit ae9e2a