summaryrefslogtreecommitdiff
path: root/examples/rev-parse.c
diff options
context:
space:
mode:
authorPatrick Steinhardt <ps@pks.im>2019-01-24 11:31:49 +0100
committerPatrick Steinhardt <ps@pks.im>2019-02-15 12:06:54 +0100
commitead10785dcd9e7599a52a0349a69c113c76e650d (patch)
tree14c30c430befd05eb160108fe49a14d72faeb3d7 /examples/rev-parse.c
parent7562422ab9310b81142986f69964194f491948cc (diff)
downloadlibgit2-ead10785dcd9e7599a52a0349a69c113c76e650d.tar.gz
examples: create common lg2 executable
Inside of our networking example code, we have a git2 executable that acts as an entry point to all the different network examples. As such, it is kind of the same like the normal git(1) executable in that it simply arbitrates to the respective subcommands. Let's extend this approach and merge all examples into a single standalone lg2 executable. Instead of building an executable for all the existing examples we have, we now bundle them all inside of the lg2 one and let them be callable via subcommands. In the process, we can get rid of duplicated library initialization, deinitialization and repository discovery code. Instead of having each subcommand handle these on its own, we simply do it inside of the single main function now.
Diffstat (limited to 'examples/rev-parse.c')
-rw-r--r--examples/rev-parse.c25
1 files changed, 6 insertions, 19 deletions
diff --git a/examples/rev-parse.c b/examples/rev-parse.c
index 483d6e019..7d6e9986f 100644
--- a/examples/rev-parse.c
+++ b/examples/rev-parse.c
@@ -16,26 +16,20 @@
/** Forward declarations for helpers. */
struct parse_state {
- git_repository *repo;
const char *repodir;
const char *spec;
int not;
};
static void parse_opts(struct parse_state *ps, int argc, char *argv[]);
-static int parse_revision(struct parse_state *ps);
+static int parse_revision(git_repository *repo, struct parse_state *ps);
-
-int main(int argc, char *argv[])
+int lg2_rev_parse(git_repository *repo, int argc, char *argv[])
{
struct parse_state ps = {0};
- git_libgit2_init();
parse_opts(&ps, argc, argv);
- check_lg2(parse_revision(&ps), "Parsing", NULL);
-
- git_repository_free(ps.repo);
- git_libgit2_shutdown();
+ check_lg2(parse_revision(repo, &ps), "Parsing", NULL);
return 0;
}
@@ -68,19 +62,12 @@ static void parse_opts(struct parse_state *ps, int argc, char *argv[])
}
}
-static int parse_revision(struct parse_state *ps)
+static int parse_revision(git_repository *repo, struct parse_state *ps)
{
git_revspec rs;
char str[GIT_OID_HEXSZ + 1];
- if (!ps->repo) {
- if (!ps->repodir)
- ps->repodir = ".";
- check_lg2(git_repository_open_ext(&ps->repo, ps->repodir, 0, NULL),
- "Could not open repository from", ps->repodir);
- }
-
- check_lg2(git_revparse(&rs, ps->repo, ps->spec), "Could not parse", ps->spec);
+ check_lg2(git_revparse(&rs, repo, ps->spec), "Could not parse", ps->spec);
if ((rs.flags & GIT_REVPARSE_SINGLE) != 0) {
git_oid_tostr(str, sizeof(str), git_object_id(rs.from));
@@ -94,7 +81,7 @@ static int parse_revision(struct parse_state *ps)
if ((rs.flags & GIT_REVPARSE_MERGE_BASE) != 0) {
git_oid base;
- check_lg2(git_merge_base(&base, ps->repo,
+ check_lg2(git_merge_base(&base, repo,
git_object_id(rs.from), git_object_id(rs.to)),
"Could not find merge base", ps->spec);