summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTomas Paladin Volf <paladin@jstation.cz>2015-05-02 12:16:22 +0200
committerTomas Paladin Volf <paladin@jstation.cz>2015-05-02 12:16:22 +0200
commit785990bead1a717698eb0c2603e3d0b2ee51262f (patch)
tree780632703e0df482f23b04847c824fbaa66363cc
parent9bff15f4a4e5e71d2c62747e826147f67ee7bd25 (diff)
downloadlibgit2-785990bead1a717698eb0c2603e3d0b2ee51262f.tar.gz
Restructured to be nicer example
Code restructured to better represent best practice when using libgit2.
-rw-r--r--examples/network/git2.c23
1 files changed, 14 insertions, 9 deletions
diff --git a/examples/network/git2.c b/examples/network/git2.c
index 1daff62ff..448103c46 100644
--- a/examples/network/git2.c
+++ b/examples/network/git2.c
@@ -23,10 +23,8 @@ static int run_command(git_cb fn, int argc, char **argv)
int error;
git_repository *repo;
- git_libgit2_init();
-
-// Before running the actual command, create an instance of the local
-// repository and pass it to the function.
+ // Before running the actual command, create an instance of the local
+ // repository and pass it to the function.
error = git_repository_open(&repo, ".git");
if (error < 0)
@@ -44,25 +42,32 @@ static int run_command(git_cb fn, int argc, char **argv)
if(repo)
git_repository_free(repo);
- git_libgit2_shutdown();
-
return !!error;
}
int main(int argc, char **argv)
{
int i;
+ int return_code = 1;
if (argc < 2) {
fprintf(stderr, "usage: %s <cmd> [repo]\n", argv[0]);
exit(EXIT_FAILURE);
}
+ git_libgit2_init();
+
for (i = 0; commands[i].name != NULL; ++i) {
- if (!strcmp(argv[1], commands[i].name))
- return run_command(commands[i].fn, --argc, ++argv);
+ if (!strcmp(argv[1], commands[i].name)) {
+ return_code = run_command(commands[i].fn, --argc, ++argv);
+ goto shutdown;
+ }
}
fprintf(stderr, "Command not found: %s\n", argv[1]);
- return 1;
+
+shutdown:
+ git_libgit2_shutdown();
+
+ return return_code;
}