summaryrefslogtreecommitdiff
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
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.
-rw-r--r--examples/CMakeLists.txt20
-rw-r--r--examples/add.c9
-rw-r--r--examples/blame.c11
-rw-r--r--examples/cat-file.c10
-rw-r--r--examples/checkout.c15
-rw-r--r--examples/clone.c (renamed from examples/network/clone.c)4
-rw-r--r--examples/common.h29
-rw-r--r--examples/describe.c11
-rw-r--r--examples/diff.c12
-rw-r--r--examples/fetch.c (renamed from examples/network/fetch.c)4
-rw-r--r--examples/for-each-ref.c10
-rw-r--r--examples/general.c3
-rw-r--r--examples/index-pack.c (renamed from examples/network/index-pack.c)4
-rw-r--r--examples/init.c7
-rw-r--r--examples/lg2.c109
-rw-r--r--examples/log.c15
-rw-r--r--examples/ls-files.c13
-rw-r--r--examples/ls-remote.c (renamed from examples/network/ls-remote.c)4
-rw-r--r--examples/merge.c10
-rw-r--r--examples/network/.gitignore1
-rw-r--r--examples/network/git2.c87
-rw-r--r--examples/remote.c15
-rw-r--r--examples/rev-list.c7
-rw-r--r--examples/rev-parse.c25
-rw-r--r--examples/show-index.c (renamed from examples/showindex.c)6
-rw-r--r--examples/status.c15
-rw-r--r--examples/tag.c11
27 files changed, 173 insertions, 294 deletions
diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt
index c02af0684..05e344df1 100644
--- a/examples/CMakeLists.txt
+++ b/examples/CMakeLists.txt
@@ -1,25 +1,15 @@
INCLUDE_DIRECTORIES(${LIBGIT2_INCLUDES})
INCLUDE_DIRECTORIES(SYSTEM ${LIBGIT2_SYSTEM_INCLUDES})
-FILE(GLOB_RECURSE SRC_EXAMPLE_GIT2 network/*.c common.?)
-ADD_EXECUTABLE(cgit2 ${SRC_EXAMPLE_GIT2})
-SET_TARGET_PROPERTIES(cgit2 PROPERTIES C_STANDARD 90)
+FILE(GLOB LG2_SOURCES *.c)
+ADD_EXECUTABLE(lg2 ${LG2_SOURCES})
+SET_TARGET_PROPERTIES(lg2 PROPERTIES C_STANDARD 90)
# Ensure that we do not use deprecated functions internally
ADD_DEFINITIONS(-DGIT_DEPRECATE_HARD)
IF(WIN32 OR ANDROID)
- TARGET_LINK_LIBRARIES(cgit2 git2)
+ TARGET_LINK_LIBRARIES(lg2 git2)
ELSE()
- TARGET_LINK_LIBRARIES(cgit2 git2 pthread)
+ TARGET_LINK_LIBRARIES(lg2 git2 pthread)
ENDIF()
-
-FILE(GLOB SRC_EXAMPLE_APPS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.c)
-FOREACH(src_app ${SRC_EXAMPLE_APPS})
- STRING(REPLACE ".c" "" app_name ${src_app})
- IF(NOT ${app_name} STREQUAL "common")
- ADD_EXECUTABLE(${app_name} ${src_app} "common.c")
- TARGET_LINK_LIBRARIES(${app_name} git2)
- SET_TARGET_PROPERTIES(${app_name} PROPERTIES C_STANDARD 90)
- ENDIF()
-ENDFOREACH()
diff --git a/examples/add.c b/examples/add.c
index e5849892e..276ba35f9 100644
--- a/examples/add.c
+++ b/examples/add.c
@@ -31,22 +31,18 @@ static void parse_opts(int *options, int *count, int argc, char *argv[]);
void init_array(git_strarray *array, int argc, char **argv);
int print_matched_cb(const char *path, const char *matched_pathspec, void *payload);
-int main (int argc, char** argv)
+int lg2_add(git_repository *repo, int argc, char** argv)
{
git_index_matched_path_cb matched_cb = NULL;
- git_repository *repo = NULL;
git_index *index;
git_strarray array = {0};
int options = 0, count = 0;
struct print_payload payload = {0};
- git_libgit2_init();
-
parse_opts(&options, &count, argc, argv);
init_array(&array, argc-count, argv+count);
- check_lg2(git_repository_open(&repo, "."), "No git repository", NULL);
check_lg2(git_repository_index(&index, repo), "Could not open repository index", NULL);
if (options&VERBOSE || options&SKIP) {
@@ -64,9 +60,6 @@ int main (int argc, char** argv)
git_index_write(index);
git_index_free(index);
- git_repository_free(repo);
-
- git_libgit2_shutdown();
return 0;
}
diff --git a/examples/blame.c b/examples/blame.c
index 9288352e2..0ddbfc68a 100644
--- a/examples/blame.c
+++ b/examples/blame.c
@@ -35,30 +35,24 @@ struct opts {
};
static void parse_opts(struct opts *o, int argc, char *argv[]);
-int main(int argc, char *argv[])
+int lg2_blame(git_repository *repo, int argc, char *argv[])
{
int line, break_on_null_hunk;
size_t i, rawsize;
char spec[1024] = {0};
struct opts o = {0};
const char *rawdata;
- git_repository *repo = NULL;
git_revspec revspec = {0};
git_blame_options blameopts = GIT_BLAME_OPTIONS_INIT;
git_blame *blame = NULL;
git_blob *blob;
git_object *obj;
- git_libgit2_init();
-
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);
-
/**
* The commit range comes in "commitish" form. Use the rev-parse API to
* nail down the end points.
@@ -131,9 +125,6 @@ int main(int argc, char *argv[])
/** Cleanup. */
git_blob_free(blob);
git_blame_free(blame);
- git_repository_free(repo);
-
- git_libgit2_shutdown();
return 0;
}
diff --git a/examples/cat-file.c b/examples/cat-file.c
index 0fb7d4535..9b14a7048 100644
--- a/examples/cat-file.c
+++ b/examples/cat-file.c
@@ -120,19 +120,14 @@ static void parse_opts(struct opts *o, int argc, char *argv[]);
/** Entry point for this command */
-int main(int argc, char *argv[])
+int lg2_cat_file(git_repository *repo, int argc, char *argv[])
{
- git_repository *repo;
struct opts o = { ".", NULL, 0, 0 };
git_object *obj = NULL;
char oidstr[GIT_OID_HEXSZ + 1];
- git_libgit2_init();
-
parse_opts(&o, argc, argv);
- check_lg2(git_repository_open_ext(&repo, o.dir, 0, NULL),
- "Could not open repository", NULL);
check_lg2(git_revparse_single(&obj, repo, o.rev),
"Could not resolve", o.rev);
@@ -188,9 +183,6 @@ int main(int argc, char *argv[])
}
git_object_free(obj);
- git_repository_free(repo);
-
- git_libgit2_shutdown();
return 0;
}
diff --git a/examples/checkout.c b/examples/checkout.c
index 9119d6503..577a6f124 100644
--- a/examples/checkout.c
+++ b/examples/checkout.c
@@ -172,9 +172,8 @@ cleanup:
}
/** That example's entry point */
-int main(int argc, char **argv)
+int lg2_checkout(git_repository *repo, int argc, char **argv)
{
- git_repository *repo = NULL;
struct args_info args = ARGS_INFO_INIT;
checkout_options opts;
git_repository_state_t state;
@@ -185,15 +184,6 @@ int main(int argc, char **argv)
/** Parse our command line options */
parse_options(&path, &opts, &args);
- /** Initialize the library */
- err = git_libgit2_init();
- if (!err)
- check_lg2(err, "Failed to initialize libgit2", NULL);
-
- /** Open the repository corresponding to the options */
- check_lg2(git_repository_open_ext(&repo, path, 0, NULL),
- "Could not open repository", NULL);
-
/** Make sure we're not about to checkout while something else is going on */
state = git_repository_state(repo);
if (state != GIT_REPOSITORY_STATE_NONE) {
@@ -228,8 +218,5 @@ int main(int argc, char **argv)
cleanup:
git_annotated_commit_free(checkout_target);
- git_repository_free(repo);
- git_libgit2_shutdown();
-
return err;
}
diff --git a/examples/network/clone.c b/examples/clone.c
index 970e84ff9..fc121bc1e 100644
--- a/examples/network/clone.c
+++ b/examples/clone.c
@@ -1,4 +1,4 @@
-#include "../common.h"
+#include "common.h"
typedef struct progress_data {
git_transfer_progress fetch_progress;
@@ -63,7 +63,7 @@ static void checkout_progress(const char *path, size_t cur, size_t tot, void *pa
}
-int do_clone(git_repository *repo, int argc, char **argv)
+int lg2_clone(git_repository *repo, int argc, char **argv)
{
progress_data pd = {{0}};
git_repository *cloned_repo = NULL;
diff --git a/examples/common.h b/examples/common.h
index 63f25c7c5..ac1c067dd 100644
--- a/examples/common.h
+++ b/examples/common.h
@@ -26,8 +26,32 @@
#endif
#endif
+#define ARRAY_SIZE(x) (sizeof(x)/sizeof(*x))
#define UNUSED(x) (void)(x)
+extern int lg2_add(git_repository *repo, int argc, char **argv);
+extern int lg2_blame(git_repository *repo, int argc, char **argv);
+extern int lg2_cat_file(git_repository *repo, int argc, char **argv);
+extern int lg2_checkout(git_repository *repo, int argc, char **argv);
+extern int lg2_clone(git_repository *repo, int argc, char **argv);
+extern int lg2_describe(git_repository *repo, int argc, char **argv);
+extern int lg2_diff(git_repository *repo, int argc, char **argv);
+extern int lg2_fetch(git_repository *repo, int argc, char **argv);
+extern int lg2_for_each_ref(git_repository *repo, int argc, char **argv);
+extern int lg2_general(git_repository *repo, int argc, char **argv);
+extern int lg2_index_pack(git_repository *repo, int argc, char **argv);
+extern int lg2_init(git_repository *repo, int argc, char **argv);
+extern int lg2_log(git_repository *repo, int argc, char **argv);
+extern int lg2_ls_files(git_repository *repo, int argc, char **argv);
+extern int lg2_ls_remote(git_repository *repo, int argc, char **argv);
+extern int lg2_merge(git_repository *repo, int argc, char **argv);
+extern int lg2_remote(git_repository *repo, int argc, char **argv);
+extern int lg2_rev_list(git_repository *repo, int argc, char **argv);
+extern int lg2_rev_parse(git_repository *repo, int argc, char **argv);
+extern int lg2_show_index(git_repository *repo, int argc, char **argv);
+extern int lg2_status(git_repository *repo, int argc, char **argv);
+extern int lg2_tag(git_repository *repo, int argc, char **argv);
+
/**
* Check libgit2 error code, printing error to stderr on failure and
* exiting the program.
@@ -142,8 +166,3 @@ extern int cred_acquire_cb(git_cred **out,
const char *username_from_url,
unsigned int allowed_types,
void *payload);
-
-extern int ls_remote(git_repository *repo, int argc, char **argv);
-extern int fetch(git_repository *repo, int argc, char **argv);
-extern int index_pack(git_repository *repo, int argc, char **argv);
-extern int do_clone(git_repository *repo, int argc, char **argv);
diff --git a/examples/describe.c b/examples/describe.c
index 2005de4ae..966da15c1 100644
--- a/examples/describe.c
+++ b/examples/describe.c
@@ -152,23 +152,14 @@ static void describe_options_init(describe_options *opts)
git_describe_init_format_options(&opts->format_options, GIT_DESCRIBE_FORMAT_OPTIONS_VERSION);
}
-int main(int argc, char **argv)
+int lg2_describe(git_repository *repo, int argc, char **argv)
{
- git_repository *repo;
describe_options opts;
- git_libgit2_init();
-
- check_lg2(git_repository_open_ext(&repo, ".", 0, NULL),
- "Could not open repository", NULL);
-
describe_options_init(&opts);
parse_options(&opts, argc, argv);
do_describe(repo, &opts);
- git_repository_free(repo);
- git_libgit2_shutdown();
-
return 0;
}
diff --git a/examples/diff.c b/examples/diff.c
index 1de0483a3..e8ba918f6 100644
--- a/examples/diff.c
+++ b/examples/diff.c
@@ -67,9 +67,8 @@ static int color_printer(
const git_diff_delta*, const git_diff_hunk*, const git_diff_line*, void*);
static void diff_print_stats(git_diff *diff, struct opts *o);
-int main(int argc, char *argv[])
+int lg2_diff(git_repository *repo, int argc, char *argv[])
{
- git_repository *repo = NULL;
git_tree *t1 = NULL, *t2 = NULL;
git_diff *diff;
struct opts o = {
@@ -77,13 +76,8 @@ int main(int argc, char *argv[])
-1, 0, 0, GIT_DIFF_FORMAT_PATCH, NULL, NULL, "."
};
- git_libgit2_init();
-
parse_opts(&o, argc, argv);
- check_lg2(git_repository_open_ext(&repo, o.dir, 0, NULL),
- "Could not open repository", o.dir);
-
/**
* Possible argument patterns:
*
@@ -157,13 +151,9 @@ int main(int argc, char *argv[])
}
/** Cleanup before exiting. */
-
git_diff_free(diff);
git_tree_free(t1);
git_tree_free(t2);
- git_repository_free(repo);
-
- git_libgit2_shutdown();
return 0;
}
diff --git a/examples/network/fetch.c b/examples/fetch.c
index bb5f1b189..1df373e38 100644
--- a/examples/network/fetch.c
+++ b/examples/fetch.c
@@ -1,4 +1,4 @@
-#include "../common.h"
+#include "common.h"
static int progress_cb(const char *str, int len, void *data)
{
@@ -54,7 +54,7 @@ static int transfer_progress_cb(const git_transfer_progress *stats, void *payloa
}
/** Entry point for this command */
-int fetch(git_repository *repo, int argc, char **argv)
+int lg2_fetch(git_repository *repo, int argc, char **argv)
{
git_remote *remote = NULL;
const git_transfer_progress *stats;
diff --git a/examples/for-each-ref.c b/examples/for-each-ref.c
index 3bc25fcf2..eea73d2fc 100644
--- a/examples/for-each-ref.c
+++ b/examples/for-each-ref.c
@@ -31,19 +31,15 @@ static int show_ref(git_reference *ref, void *data)
return 0;
}
-int main(int argc, char **argv)
+int lg2_for_each_ref(git_repository *repo, int argc, char **argv)
{
- git_repository *repo;
- git_libgit2_init();
+ UNUSED(argv);
- if (argc != 1 || argv[1] /* silence -Wunused-parameter */)
+ if (argc != 1)
fatal("Sorry, no for-each-ref options supported yet", NULL);
- check_lg2(git_repository_open(&repo, "."),
- "Could not open repository", NULL);
check_lg2(git_reference_foreach(repo, show_ref, repo),
"Could not iterate over references", NULL);
- git_libgit2_shutdown();
return 0;
}
diff --git a/examples/general.c b/examples/general.c
index e0f5b5ec5..2e9a32151 100644
--- a/examples/general.c
+++ b/examples/general.c
@@ -76,12 +76,11 @@ static void check_error(int error_code, const char *action)
exit(1);
}
-int main (int argc, char** argv)
+int lg2_general(git_repository *repo, int argc, char** argv)
{
int error;
git_oid oid;
char *repo_path;
- git_repository *repo;
/**
* Initialize the library, this will set up any global state which libgit2 needs
diff --git a/examples/network/index-pack.c b/examples/index-pack.c
index 87445e439..df11177c6 100644
--- a/examples/network/index-pack.c
+++ b/examples/index-pack.c
@@ -1,4 +1,4 @@
-#include "../common.h"
+#include "common.h"
#include <sys/types.h>
#include <sys/stat.h>
@@ -28,7 +28,7 @@ static int index_cb(const git_transfer_progress *stats, void *data)
return 0;
}
-int index_pack(git_repository *repo, int argc, char **argv)
+int lg2_index_pack(git_repository *repo, int argc, char **argv)
{
git_indexer *idx;
git_transfer_progress stats = {0, 0};
diff --git a/examples/init.c b/examples/init.c
index fe7a67224..23044d10d 100644
--- a/examples/init.c
+++ b/examples/init.c
@@ -40,14 +40,10 @@ struct opts {
static void create_initial_commit(git_repository *repo);
static void parse_opts(struct opts *o, int argc, char *argv[]);
-
-int main(int argc, char *argv[])
+int lg2_init(git_repository *repo, int argc, char *argv[])
{
- git_repository *repo = NULL;
struct opts o = { 1, 0, 0, 0, GIT_REPOSITORY_INIT_SHARED_UMASK, 0, 0, 0 };
- git_libgit2_init();
-
parse_opts(&o, argc, argv);
/* Initialize repository. */
@@ -116,7 +112,6 @@ int main(int argc, char *argv[])
}
git_repository_free(repo);
- git_libgit2_shutdown();
return 0;
}
diff --git a/examples/lg2.c b/examples/lg2.c
new file mode 100644
index 000000000..1f6fd8560
--- /dev/null
+++ b/examples/lg2.c
@@ -0,0 +1,109 @@
+#include "common.h"
+
+/* This part is not strictly libgit2-dependent, but you can use this
+ * as a starting point for a git-like tool */
+
+typedef int (*git_command_fn)(git_repository *, int , char **);
+
+struct {
+ char *name;
+ git_command_fn fn;
+ char requires_repo;
+} commands[] = {
+ { "add", lg2_add, 1 },
+ { "blame", lg2_blame, 1 },
+ { "cat-file", lg2_cat_file, 1 },
+ { "checkout", lg2_checkout, 1 },
+ { "clone", lg2_clone, 0 },
+ { "describe", lg2_describe, 1 },
+ { "diff", lg2_diff, 1 },
+ { "fetch", lg2_fetch, 1 },
+ { "for-each-ref", lg2_for_each_ref, 1 },
+ { "general", lg2_general, 0 },
+ { "index-pack", lg2_index_pack, 1 },
+ { "init", lg2_init, 0 },
+ { "log", lg2_log, 1 },
+ { "ls-files", lg2_ls_files, 1 },
+ { "ls-remote", lg2_ls_remote, 1 },
+ { "merge", lg2_merge, 1 },
+ { "remote", lg2_remote, 1 },
+ { "rev-list", lg2_rev_list, 1 },
+ { "rev-parse", lg2_rev_parse, 1 },
+ { "show-index", lg2_show_index, 0 },
+ { "status", lg2_status, 1 },
+ { "tag", lg2_tag, 1 },
+};
+
+static int run_command(git_command_fn fn, git_repository *repo, struct args_info args)
+{
+ int error;
+
+ /* Run the command. If something goes wrong, print the error message to stderr */
+ error = fn(repo, args.argc - args.pos, &args.argv[args.pos]);
+ if (error < 0) {
+ if (git_error_last() == NULL)
+ fprintf(stderr, "Error without message");
+ else
+ fprintf(stderr, "Bad news:\n %s\n", git_error_last()->message);
+ }
+
+ return !!error;
+}
+
+int main(int argc, char **argv)
+{
+ struct args_info args = ARGS_INFO_INIT;
+ git_repository *repo = NULL;
+ const char *git_dir = NULL;
+ int return_code = 1;
+ size_t i;
+
+ if (argc < 2) {
+ fprintf(stderr, "usage: %s <cmd> [repo]\n", argv[0]);
+ exit(EXIT_FAILURE);
+ }
+
+ git_libgit2_init();
+
+ for (args.pos = 1; args.pos < args.argc; ++args.pos) {
+ char *a = args.argv[args.pos];
+
+ if (a[0] != '-') {
+ /* non-arg */
+ break;
+ } else if (optional_str_arg(&git_dir, &args, "--git-dir", ".git")) {
+ continue;
+ } else if (!strcmp(a, "--")) {
+ /* arg separator */
+ break;
+ }
+ }
+
+ if (!git_dir)
+ git_dir = ".";
+
+ for (i = 0; i < ARRAY_SIZE(commands); ++i) {
+ if (strcmp(args.argv[args.pos], commands[i].name))
+ continue;
+
+ /*
+ * Before running the actual command, create an instance
+ * of the local repository and pass it to the function.
+ * */
+ if (commands[i].requires_repo) {
+ check_lg2(git_repository_open_ext(&repo, git_dir, 0, NULL),
+ "Unable to open repository '%s'", git_dir);
+ }
+
+ return_code = run_command(commands[i].fn, repo, args);
+ goto shutdown;
+ }
+
+ fprintf(stderr, "Command not found: %s\n", argv[1]);
+
+shutdown:
+ git_repository_free(repo);
+ git_libgit2_shutdown();
+
+ return return_code;
+}
diff --git a/examples/log.c b/examples/log.c
index 3e891e4d8..a6bd957ae 100644
--- a/examples/log.c
+++ b/examples/log.c
@@ -71,7 +71,7 @@ static int match_with_parent(git_commit *commit, int i, git_diff_options *);
static int signature_matches(const git_signature *sig, const char *filter);
static int log_message_matches(const git_commit *commit, const char *filter);
-int main(int argc, char *argv[])
+int lg2_log(git_repository *repo, int argc, char *argv[])
{
int i, count = 0, printed = 0, parents, last_arg;
struct log_state s;
@@ -81,11 +81,9 @@ int main(int argc, char *argv[])
git_commit *commit = NULL;
git_pathspec *ps = NULL;
- git_libgit2_init();
-
/** Parse arguments and set up revwalker. */
-
last_arg = parse_options(&s, &opt, argc, argv);
+ s.repo = repo;
diffopts.pathspec.strings = &argv[last_arg];
diffopts.pathspec.count = argc - last_arg;
@@ -180,8 +178,6 @@ int main(int argc, char *argv[])
git_pathspec_free(ps);
git_revwalk_free(s.walker);
- git_repository_free(s.repo);
- git_libgit2_shutdown();
return 0;
}
@@ -243,13 +239,6 @@ static int add_revision(struct log_state *s, const char *revstr)
git_revspec revs;
int hide = 0;
- /** Open repo on demand if it isn't already open. */
- if (!s->repo) {
- if (!s->repodir) s->repodir = ".";
- check_lg2(git_repository_open_ext(&s->repo, s->repodir, 0, NULL),
- "Could not open repository", s->repodir);
- }
-
if (!revstr) {
push_rev(s, NULL, hide);
return 0;
diff --git a/examples/ls-files.c b/examples/ls-files.c
index 98c89c93c..b82af0124 100644
--- a/examples/ls-files.c
+++ b/examples/ls-files.c
@@ -13,7 +13,6 @@
*/
#include "common.h"
-#include "array.h"
/**
* This example demonstrates the libgit2 index APIs to roughly
@@ -111,21 +110,15 @@ static int print_paths(ls_options *opts, git_index *index)
return 0;
}
-int main(int argc, char *argv[])
+int lg2_ls_files(git_repository *repo, int argc, char *argv[])
{
- ls_options opts;
- git_repository *repo = NULL;
git_index *index = NULL;
+ ls_options opts;
int error;
if ((error = parse_options(&opts, argc, argv)) < 0)
return error;
- git_libgit2_init();
-
- if ((error = git_repository_open_ext(&repo, ".", 0, NULL)) < 0)
- goto cleanup;
-
if ((error = git_repository_index(&index, repo)) < 0)
goto cleanup;
@@ -133,8 +126,6 @@ int main(int argc, char *argv[])
cleanup:
git_index_free(index);
- git_repository_free(repo);
- git_libgit2_shutdown();
return error;
}
diff --git a/examples/network/ls-remote.c b/examples/ls-remote.c
index 8181629b9..03ed887d1 100644
--- a/examples/network/ls-remote.c
+++ b/examples/ls-remote.c
@@ -1,4 +1,4 @@
-#include "../common.h"
+#include "common.h"
static int use_remote(git_repository *repo, char *name)
{
@@ -45,7 +45,7 @@ cleanup:
}
/** Entry point for this command */
-int ls_remote(git_repository *repo, int argc, char **argv)
+int lg2_ls_remote(git_repository *repo, int argc, char **argv)
{
int error;
diff --git a/examples/merge.c b/examples/merge.c
index 13e3f9632..a275ecbeb 100644
--- a/examples/merge.c
+++ b/examples/merge.c
@@ -278,9 +278,8 @@ cleanup:
return err;
}
-int main(int argc, char **argv)
+int lg2_merge(git_repository *repo, int argc, char **argv)
{
- git_repository *repo = NULL;
merge_options opts;
git_index *index;
git_repository_state_t state;
@@ -292,11 +291,6 @@ int main(int argc, char **argv)
merge_options_init(&opts);
parse_options(&path, &opts, argc, argv);
- git_libgit2_init();
-
- check_lg2(git_repository_open_ext(&repo, path, 0, NULL),
- "Could not open repository", NULL);
-
state = git_repository_state(repo);
if (state != GIT_REPOSITORY_STATE_NONE) {
fprintf(stderr, "repository is in unexpected state %d\n", state);
@@ -366,8 +360,6 @@ int main(int argc, char **argv)
cleanup:
free(opts.heads);
free(opts.annotated);
- git_repository_free(repo);
- git_libgit2_shutdown();
return 0;
}
diff --git a/examples/network/.gitignore b/examples/network/.gitignore
deleted file mode 100644
index 1b48e66ed..000000000
--- a/examples/network/.gitignore
+++ /dev/null
@@ -1 +0,0 @@
-/git2
diff --git a/examples/network/git2.c b/examples/network/git2.c
deleted file mode 100644
index 4fa28390c..000000000
--- a/examples/network/git2.c
+++ /dev/null
@@ -1,87 +0,0 @@
-#include "../common.h"
-
-/* This part is not strictly libgit2-dependent, but you can use this
- * as a starting point for a git-like tool */
-
-typedef int (*git_cb)(git_repository *, int , char **);
-
-struct {
- char *name;
- git_cb fn;
-} commands[] = {
- {"ls-remote", ls_remote},
- {"fetch", fetch},
- {"clone", do_clone},
- {"index-pack", index_pack},
- { NULL, NULL}
-};
-
-static int run_command(git_cb fn, git_repository *repo, struct args_info args)
-{
- int error;
-
- /* Run the command. If something goes wrong, print the error message to stderr */
- error = fn(repo, args.argc - args.pos, &args.argv[args.pos]);
- if (error < 0) {
- if (git_error_last() == NULL)
- fprintf(stderr, "Error without message");
- else
- fprintf(stderr, "Bad news:\n %s\n", git_error_last()->message);
- }
-
- return !!error;
-}
-
-int main(int argc, char **argv)
-{
- int i;
- int return_code = 1;
- int error;
- git_repository *repo;
- struct args_info args = ARGS_INFO_INIT;
- const char *git_dir = NULL;
-
- if (argc < 2) {
- fprintf(stderr, "usage: %s <cmd> [repo]\n", argv[0]);
- exit(EXIT_FAILURE);
- }
-
- git_libgit2_init();
-
- for (args.pos = 1; args.pos < args.argc; ++args.pos) {
- char *a = args.argv[args.pos];
-
- if (a[0] != '-') {
- /* non-arg */
- break;
- } else if (optional_str_arg(&git_dir, &args, "--git-dir", ".git")) {
- continue;
- } else if (!strcmp(a, "--")) {
- /* arg separator */
- break;
- }
- }
-
- /* Before running the actual command, create an instance of the local
- * repository and pass it to the function. */
-
- error = git_repository_open(&repo, git_dir);
- if (error < 0)
- repo = NULL;
-
- for (i = 0; commands[i].name != NULL; ++i) {
- if (!strcmp(args.argv[args.pos], commands[i].name)) {
- return_code = run_command(commands[i].fn, repo, args);
- goto shutdown;
- }
- }
-
- fprintf(stderr, "Command not found: %s\n", argv[1]);
-
-shutdown:
- git_repository_free(repo);
-
- git_libgit2_shutdown();
-
- return return_code;
-}
diff --git a/examples/remote.c b/examples/remote.c
index 1cb61ccd8..7345f82de 100644
--- a/examples/remote.c
+++ b/examples/remote.c
@@ -48,24 +48,13 @@ static void parse_subcmd(
struct opts *opt, int argc, char **argv);
static void usage(const char *msg, const char *arg);
-int main(int argc, char *argv[])
+int lg2_remote(git_repository *repo, int argc, char *argv[])
{
int retval = 0;
struct opts opt = {0};
- git_buf buf = GIT_BUF_INIT_CONST(NULL, 0);
- git_repository *repo = NULL;
parse_subcmd(&opt, argc, argv);
- git_libgit2_init();
-
- check_lg2(git_repository_discover(&buf, ".", 0, NULL),
- "Could not find repository", NULL);
-
- check_lg2(git_repository_open(&repo, buf.ptr),
- "Could not open repository", NULL);
- git_buf_dispose(&buf);
-
switch (opt.cmd)
{
case subcmd_add:
@@ -85,8 +74,6 @@ int main(int argc, char *argv[])
break;
}
- git_libgit2_shutdown();
-
return retval;
}
diff --git a/examples/rev-list.c b/examples/rev-list.c
index ee9afc441..c366ecea2 100644
--- a/examples/rev-list.c
+++ b/examples/rev-list.c
@@ -17,16 +17,12 @@
static int revwalk_parseopts(git_repository *repo, git_revwalk *walk, int nopts, char **opts);
-int main (int argc, char **argv)
+int lg2_rev_list(git_repository *repo, int argc, char **argv)
{
- git_repository *repo;
git_revwalk *walk;
git_oid oid;
char buf[GIT_OID_HEXSZ+1];
- git_libgit2_init();
-
- check_lg2(git_repository_open_ext(&repo, ".", 0, NULL), "opening repository", NULL);
check_lg2(git_revwalk_new(&walk, repo), "allocating revwalk", NULL);
check_lg2(revwalk_parseopts(repo, walk, argc-1, argv+1), "parsing options", NULL);
@@ -36,7 +32,6 @@ int main (int argc, char **argv)
printf("%s\n", buf);
}
- git_libgit2_shutdown();
return 0;
}
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);
diff --git a/examples/showindex.c b/examples/show-index.c
index 43be5e24c..6acadbeb6 100644
--- a/examples/showindex.c
+++ b/examples/show-index.c
@@ -14,7 +14,7 @@
#include "common.h"
-int main (int argc, char** argv)
+int lg2_show_index(git_repository *repo, int argc, char** argv)
{
git_index *index;
unsigned int i, ecount;
@@ -23,8 +23,6 @@ int main (int argc, char** argv)
char out[GIT_OID_HEXSZ+1];
out[GIT_OID_HEXSZ] = '\0';
- git_libgit2_init();
-
if (argc > 2)
fatal("usage: showindex [<repo-dir>]", NULL);
if (argc > 1)
@@ -34,7 +32,6 @@ int main (int argc, char** argv)
if (dirlen > 5 && strcmp(dir + dirlen - 5, "index") == 0) {
check_lg2(git_index_open(&index, dir), "could not open index", dir);
} else {
- git_repository *repo;
check_lg2(git_repository_open_ext(&repo, dir, 0, NULL), "could not open repository", dir);
check_lg2(git_repository_index(&index, repo), "could not open repository index", NULL);
git_repository_free(repo);
@@ -64,7 +61,6 @@ int main (int argc, char** argv)
}
git_index_free(index);
- git_libgit2_shutdown();
return 0;
}
diff --git a/examples/status.c b/examples/status.c
index 49f006dcc..38e5120e8 100644
--- a/examples/status.c
+++ b/examples/status.c
@@ -67,14 +67,11 @@ static void print_long(git_status_list *status);
static void print_short(git_repository *repo, git_status_list *status);
static int print_submod(git_submodule *sm, const char *name, void *payload);
-int main(int argc, char *argv[])
+int lg2_status(git_repository *repo, int argc, char *argv[])
{
- git_repository *repo = NULL;
git_status_list *status;
struct opts o = { GIT_STATUS_OPTIONS_INIT, "." };
- git_libgit2_init();
-
o.statusopt.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
o.statusopt.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
@@ -82,13 +79,6 @@ int main(int argc, char *argv[])
parse_opts(&o, argc, argv);
- /**
- * Try to open the repository at the given path (or at the current
- * directory if none was given).
- */
- check_lg2(git_repository_open_ext(&repo, o.repodir, 0, NULL),
- "Could not open repository", o.repodir);
-
if (git_repository_is_bare(repo))
fatal("Cannot report status on bare repository",
git_repository_path(repo));
@@ -134,9 +124,6 @@ show_status:
goto show_status;
}
- git_repository_free(repo);
- git_libgit2_shutdown();
-
return 0;
}
diff --git a/examples/tag.c b/examples/tag.c
index fa405b8de..7bfb92a3e 100644
--- a/examples/tag.c
+++ b/examples/tag.c
@@ -293,18 +293,12 @@ static void tag_options_init(tag_options *opts)
opts->force = 0;
}
-int main(int argc, char **argv)
+int lg2_tag(git_repository *repo, int argc, char **argv)
{
- git_repository *repo;
tag_options opts;
tag_action action;
tag_state state;
- git_libgit2_init();
-
- check_lg2(git_repository_open_ext(&repo, ".", 0, NULL),
- "Could not open repository", NULL);
-
tag_options_init(&opts);
parse_options(&action, &opts, argc, argv);
@@ -312,8 +306,5 @@ int main(int argc, char **argv)
state.opts = &opts;
action(&state);
- git_repository_free(repo);
- git_libgit2_shutdown();
-
return 0;
}