blob: c67bf65b347810c576f80a0ac33e3b84f42e1516 [file] [log] [blame]
David Reissae299be2008-05-19 23:48:54 -07001#include "cache.h"
Michael Haggerty31171d92012-10-28 17:16:24 +01002#include "string-list.h"
David Reissae299be2008-05-19 23:48:54 -07003
Michael Haggerty9e2326c2012-10-28 17:16:25 +01004/*
5 * A "string_list_each_func_t" function that normalizes an entry from
6 * GIT_CEILING_DIRECTORIES. If the path is unusable for some reason,
7 * die with an explanation.
8 */
9static int normalize_ceiling_entry(struct string_list_item *item, void *unused)
10{
11 const char *ceil = item->string;
12 int len = strlen(ceil);
13 char buf[PATH_MAX+1];
14
15 if (len == 0)
16 die("Empty path is not supported");
17 if (len > PATH_MAX)
18 die("Path \"%s\" is too long", ceil);
19 if (!is_absolute_path(ceil))
20 die("Path \"%s\" is not absolute", ceil);
21 if (normalize_path_copy(buf, ceil) < 0)
22 die("Path \"%s\" could not be normalized", ceil);
23 len = strlen(buf);
Michael Haggerty9e2326c2012-10-28 17:16:25 +010024 free(item->string);
25 item->string = xstrdup(buf);
26 return 1;
27}
28
Jiang Xin203439b2013-06-25 23:53:42 +080029static void normalize_argv_string(const char **var, const char *input)
30{
31 if (!strcmp(input, "<null>"))
32 *var = NULL;
33 else if (!strcmp(input, "<empty>"))
34 *var = "";
35 else
36 *var = input;
37
38 if (*var && (**var == '<' || **var == '('))
39 die("Bad value: %s\n", input);
40}
41
David Reissae299be2008-05-19 23:48:54 -070042int main(int argc, char **argv)
43{
Johannes Sixtf42302b2009-02-07 16:08:30 +010044 if (argc == 3 && !strcmp(argv[1], "normalize_path_copy")) {
Johannes Schindelinb8469ad2009-01-28 00:07:36 +010045 char *buf = xmalloc(PATH_MAX + 1);
Johannes Sixtf42302b2009-02-07 16:08:30 +010046 int rv = normalize_path_copy(buf, argv[2]);
47 if (rv)
48 buf = "++failed++";
David Reissae299be2008-05-19 23:48:54 -070049 puts(buf);
Johannes Sixt2cd85c42009-02-07 16:08:27 +010050 return 0;
David Reissae299be2008-05-19 23:48:54 -070051 }
52
Carlos Martín Nietoe2a57aa2011-03-17 12:26:46 +010053 if (argc >= 2 && !strcmp(argv[1], "real_path")) {
David Reissd553e732008-05-19 23:49:00 -070054 while (argc > 2) {
Carlos Martín Nietoe2a57aa2011-03-17 12:26:46 +010055 puts(real_path(argv[2]));
David Reissd553e732008-05-19 23:49:00 -070056 argc--;
57 argv++;
58 }
Johannes Sixt2cd85c42009-02-07 16:08:27 +010059 return 0;
David Reissd553e732008-05-19 23:49:00 -070060 }
61
Michael Haggerty87a246e2011-08-04 06:47:47 +020062 if (argc >= 2 && !strcmp(argv[1], "absolute_path")) {
63 while (argc > 2) {
64 puts(absolute_path(argv[2]));
65 argc--;
66 argv++;
67 }
68 return 0;
69 }
70
David Reiss0454dd92008-05-19 23:49:26 -070071 if (argc == 4 && !strcmp(argv[1], "longest_ancestor_length")) {
Michael Haggerty31171d92012-10-28 17:16:24 +010072 int len;
73 struct string_list ceiling_dirs = STRING_LIST_INIT_DUP;
Michael Haggerty9e2326c2012-10-28 17:16:25 +010074 char *path = xstrdup(argv[2]);
Michael Haggerty31171d92012-10-28 17:16:24 +010075
Michael Haggerty9e2326c2012-10-28 17:16:25 +010076 /*
77 * We have to normalize the arguments because under
78 * Windows, bash mangles arguments that look like
79 * absolute POSIX paths or colon-separate lists of
80 * absolute POSIX paths into DOS paths (e.g.,
81 * "/foo:/foo/bar" might be converted to
82 * "D:\Src\msysgit\foo;D:\Src\msysgit\foo\bar"),
83 * whereas longest_ancestor_length() requires paths
84 * that use forward slashes.
85 */
86 if (normalize_path_copy(path, path))
87 die("Path \"%s\" could not be normalized", argv[2]);
Michael Haggerty31171d92012-10-28 17:16:24 +010088 string_list_split(&ceiling_dirs, argv[3], PATH_SEP, -1);
Michael Haggerty9e2326c2012-10-28 17:16:25 +010089 filter_string_list(&ceiling_dirs, 0,
90 normalize_ceiling_entry, NULL);
91 len = longest_ancestor_length(path, &ceiling_dirs);
Michael Haggerty31171d92012-10-28 17:16:24 +010092 string_list_clear(&ceiling_dirs, 0);
Michael Haggerty9e2326c2012-10-28 17:16:25 +010093 free(path);
David Reiss0454dd92008-05-19 23:49:26 -070094 printf("%d\n", len);
Johannes Sixt2cd85c42009-02-07 16:08:27 +010095 return 0;
David Reiss0454dd92008-05-19 23:49:26 -070096 }
97
Michael Haggerty9e813722011-08-04 06:47:48 +020098 if (argc >= 4 && !strcmp(argv[1], "prefix_path")) {
99 char *prefix = argv[2];
100 int prefix_len = strlen(prefix);
101 int nongit_ok;
102 setup_git_directory_gently(&nongit_ok);
103 while (argc > 3) {
104 puts(prefix_path(prefix, prefix_len, argv[3]));
105 argc--;
106 argv++;
107 }
108 return 0;
109 }
110
Johannes Schindelin4fcc86b2009-02-19 20:10:49 +0100111 if (argc == 4 && !strcmp(argv[1], "strip_path_suffix")) {
112 char *prefix = strip_path_suffix(argv[2], argv[3]);
113 printf("%s\n", prefix ? prefix : "(null)");
114 return 0;
115 }
116
Sebastian Schuberth7ffd18f2013-10-10 22:49:43 +0200117 if (argc == 3 && !strcmp(argv[1], "print_path")) {
Jiang Xinabd42842013-06-25 23:53:57 +0800118 puts(argv[2]);
119 return 0;
120 }
121
Jiang Xin203439b2013-06-25 23:53:42 +0800122 if (argc == 4 && !strcmp(argv[1], "relative_path")) {
Jiang Xine02ca722013-06-25 23:53:43 +0800123 struct strbuf sb = STRBUF_INIT;
Jiang Xin203439b2013-06-25 23:53:42 +0800124 const char *in, *prefix, *rel;
125 normalize_argv_string(&in, argv[2]);
126 normalize_argv_string(&prefix, argv[3]);
Jiang Xine02ca722013-06-25 23:53:43 +0800127 rel = relative_path(in, prefix, &sb);
Jiang Xin203439b2013-06-25 23:53:42 +0800128 if (!rel)
129 puts("(null)");
130 else
131 puts(strlen(rel) > 0 ? rel : "(empty)");
Jiang Xine02ca722013-06-25 23:53:43 +0800132 strbuf_release(&sb);
Jiang Xin203439b2013-06-25 23:53:42 +0800133 return 0;
134 }
135
Johannes Sixt2cd85c42009-02-07 16:08:27 +0100136 fprintf(stderr, "%s: unknown function name: %s\n", argv[0],
137 argv[1] ? argv[1] : "(there was none)");
138 return 1;
David Reissae299be2008-05-19 23:48:54 -0700139}