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(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()
......@@ -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;
}
......
......@@ -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;
}
......
......@@ -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;
}
......
......@@ -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;
}
#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;
......
......@@ -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);
......@@ -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;
}
......@@ -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;
}
......
#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;
......
......@@ -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;
}
......@@ -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
......
#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};
......
......@@ -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;
}
......
#include "../common.h"
#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 **);
typedef int (*git_command_fn)(git_repository *, int , char **);
struct {
char *name;
git_cb fn;
git_command_fn fn;
char requires_repo;
} commands[] = {
{"ls-remote", ls_remote},
{"fetch", fetch},
{"clone", do_clone},
{"index-pack", index_pack},
{ NULL, NULL}
{ "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_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;
......@@ -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 i;
int return_code = 1;
int error;
git_repository *repo;
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]);
......@@ -62,25 +79,30 @@ int main(int argc, char **argv)
}
}
/* Before running the actual command, create an instance of the local
* repository and pass it to the function. */
if (!git_dir)
git_dir = ".";
for (i = 0; i < ARRAY_SIZE(commands); ++i) {
if (strcmp(args.argv[args.pos], commands[i].name))
continue;
error = git_repository_open(&repo, git_dir);
if (error < 0)
repo = NULL;
/*
* 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);
}
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;
......
......@@ -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;
......
......@@ -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;
}
#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;
......
......@@ -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;
}
......@@ -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;
}
......
......@@ -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;
}
......
......@@ -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);
......
......@@ -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;
}
......@@ -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;
}
......
......@@ -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;
}
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