Commit ead10785 by Patrick Steinhardt

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.
parent 7562422a
INCLUDE_DIRECTORIES(${LIBGIT2_INCLUDES}) INCLUDE_DIRECTORIES(${LIBGIT2_INCLUDES})
INCLUDE_DIRECTORIES(SYSTEM ${LIBGIT2_SYSTEM_INCLUDES}) INCLUDE_DIRECTORIES(SYSTEM ${LIBGIT2_SYSTEM_INCLUDES})
FILE(GLOB_RECURSE SRC_EXAMPLE_GIT2 network/*.c common.?) FILE(GLOB LG2_SOURCES *.c)
ADD_EXECUTABLE(cgit2 ${SRC_EXAMPLE_GIT2}) ADD_EXECUTABLE(lg2 ${LG2_SOURCES})
SET_TARGET_PROPERTIES(cgit2 PROPERTIES C_STANDARD 90) SET_TARGET_PROPERTIES(lg2 PROPERTIES C_STANDARD 90)
# Ensure that we do not use deprecated functions internally # Ensure that we do not use deprecated functions internally
ADD_DEFINITIONS(-DGIT_DEPRECATE_HARD) ADD_DEFINITIONS(-DGIT_DEPRECATE_HARD)
IF(WIN32 OR ANDROID) IF(WIN32 OR ANDROID)
TARGET_LINK_LIBRARIES(cgit2 git2) TARGET_LINK_LIBRARIES(lg2 git2)
ELSE() ELSE()
TARGET_LINK_LIBRARIES(cgit2 git2 pthread) TARGET_LINK_LIBRARIES(lg2 git2 pthread)
ENDIF() 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()
...@@ -31,22 +31,18 @@ static void parse_opts(int *options, int *count, int argc, char *argv[]); ...@@ -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); void init_array(git_strarray *array, int argc, char **argv);
int print_matched_cb(const char *path, const char *matched_pathspec, void *payload); 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_index_matched_path_cb matched_cb = NULL;
git_repository *repo = NULL;
git_index *index; git_index *index;
git_strarray array = {0}; git_strarray array = {0};
int options = 0, count = 0; int options = 0, count = 0;
struct print_payload payload = {0}; struct print_payload payload = {0};
git_libgit2_init();
parse_opts(&options, &count, argc, argv); parse_opts(&options, &count, argc, argv);
init_array(&array, argc-count, argv+count); 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); check_lg2(git_repository_index(&index, repo), "Could not open repository index", NULL);
if (options&VERBOSE || options&SKIP) { if (options&VERBOSE || options&SKIP) {
...@@ -64,9 +60,6 @@ int main (int argc, char** argv) ...@@ -64,9 +60,6 @@ int main (int argc, char** argv)
git_index_write(index); git_index_write(index);
git_index_free(index); git_index_free(index);
git_repository_free(repo);
git_libgit2_shutdown();
return 0; return 0;
} }
......
...@@ -35,30 +35,24 @@ struct opts { ...@@ -35,30 +35,24 @@ struct opts {
}; };
static void parse_opts(struct opts *o, int argc, char *argv[]); 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; int line, break_on_null_hunk;
size_t i, rawsize; size_t i, rawsize;
char spec[1024] = {0}; char spec[1024] = {0};
struct opts o = {0}; struct opts o = {0};
const char *rawdata; const char *rawdata;
git_repository *repo = NULL;
git_revspec revspec = {0}; git_revspec revspec = {0};
git_blame_options blameopts = GIT_BLAME_OPTIONS_INIT; git_blame_options blameopts = GIT_BLAME_OPTIONS_INIT;
git_blame *blame = NULL; git_blame *blame = NULL;
git_blob *blob; git_blob *blob;
git_object *obj; git_object *obj;
git_libgit2_init();
parse_opts(&o, argc, argv); parse_opts(&o, argc, argv);
if (o.M) blameopts.flags |= GIT_BLAME_TRACK_COPIES_SAME_COMMIT_MOVES; 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.C) blameopts.flags |= GIT_BLAME_TRACK_COPIES_SAME_COMMIT_COPIES;
if (o.F) blameopts.flags |= GIT_BLAME_FIRST_PARENT; 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 * The commit range comes in "commitish" form. Use the rev-parse API to
* nail down the end points. * nail down the end points.
...@@ -131,9 +125,6 @@ int main(int argc, char *argv[]) ...@@ -131,9 +125,6 @@ int main(int argc, char *argv[])
/** Cleanup. */ /** Cleanup. */
git_blob_free(blob); git_blob_free(blob);
git_blame_free(blame); git_blame_free(blame);
git_repository_free(repo);
git_libgit2_shutdown();
return 0; return 0;
} }
......
...@@ -120,19 +120,14 @@ static void parse_opts(struct opts *o, int argc, char *argv[]); ...@@ -120,19 +120,14 @@ static void parse_opts(struct opts *o, int argc, char *argv[]);
/** Entry point for this command */ /** 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 }; struct opts o = { ".", NULL, 0, 0 };
git_object *obj = NULL; git_object *obj = NULL;
char oidstr[GIT_OID_HEXSZ + 1]; char oidstr[GIT_OID_HEXSZ + 1];
git_libgit2_init();
parse_opts(&o, argc, argv); 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), check_lg2(git_revparse_single(&obj, repo, o.rev),
"Could not resolve", o.rev); "Could not resolve", o.rev);
...@@ -188,9 +183,6 @@ int main(int argc, char *argv[]) ...@@ -188,9 +183,6 @@ int main(int argc, char *argv[])
} }
git_object_free(obj); git_object_free(obj);
git_repository_free(repo);
git_libgit2_shutdown();
return 0; return 0;
} }
......
...@@ -172,9 +172,8 @@ cleanup: ...@@ -172,9 +172,8 @@ cleanup:
} }
/** That example's entry point */ /** 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; struct args_info args = ARGS_INFO_INIT;
checkout_options opts; checkout_options opts;
git_repository_state_t state; git_repository_state_t state;
...@@ -185,15 +184,6 @@ int main(int argc, char **argv) ...@@ -185,15 +184,6 @@ int main(int argc, char **argv)
/** Parse our command line options */ /** Parse our command line options */
parse_options(&path, &opts, &args); 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 */ /** Make sure we're not about to checkout while something else is going on */
state = git_repository_state(repo); state = git_repository_state(repo);
if (state != GIT_REPOSITORY_STATE_NONE) { if (state != GIT_REPOSITORY_STATE_NONE) {
...@@ -228,8 +218,5 @@ int main(int argc, char **argv) ...@@ -228,8 +218,5 @@ int main(int argc, char **argv)
cleanup: cleanup:
git_annotated_commit_free(checkout_target); git_annotated_commit_free(checkout_target);
git_repository_free(repo);
git_libgit2_shutdown();
return err; return err;
} }
#include "../common.h" #include "common.h"
typedef struct progress_data { typedef struct progress_data {
git_transfer_progress fetch_progress; git_transfer_progress fetch_progress;
...@@ -63,7 +63,7 @@ static void checkout_progress(const char *path, size_t cur, size_t tot, void *pa ...@@ -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}}; progress_data pd = {{0}};
git_repository *cloned_repo = NULL; git_repository *cloned_repo = NULL;
......
...@@ -26,8 +26,32 @@ ...@@ -26,8 +26,32 @@
#endif #endif
#endif #endif
#define ARRAY_SIZE(x) (sizeof(x)/sizeof(*x))
#define UNUSED(x) (void)(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 * Check libgit2 error code, printing error to stderr on failure and
* exiting the program. * exiting the program.
...@@ -142,8 +166,3 @@ extern int cred_acquire_cb(git_cred **out, ...@@ -142,8 +166,3 @@ extern int cred_acquire_cb(git_cred **out,
const char *username_from_url, const char *username_from_url,
unsigned int allowed_types, unsigned int allowed_types,
void *payload); 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);
...@@ -152,23 +152,14 @@ static void describe_options_init(describe_options *opts) ...@@ -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); 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; describe_options opts;
git_libgit2_init();
check_lg2(git_repository_open_ext(&repo, ".", 0, NULL),
"Could not open repository", NULL);
describe_options_init(&opts); describe_options_init(&opts);
parse_options(&opts, argc, argv); parse_options(&opts, argc, argv);
do_describe(repo, &opts); do_describe(repo, &opts);
git_repository_free(repo);
git_libgit2_shutdown();
return 0; return 0;
} }
...@@ -67,9 +67,8 @@ static int color_printer( ...@@ -67,9 +67,8 @@ static int color_printer(
const git_diff_delta*, const git_diff_hunk*, const git_diff_line*, void*); const git_diff_delta*, const git_diff_hunk*, const git_diff_line*, void*);
static void diff_print_stats(git_diff *diff, struct opts *o); 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_tree *t1 = NULL, *t2 = NULL;
git_diff *diff; git_diff *diff;
struct opts o = { struct opts o = {
...@@ -77,13 +76,8 @@ int main(int argc, char *argv[]) ...@@ -77,13 +76,8 @@ int main(int argc, char *argv[])
-1, 0, 0, GIT_DIFF_FORMAT_PATCH, NULL, NULL, "." -1, 0, 0, GIT_DIFF_FORMAT_PATCH, NULL, NULL, "."
}; };
git_libgit2_init();
parse_opts(&o, argc, argv); 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: * Possible argument patterns:
* *
...@@ -157,13 +151,9 @@ int main(int argc, char *argv[]) ...@@ -157,13 +151,9 @@ int main(int argc, char *argv[])
} }
/** Cleanup before exiting. */ /** Cleanup before exiting. */
git_diff_free(diff); git_diff_free(diff);
git_tree_free(t1); git_tree_free(t1);
git_tree_free(t2); git_tree_free(t2);
git_repository_free(repo);
git_libgit2_shutdown();
return 0; return 0;
} }
......
#include "../common.h" #include "common.h"
static int progress_cb(const char *str, int len, void *data) 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 ...@@ -54,7 +54,7 @@ static int transfer_progress_cb(const git_transfer_progress *stats, void *payloa
} }
/** Entry point for this command */ /** 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; git_remote *remote = NULL;
const git_transfer_progress *stats; const git_transfer_progress *stats;
......
...@@ -31,19 +31,15 @@ static int show_ref(git_reference *ref, void *data) ...@@ -31,19 +31,15 @@ static int show_ref(git_reference *ref, void *data)
return 0; return 0;
} }
int main(int argc, char **argv) int lg2_for_each_ref(git_repository *repo, int argc, char **argv)
{ {
git_repository *repo; UNUSED(argv);
git_libgit2_init();
if (argc != 1 || argv[1] /* silence -Wunused-parameter */) if (argc != 1)
fatal("Sorry, no for-each-ref options supported yet", NULL); 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), check_lg2(git_reference_foreach(repo, show_ref, repo),
"Could not iterate over references", NULL); "Could not iterate over references", NULL);
git_libgit2_shutdown();
return 0; return 0;
} }
...@@ -76,12 +76,11 @@ static void check_error(int error_code, const char *action) ...@@ -76,12 +76,11 @@ static void check_error(int error_code, const char *action)
exit(1); exit(1);
} }
int main (int argc, char** argv) int lg2_general(git_repository *repo, int argc, char** argv)
{ {
int error; int error;
git_oid oid; git_oid oid;
char *repo_path; char *repo_path;
git_repository *repo;
/** /**
* Initialize the library, this will set up any global state which libgit2 needs * Initialize the library, this will set up any global state which libgit2 needs
......
#include "../common.h" #include "common.h"
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
...@@ -28,7 +28,7 @@ static int index_cb(const git_transfer_progress *stats, void *data) ...@@ -28,7 +28,7 @@ static int index_cb(const git_transfer_progress *stats, void *data)
return 0; 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_indexer *idx;
git_transfer_progress stats = {0, 0}; git_transfer_progress stats = {0, 0};
......
...@@ -40,14 +40,10 @@ struct opts { ...@@ -40,14 +40,10 @@ struct opts {
static void create_initial_commit(git_repository *repo); static void create_initial_commit(git_repository *repo);
static void parse_opts(struct opts *o, int argc, char *argv[]); static void parse_opts(struct opts *o, int argc, char *argv[]);
int lg2_init(git_repository *repo, int argc, char *argv[])
int main(int argc, char *argv[])
{ {
git_repository *repo = NULL;
struct opts o = { 1, 0, 0, 0, GIT_REPOSITORY_INIT_SHARED_UMASK, 0, 0, 0 }; struct opts o = { 1, 0, 0, 0, GIT_REPOSITORY_INIT_SHARED_UMASK, 0, 0, 0 };
git_libgit2_init();
parse_opts(&o, argc, argv); parse_opts(&o, argc, argv);
/* Initialize repository. */ /* Initialize repository. */
...@@ -116,7 +112,6 @@ int main(int argc, char *argv[]) ...@@ -116,7 +112,6 @@ int main(int argc, char *argv[])
} }
git_repository_free(repo); git_repository_free(repo);
git_libgit2_shutdown();
return 0; return 0;
} }
......
#include "../common.h" #include "common.h"
/* This part is not strictly libgit2-dependent, but you can use this /* This part is not strictly libgit2-dependent, but you can use this
* as a starting point for a git-like tool */ * as a starting point for a git-like tool */
typedef int (*git_cb)(git_repository *, int , char **); typedef int (*git_command_fn)(git_repository *, int , char **);
struct { struct {
char *name; char *name;
git_cb fn; git_command_fn fn;
char requires_repo;
} commands[] = { } commands[] = {
{"ls-remote", ls_remote}, { "add", lg2_add, 1 },
{"fetch", fetch}, { "blame", lg2_blame, 1 },
{"clone", do_clone}, { "cat-file", lg2_cat_file, 1 },
{"index-pack", index_pack}, { "checkout", lg2_checkout, 1 },
{ NULL, NULL} { "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_cb fn, git_repository *repo, struct args_info args) static int run_command(git_command_fn fn, git_repository *repo, struct args_info args)
{ {
int error; int error;
...@@ -34,12 +52,11 @@ static int run_command(git_cb fn, git_repository *repo, struct args_info args) ...@@ -34,12 +52,11 @@ static int run_command(git_cb fn, git_repository *repo, struct args_info args)
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
int i;
int return_code = 1;
int error;
git_repository *repo;
struct args_info args = ARGS_INFO_INIT; struct args_info args = ARGS_INFO_INIT;
git_repository *repo = NULL;
const char *git_dir = NULL; const char *git_dir = NULL;
int return_code = 1;
size_t i;
if (argc < 2) { if (argc < 2) {
fprintf(stderr, "usage: %s <cmd> [repo]\n", argv[0]); fprintf(stderr, "usage: %s <cmd> [repo]\n", argv[0]);
...@@ -62,25 +79,30 @@ int main(int argc, char **argv) ...@@ -62,25 +79,30 @@ int main(int argc, char **argv)
} }
} }
/* Before running the actual command, create an instance of the local if (!git_dir)
* repository and pass it to the function. */ git_dir = ".";
error = git_repository_open(&repo, git_dir); for (i = 0; i < ARRAY_SIZE(commands); ++i) {
if (error < 0) if (strcmp(args.argv[args.pos], commands[i].name))
repo = NULL; continue;
for (i = 0; commands[i].name != NULL; ++i) { /*
if (!strcmp(args.argv[args.pos], commands[i].name)) { * Before running the actual command, create an instance
return_code = run_command(commands[i].fn, repo, args); * of the local repository and pass it to the function.
goto shutdown; * */
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]); fprintf(stderr, "Command not found: %s\n", argv[1]);
shutdown: shutdown:
git_repository_free(repo); git_repository_free(repo);
git_libgit2_shutdown(); git_libgit2_shutdown();
return return_code; return return_code;
......
...@@ -71,7 +71,7 @@ static int match_with_parent(git_commit *commit, int i, git_diff_options *); ...@@ -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 signature_matches(const git_signature *sig, const char *filter);
static int log_message_matches(const git_commit *commit, 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; int i, count = 0, printed = 0, parents, last_arg;
struct log_state s; struct log_state s;
...@@ -81,11 +81,9 @@ int main(int argc, char *argv[]) ...@@ -81,11 +81,9 @@ int main(int argc, char *argv[])
git_commit *commit = NULL; git_commit *commit = NULL;
git_pathspec *ps = NULL; git_pathspec *ps = NULL;
git_libgit2_init();
/** Parse arguments and set up revwalker. */ /** Parse arguments and set up revwalker. */
last_arg = parse_options(&s, &opt, argc, argv); last_arg = parse_options(&s, &opt, argc, argv);
s.repo = repo;
diffopts.pathspec.strings = &argv[last_arg]; diffopts.pathspec.strings = &argv[last_arg];
diffopts.pathspec.count = argc - last_arg; diffopts.pathspec.count = argc - last_arg;
...@@ -180,8 +178,6 @@ int main(int argc, char *argv[]) ...@@ -180,8 +178,6 @@ int main(int argc, char *argv[])
git_pathspec_free(ps); git_pathspec_free(ps);
git_revwalk_free(s.walker); git_revwalk_free(s.walker);
git_repository_free(s.repo);
git_libgit2_shutdown();
return 0; return 0;
} }
...@@ -243,13 +239,6 @@ static int add_revision(struct log_state *s, const char *revstr) ...@@ -243,13 +239,6 @@ static int add_revision(struct log_state *s, const char *revstr)
git_revspec revs; git_revspec revs;
int hide = 0; 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) { if (!revstr) {
push_rev(s, NULL, hide); push_rev(s, NULL, hide);
return 0; return 0;
......
...@@ -13,7 +13,6 @@ ...@@ -13,7 +13,6 @@
*/ */
#include "common.h" #include "common.h"
#include "array.h"
/** /**
* This example demonstrates the libgit2 index APIs to roughly * This example demonstrates the libgit2 index APIs to roughly
...@@ -111,21 +110,15 @@ static int print_paths(ls_options *opts, git_index *index) ...@@ -111,21 +110,15 @@ static int print_paths(ls_options *opts, git_index *index)
return 0; 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; git_index *index = NULL;
ls_options opts;
int error; int error;
if ((error = parse_options(&opts, argc, argv)) < 0) if ((error = parse_options(&opts, argc, argv)) < 0)
return error; 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) if ((error = git_repository_index(&index, repo)) < 0)
goto cleanup; goto cleanup;
...@@ -133,8 +126,6 @@ int main(int argc, char *argv[]) ...@@ -133,8 +126,6 @@ int main(int argc, char *argv[])
cleanup: cleanup:
git_index_free(index); git_index_free(index);
git_repository_free(repo);
git_libgit2_shutdown();
return error; return error;
} }
#include "../common.h" #include "common.h"
static int use_remote(git_repository *repo, char *name) static int use_remote(git_repository *repo, char *name)
{ {
...@@ -45,7 +45,7 @@ cleanup: ...@@ -45,7 +45,7 @@ cleanup:
} }
/** Entry point for this command */ /** 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; int error;
......
...@@ -278,9 +278,8 @@ cleanup: ...@@ -278,9 +278,8 @@ cleanup:
return err; 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; merge_options opts;
git_index *index; git_index *index;
git_repository_state_t state; git_repository_state_t state;
...@@ -292,11 +291,6 @@ int main(int argc, char **argv) ...@@ -292,11 +291,6 @@ int main(int argc, char **argv)
merge_options_init(&opts); merge_options_init(&opts);
parse_options(&path, &opts, argc, argv); 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); state = git_repository_state(repo);
if (state != GIT_REPOSITORY_STATE_NONE) { if (state != GIT_REPOSITORY_STATE_NONE) {
fprintf(stderr, "repository is in unexpected state %d\n", state); fprintf(stderr, "repository is in unexpected state %d\n", state);
...@@ -366,8 +360,6 @@ int main(int argc, char **argv) ...@@ -366,8 +360,6 @@ int main(int argc, char **argv)
cleanup: cleanup:
free(opts.heads); free(opts.heads);
free(opts.annotated); free(opts.annotated);
git_repository_free(repo);
git_libgit2_shutdown();
return 0; return 0;
} }
...@@ -48,24 +48,13 @@ static void parse_subcmd( ...@@ -48,24 +48,13 @@ static void parse_subcmd(
struct opts *opt, int argc, char **argv); struct opts *opt, int argc, char **argv);
static void usage(const char *msg, const char *arg); 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; int retval = 0;
struct opts opt = {0}; struct opts opt = {0};
git_buf buf = GIT_BUF_INIT_CONST(NULL, 0);
git_repository *repo = NULL;
parse_subcmd(&opt, argc, argv); 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) switch (opt.cmd)
{ {
case subcmd_add: case subcmd_add:
...@@ -85,8 +74,6 @@ int main(int argc, char *argv[]) ...@@ -85,8 +74,6 @@ int main(int argc, char *argv[])
break; break;
} }
git_libgit2_shutdown();
return retval; return retval;
} }
......
...@@ -17,16 +17,12 @@ ...@@ -17,16 +17,12 @@
static int revwalk_parseopts(git_repository *repo, git_revwalk *walk, int nopts, char **opts); 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_revwalk *walk;
git_oid oid; git_oid oid;
char buf[GIT_OID_HEXSZ+1]; 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(git_revwalk_new(&walk, repo), "allocating revwalk", NULL);
check_lg2(revwalk_parseopts(repo, walk, argc-1, argv+1), "parsing options", NULL); check_lg2(revwalk_parseopts(repo, walk, argc-1, argv+1), "parsing options", NULL);
...@@ -36,7 +32,6 @@ int main (int argc, char **argv) ...@@ -36,7 +32,6 @@ int main (int argc, char **argv)
printf("%s\n", buf); printf("%s\n", buf);
} }
git_libgit2_shutdown();
return 0; return 0;
} }
......
...@@ -16,26 +16,20 @@ ...@@ -16,26 +16,20 @@
/** Forward declarations for helpers. */ /** Forward declarations for helpers. */
struct parse_state { struct parse_state {
git_repository *repo;
const char *repodir; const char *repodir;
const char *spec; const char *spec;
int not; int not;
}; };
static void parse_opts(struct parse_state *ps, int argc, char *argv[]); 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 lg2_rev_parse(git_repository *repo, int argc, char *argv[])
int main(int argc, char *argv[])
{ {
struct parse_state ps = {0}; struct parse_state ps = {0};
git_libgit2_init();
parse_opts(&ps, argc, argv); parse_opts(&ps, argc, argv);
check_lg2(parse_revision(&ps), "Parsing", NULL); check_lg2(parse_revision(repo, &ps), "Parsing", NULL);
git_repository_free(ps.repo);
git_libgit2_shutdown();
return 0; return 0;
} }
...@@ -68,19 +62,12 @@ static void parse_opts(struct parse_state *ps, int argc, char *argv[]) ...@@ -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; git_revspec rs;
char str[GIT_OID_HEXSZ + 1]; char str[GIT_OID_HEXSZ + 1];
if (!ps->repo) { check_lg2(git_revparse(&rs, repo, ps->spec), "Could not parse", ps->spec);
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);
if ((rs.flags & GIT_REVPARSE_SINGLE) != 0) { if ((rs.flags & GIT_REVPARSE_SINGLE) != 0) {
git_oid_tostr(str, sizeof(str), git_object_id(rs.from)); git_oid_tostr(str, sizeof(str), git_object_id(rs.from));
...@@ -94,7 +81,7 @@ static int parse_revision(struct parse_state *ps) ...@@ -94,7 +81,7 @@ static int parse_revision(struct parse_state *ps)
if ((rs.flags & GIT_REVPARSE_MERGE_BASE) != 0) { if ((rs.flags & GIT_REVPARSE_MERGE_BASE) != 0) {
git_oid base; 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)), git_object_id(rs.from), git_object_id(rs.to)),
"Could not find merge base", ps->spec); "Could not find merge base", ps->spec);
......
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
#include "common.h" #include "common.h"
int main (int argc, char** argv) int lg2_show_index(git_repository *repo, int argc, char** argv)
{ {
git_index *index; git_index *index;
unsigned int i, ecount; unsigned int i, ecount;
...@@ -23,8 +23,6 @@ int main (int argc, char** argv) ...@@ -23,8 +23,6 @@ int main (int argc, char** argv)
char out[GIT_OID_HEXSZ+1]; char out[GIT_OID_HEXSZ+1];
out[GIT_OID_HEXSZ] = '\0'; out[GIT_OID_HEXSZ] = '\0';
git_libgit2_init();
if (argc > 2) if (argc > 2)
fatal("usage: showindex [<repo-dir>]", NULL); fatal("usage: showindex [<repo-dir>]", NULL);
if (argc > 1) if (argc > 1)
...@@ -34,7 +32,6 @@ int main (int argc, char** argv) ...@@ -34,7 +32,6 @@ int main (int argc, char** argv)
if (dirlen > 5 && strcmp(dir + dirlen - 5, "index") == 0) { if (dirlen > 5 && strcmp(dir + dirlen - 5, "index") == 0) {
check_lg2(git_index_open(&index, dir), "could not open index", dir); check_lg2(git_index_open(&index, dir), "could not open index", dir);
} else { } else {
git_repository *repo;
check_lg2(git_repository_open_ext(&repo, dir, 0, NULL), "could not open repository", dir); 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); check_lg2(git_repository_index(&index, repo), "could not open repository index", NULL);
git_repository_free(repo); git_repository_free(repo);
...@@ -64,7 +61,6 @@ int main (int argc, char** argv) ...@@ -64,7 +61,6 @@ int main (int argc, char** argv)
} }
git_index_free(index); git_index_free(index);
git_libgit2_shutdown();
return 0; return 0;
} }
...@@ -67,14 +67,11 @@ static void print_long(git_status_list *status); ...@@ -67,14 +67,11 @@ static void print_long(git_status_list *status);
static void print_short(git_repository *repo, 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); 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; git_status_list *status;
struct opts o = { GIT_STATUS_OPTIONS_INIT, "." }; struct opts o = { GIT_STATUS_OPTIONS_INIT, "." };
git_libgit2_init();
o.statusopt.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR; o.statusopt.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
o.statusopt.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED | o.statusopt.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX | GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
...@@ -82,13 +79,6 @@ int main(int argc, char *argv[]) ...@@ -82,13 +79,6 @@ int main(int argc, char *argv[])
parse_opts(&o, argc, 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)) if (git_repository_is_bare(repo))
fatal("Cannot report status on bare repository", fatal("Cannot report status on bare repository",
git_repository_path(repo)); git_repository_path(repo));
...@@ -134,9 +124,6 @@ show_status: ...@@ -134,9 +124,6 @@ show_status:
goto show_status; goto show_status;
} }
git_repository_free(repo);
git_libgit2_shutdown();
return 0; return 0;
} }
......
...@@ -293,18 +293,12 @@ static void tag_options_init(tag_options *opts) ...@@ -293,18 +293,12 @@ static void tag_options_init(tag_options *opts)
opts->force = 0; 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_options opts;
tag_action action; tag_action action;
tag_state state; tag_state state;
git_libgit2_init();
check_lg2(git_repository_open_ext(&repo, ".", 0, NULL),
"Could not open repository", NULL);
tag_options_init(&opts); tag_options_init(&opts);
parse_options(&action, &opts, argc, argv); parse_options(&action, &opts, argc, argv);
...@@ -312,8 +306,5 @@ int main(int argc, char **argv) ...@@ -312,8 +306,5 @@ int main(int argc, char **argv)
state.opts = &opts; state.opts = &opts;
action(&state); action(&state);
git_repository_free(repo);
git_libgit2_shutdown();
return 0; return 0;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment