Skip to content

Commit dbdb22b

Browse files
committed
Clean up showindex sample
1 parent b9d0246 commit dbdb22b

File tree

1 file changed

+15
-23
lines changed

1 file changed

+15
-23
lines changed

examples/showindex.c

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
#include <git2.h>
2-
#include <stdio.h>
3-
#include <string.h>
1+
/*
2+
* Copyright (C) the libgit2 contributors. All rights reserved.
3+
*
4+
* This file is part of libgit2, distributed under the GNU GPL v2 with
5+
* a Linking Exception. For full terms see the included COPYING file.
6+
*/
7+
8+
#include "common.h"
49

510
int main (int argc, char** argv)
611
{
7-
git_repository *repo = NULL;
812
git_index *index;
913
unsigned int i, ecount;
1014
char *dir = ".";
@@ -14,28 +18,19 @@ int main (int argc, char** argv)
1418

1519
git_threads_init();
1620

21+
if (argc > 2)
22+
fatal("usage: showindex [<repo-dir>]", NULL);
1723
if (argc > 1)
1824
dir = argv[1];
19-
if (!dir || argc > 2) {
20-
fprintf(stderr, "usage: showindex [<repo-dir>]\n");
21-
return 1;
22-
}
2325

2426
dirlen = strlen(dir);
2527
if (dirlen > 5 && strcmp(dir + dirlen - 5, "index") == 0) {
26-
if (git_index_open(&index, dir) < 0) {
27-
fprintf(stderr, "could not open index: %s\n", dir);
28-
return 1;
29-
}
28+
check_lg2(git_index_open(&index, dir), "could not open index", dir);
3029
} else {
31-
if (git_repository_open_ext(&repo, dir, 0, NULL) < 0) {
32-
fprintf(stderr, "could not open repository: %s\n", dir);
33-
return 1;
34-
}
35-
if (git_repository_index(&index, repo) < 0) {
36-
fprintf(stderr, "could not open repository index\n");
37-
return 1;
38-
}
30+
git_repository *repo;
31+
check_lg2(git_repository_open_ext(&repo, dir, 0, NULL), "could not open repository", dir);
32+
check_lg2(git_repository_index(&index, repo), "could not open repository index", NULL);
33+
git_repository_free(repo);
3934
}
4035

4136
git_index_read(index);
@@ -62,10 +57,7 @@ int main (int argc, char** argv)
6257
}
6358

6459
git_index_free(index);
65-
git_repository_free(repo);
66-
6760
git_threads_shutdown();
6861

6962
return 0;
7063
}
71-

0 commit comments

Comments
 (0)