longest_ancestor_length(): take a string_list argument for prefixes
[git] / test-path-utils.c
1 #include "cache.h"
2 #include "string-list.h"
3
4 int main(int argc, char **argv)
5 {
6         if (argc == 3 && !strcmp(argv[1], "normalize_path_copy")) {
7                 char *buf = xmalloc(PATH_MAX + 1);
8                 int rv = normalize_path_copy(buf, argv[2]);
9                 if (rv)
10                         buf = "++failed++";
11                 puts(buf);
12                 return 0;
13         }
14
15         if (argc >= 2 && !strcmp(argv[1], "real_path")) {
16                 while (argc > 2) {
17                         puts(real_path(argv[2]));
18                         argc--;
19                         argv++;
20                 }
21                 return 0;
22         }
23
24         if (argc >= 2 && !strcmp(argv[1], "absolute_path")) {
25                 while (argc > 2) {
26                         puts(absolute_path(argv[2]));
27                         argc--;
28                         argv++;
29                 }
30                 return 0;
31         }
32
33         if (argc == 4 && !strcmp(argv[1], "longest_ancestor_length")) {
34                 int len;
35                 struct string_list ceiling_dirs = STRING_LIST_INIT_DUP;
36
37                 string_list_split(&ceiling_dirs, argv[3], PATH_SEP, -1);
38                 len = longest_ancestor_length(argv[2], &ceiling_dirs);
39                 string_list_clear(&ceiling_dirs, 0);
40                 printf("%d\n", len);
41                 return 0;
42         }
43
44         if (argc >= 4 && !strcmp(argv[1], "prefix_path")) {
45                 char *prefix = argv[2];
46                 int prefix_len = strlen(prefix);
47                 int nongit_ok;
48                 setup_git_directory_gently(&nongit_ok);
49                 while (argc > 3) {
50                         puts(prefix_path(prefix, prefix_len, argv[3]));
51                         argc--;
52                         argv++;
53                 }
54                 return 0;
55         }
56
57         if (argc == 4 && !strcmp(argv[1], "strip_path_suffix")) {
58                 char *prefix = strip_path_suffix(argv[2], argv[3]);
59                 printf("%s\n", prefix ? prefix : "(null)");
60                 return 0;
61         }
62
63         fprintf(stderr, "%s: unknown function name: %s\n", argv[0],
64                 argv[1] ? argv[1] : "(there was none)");
65         return 1;
66 }