11 # define PATH_MAX 4096
14 static const char git_usage
[] =
15 "Usage: git [--version] [--exec-path[=GIT_EXEC_PATH]] [--help] COMMAND [ ARGS ]";
20 struct string_list
*next
;
23 /* most gui terms set COLUMNS (although some don't export it) */
24 static int term_columns(void)
26 char *col_string
= getenv("COLUMNS");
29 if (col_string
&& (n_cols
= atoi(col_string
)) > 0)
35 static inline void mput_char(char c
, unsigned int num
)
41 static void pretty_print_string_list(struct string_list
*list
, int longest
)
44 int space
= longest
+ 1; /* min 1 SP between words */
45 int max_cols
= term_columns() - 1; /* don't print *on* the edge */
48 cols
= max_cols
/ space
;
54 for (c
= cols
; c
&& list
; list
= list
->next
) {
55 printf("%s", list
->str
);
58 mput_char(' ', space
- list
->len
);
64 static void list_commands(const char *exec_path
, const char *pattern
)
66 struct string_list
*list
= NULL
, *tail
= NULL
;
67 unsigned int longest
= 0, i
;
70 if (chdir(exec_path
) < 0) {
71 printf("git: '%s': %s\n", exec_path
, strerror(errno
));
75 i
= glob(pattern
, 0, NULL
, &gl
);
78 puts("Out of memory when running glob()");
81 printf("'%s': Read error: %s\n", exec_path
, strerror(errno
));
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");
89 for (i
= 0; i
< gl
.gl_pathc
; i
++) {
90 int len
= strlen(gl
.gl_pathv
[i
] + 4);
92 if (access(gl
.gl_pathv
[i
], X_OK
))
99 tail
= list
= malloc(sizeof(struct string_list
));
101 tail
->next
= malloc(sizeof(struct string_list
));
105 tail
->str
= gl
.gl_pathv
[i
] + 4;
109 printf("git commands available in '%s'\n", exec_path
);
110 printf("----------------------------");
111 mput_char('-', strlen(exec_path
));
113 pretty_print_string_list(list
, longest
);
118 static void usage(const char *exec_path
, const char *fmt
, ...)
119 __attribute__((__format__(__printf__
, 2, 3), __noreturn__
));
121 static void usage(const char *exec_path
, const char *fmt
, ...)
138 list_commands(exec_path
, "git-*");
143 static void prepend_to_path(const char *dir
, int len
)
145 char *path
, *old_path
= getenv("PATH");
149 old_path
= "/bin:/usr/bin:.";
151 path_len
= len
+ strlen(old_path
) + 1;
153 path
= malloc(path_len
+ 1);
154 path
[path_len
+ 1] = '\0';
156 memcpy(path
, dir
, len
);
158 memcpy(path
+ len
+ 1, old_path
, path_len
- len
);
160 setenv("PATH", path
, 1);
163 /* has anyone seen 'man' installed anywhere else than in /usr/bin? */
164 #define PATH_TO_MAN "/usr/bin/man"
165 static void show_man_page(char *git_cmd
)
169 if (!strncmp(git_cmd
, "git", 3))
172 int page_len
= strlen(git_cmd
) + 4;
174 page
= malloc(page_len
+ 1);
175 strcpy(page
, "git-");
176 strcpy(page
+ 4, git_cmd
);
180 execlp(PATH_TO_MAN
, "man", page
, NULL
);
183 int main(int argc
, char **argv
, char **envp
)
185 char git_command
[PATH_MAX
+ 1];
186 char wd
[PATH_MAX
+ 1];
187 int i
, len
, show_help
= 0;
188 char *exec_path
= getenv("GIT_EXEC_PATH");
190 getcwd(wd
, PATH_MAX
);
193 exec_path
= GIT_EXEC_PATH
;
195 for (i
= 1; i
< argc
; i
++) {
198 if (strncmp(arg
, "--", 2))
203 if (!strncmp(arg
, "exec-path", 9)) {
212 else if (!strcmp(arg
, "version")) {
213 printf("git version %s\n", GIT_VERSION
);
216 else if (!strcmp(arg
, "help"))
222 if (i
>= argc
|| show_help
) {
224 usage(exec_path
, NULL
);
226 show_man_page(argv
[i
]);
229 /* allow relative paths, but run with exact */
230 if (chdir(exec_path
)) {
231 printf("git: '%s': %s\n", exec_path
, strerror(errno
));
235 getcwd(git_command
, sizeof(git_command
));
238 len
= strlen(git_command
);
239 prepend_to_path(git_command
, len
);
241 strncat(&git_command
[len
], "/git-", sizeof(git_command
) - len
);
243 strncat(&git_command
[len
], argv
[i
], sizeof(git_command
) - len
);
245 if (access(git_command
, X_OK
))
246 usage(exec_path
, "'%s' is not a git-command", argv
[i
]);
248 /* execve() can only ever return if it fails */
249 execve(git_command
, &argv
[i
], envp
);
250 printf("Failed to run command '%s': %s\n", git_command
, strerror(errno
));