git-verify-pack: get rid of while loop
[git] / verify-pack.c
1 #include "cache.h"
2 #include "pack.h"
3
4 static int verify_one_pack(const char *path, int verbose)
5 {
6         char arg[PATH_MAX];
7         int len;
8         struct packed_git *g;
9
10         len = strlcpy(arg, path, PATH_MAX);
11         if (len >= PATH_MAX)
12                 return error("name too long: %s", path);
13
14         /*
15          * In addition to "foo.idx" we accept "foo.pack" and "foo";
16          * normalize these forms to "foo.idx" for add_packed_git().
17          */
18         if (has_extension(arg, len, ".pack")) {
19                 strcpy(arg + len - 5, ".idx");
20                 len--;
21         } else if (!has_extension(arg, len, ".idx")) {
22                 if (len + 4 >= PATH_MAX)
23                         return error("name too long: %s.idx", arg);
24                 strcpy(arg + len, ".idx");
25                 len += 4;
26         }
27
28         if (!(g = add_packed_git(arg, len, 1)))
29                 return error("packfile %s not found.", arg);
30
31         return verify_pack(g, verbose);
32 }
33
34 static const char verify_pack_usage[] = "git-verify-pack [-v] <pack>...";
35
36 int main(int ac, char **av)
37 {
38         int errs = 0;
39         int verbose = 0;
40         int no_more_options = 0;
41         int nothing_done = 1;
42
43         while (1 < ac) {
44                 if (!no_more_options && av[1][0] == '-') {
45                         if (!strcmp("-v", av[1]))
46                                 verbose = 1;
47                         else if (!strcmp("--", av[1]))
48                                 no_more_options = 1;
49                         else
50                                 usage(verify_pack_usage);
51                 }
52                 else {
53                         if (verify_one_pack(av[1], verbose))
54                                 errs++;
55                         nothing_done = 0;
56                 }
57                 ac--; av++;
58         }
59
60         if (nothing_done)
61                 usage(verify_pack_usage);
62
63         return !!errs;
64 }