@matheusbsbs / matheus-bagliano / commits / 7dbc2c0402
Fork 0

git wrapper: basic fixes.

Updates to fix the nits found during the list discussion. - Lose PATH_TO_MAN; just rely on execlp() to find whereever the "man" command is installed. - Do not randomly chdir(), but concatenate to the current working directory only if the given path is not absolute. - Lose use of glob(); read from exec_path and do sorting ourselves -- it is not that much more work. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Nov 15, 2005 at 23:13 UTC 7dbc2c0402d728a206d4f1bc59729bf3a5cc4455
1 file changed +98 -56
git.c
+98 -56
@@ -1,11 +1,13 @@
1 #include <stdio.h>
2 +#include <sys/types.h>
3 +#include <sys/stat.h>
4 +#include <dirent.h>
5 #include <unistd.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <errno.h>
9 #include <limits.h>
10 #include <stdarg.h>
8 -#include <glob.h>
11
12 #ifndef PATH_MAX
13 # define PATH_MAX 4096
@@ -14,12 +16,6 @@
16 static const char git_usage[] =
17 "Usage: git [--version] [--exec-path[=GIT_EXEC_PATH]] [--help] COMMAND [ ARGS ]";
18
17 -struct string_list {
18 - size_t len;
19 - char *str;
20 - struct string_list *next;
21 -};
22 -
19 /* most gui terms set COLUMNS (although some don't export it) */
20 static int term_columns(void)
21 {
@@ -32,30 +28,69 @@ static int term_columns(void)
28 return 80;
29 }
30
31 +static void oom(void)
32 +{
33 + fprintf(stderr, "git: out of memory\n");
34 + exit(1);
35 +}
36 +
37 static inline void mput_char(char c, unsigned int num)
38 {
39 while(num--)
40 putchar(c);
41 }
42
41 -static void pretty_print_string_list(struct string_list *list, int longest)
43 +static struct cmdname {
44 + size_t len;
45 + char name[1];
46 +} **cmdname;
47 +static int cmdname_alloc, cmdname_cnt;
48 +
49 +static void add_cmdname(const char *name, int len)
50 +{
51 + struct cmdname *ent;
52 + if (cmdname_alloc <= cmdname_cnt) {
53 + cmdname_alloc = cmdname_alloc + 200;
54 + cmdname = realloc(cmdname, cmdname_alloc * sizeof(*cmdname));
55 + if (!cmdname)
56 + oom();
57 + }
58 + ent = malloc(sizeof(*ent) + len);
59 + if (!ent)
60 + oom();
61 + ent->len = len;
62 + memcpy(ent->name, name, len+1);
63 + cmdname[cmdname_cnt++] = ent;
64 +}
65 +
66 +static int cmdname_compare(const void *a_, const void *b_)
67 +{
68 + struct cmdname *a = *(struct cmdname **)a_;
69 + struct cmdname *b = *(struct cmdname **)b_;
70 + return strcmp(a->name, b->name);
71 +}
72 +
73 +static void pretty_print_string_list(struct cmdname **cmdname, int longest)
74 {
75 int cols = 1;
76 int space = longest + 1; /* min 1 SP between words */
77 int max_cols = term_columns() - 1; /* don't print *on* the edge */
78 + int i;
79
80 if (space < max_cols)
81 cols = max_cols / space;
82
50 - while (list) {
83 + qsort(cmdname, cmdname_cnt, sizeof(*cmdname), cmdname_compare);
84 +
85 + for (i = 0; i < cmdname_cnt; ) {
86 int c;
87 printf(" ");
88
54 - for (c = cols; c && list; list = list->next) {
55 - printf("%s", list->str);
89 + for (c = cols; c && i < cmdname_cnt; i++) {
90 + printf("%s", cmdname[i]->name);
91
92 if (--c)
58 - mput_char(' ', space - list->len);
93 + mput_char(' ', space - cmdname[i]->len);
94 }
95 putchar('\n');
96 }
@@ -63,54 +98,53 @@ static void pretty_print_string_list(struct string_list *list, int longest)
98
99 static void list_commands(const char *exec_path, const char *pattern)
100 {
66 - struct string_list *list = NULL, *tail = NULL;
67 - unsigned int longest = 0, i;
68 - glob_t gl;
69 -
70 - if (chdir(exec_path) < 0) {
71 - printf("git: '%s': %s\n", exec_path, strerror(errno));
101 + unsigned int longest = 0;
102 + char path[PATH_MAX];
103 + int dirlen;
104 + DIR *dir = opendir(exec_path);
105 + struct dirent *de;
106 +
107 + if (!dir) {
108 + fprintf(stderr, "git: '%s': %s\n", exec_path, strerror(errno));
109 exit(1);
110 }
111
75 - i = glob(pattern, 0, NULL, &gl);
76 - switch(i) {
77 - case GLOB_NOSPACE:
78 - puts("Out of memory when running glob()");
79 - exit(2);
80 - case GLOB_ABORTED:
81 - printf("'%s': Read error: %s\n", exec_path, strerror(errno));
82 - exit(2);
83 - case GLOB_NOMATCH:
84 - printf("No git commands available in '%s'.\n", exec_path);
85 - printf("Do you need to specify --exec-path or set GIT_EXEC_PATH?\n");
112 + dirlen = strlen(exec_path);
113 + if (PATH_MAX - 20 < dirlen) {
114 + fprintf(stderr, "git: insanely long exec-path '%s'\n",
115 + exec_path);
116 exit(1);
117 }
118
89 - for (i = 0; i < gl.gl_pathc; i++) {
90 - int len = strlen(gl.gl_pathv[i] + 4);
119 + memcpy(path, exec_path, dirlen);
120 + path[dirlen++] = '/';
121 +
122 + while ((de = readdir(dir)) != NULL) {
123 + struct stat st;
124 + int entlen;
125
92 - if (access(gl.gl_pathv[i], X_OK))
126 + if (strncmp(de->d_name, "git-", 4))
127 + continue;
128 + strcpy(path+dirlen, de->d_name);
129 + if (stat(path, &st) || /* stat, not lstat */
130 + !S_ISREG(st.st_mode) ||
131 + !(st.st_mode & S_IXUSR))
132 continue;
133
95 - if (longest < len)
96 - longest = len;
134 + entlen = strlen(de->d_name);
135
98 - if (!tail)
99 - tail = list = malloc(sizeof(struct string_list));
100 - else {
101 - tail->next = malloc(sizeof(struct string_list));
102 - tail = tail->next;
103 - }
104 - tail->len = len;
105 - tail->str = gl.gl_pathv[i] + 4;
106 - tail->next = NULL;
136 + if (longest < entlen)
137 + longest = entlen;
138 +
139 + add_cmdname(de->d_name + 4, entlen-4);
140 }
141 + closedir(dir);
142
143 printf("git commands available in '%s'\n", exec_path);
144 printf("----------------------------");
145 mput_char('-', strlen(exec_path));
146 putchar('\n');
113 - pretty_print_string_list(list, longest);
147 + pretty_print_string_list(cmdname, longest - 4);
148 putchar('\n');
149 }
150
@@ -146,7 +180,7 @@ static void prepend_to_path(const char *dir, int len)
180 int path_len = len;
181
182 if (!old_path)
149 - old_path = "/bin:/usr/bin:.";
183 + old_path = "/usr/local/bin:/usr/bin:/bin";
184
185 path_len = len + strlen(old_path) + 1;
186
@@ -160,8 +194,6 @@ static void prepend_to_path(const char *dir, int len)
194 setenv("PATH", path, 1);
195 }
196
163 -/* has anyone seen 'man' installed anywhere else than in /usr/bin? */
164 -#define PATH_TO_MAN "/usr/bin/man"
197 static void show_man_page(char *git_cmd)
198 {
199 char *page;
@@ -177,7 +209,7 @@ static void show_man_page(char *git_cmd)
209 page[page_len] = 0;
210 }
211
180 - execlp(PATH_TO_MAN, "man", page, NULL);
212 + execlp("man", "man", page, NULL);
213 }
214
215 int main(int argc, char **argv, char **envp)
@@ -226,15 +258,25 @@ int main(int argc, char **argv, char **envp)
258 show_man_page(argv[i]);
259 }
260
229 - /* allow relative paths, but run with exact */
230 - if (chdir(exec_path)) {
231 - printf("git: '%s': %s\n", exec_path, strerror(errno));
232 - exit (1);
233 - }
234 -
235 - getcwd(git_command, sizeof(git_command));
236 - chdir(wd);
261 + if (*exec_path != '/') {
262 + if (!getcwd(git_command, sizeof(git_command))) {
263 + fprintf(stderr,
264 + "git: cannot determine current directory");
265 + exit(1);
266 + }
267 + len = strlen(git_command);
268
269 + /* Trivial cleanup */
270 + while (!strncmp(exec_path, "./", 2)) {
271 + exec_path += 2;
272 + while (*exec_path == '/')
273 + *exec_path++;
274 + }
275 + snprintf(git_command + len, sizeof(git_command) - len,
276 + "/%s", exec_path);
277 + }
278 + else
279 + strcpy(git_command, exec_path);
280 len = strlen(git_command);
281 prepend_to_path(git_command, len);
282