Skip to content

Commit 231af83

Browse files
Linus TorvaldsJunio C Hamano
authored andcommitted
Teach the "git" command to handle some commands internally
This is another patch in the "prepare to do more in C" series, where the git wrapper command is taught about the notion of handling some functionality internally. Right now, the only internal commands are "version" and "help", but the point being that we can now easily extend it to handle some of the trivial scripts internally. Things like "git log" and "git diff" wouldn't need separate external scripts any more. This also implies that to support the old "git-log" and "git-diff" syntax, the "git" wrapper now automatically looks at the name it was executed as, and if it is "git-xxxx", it will assume that it is to internally do what "git xxxx" would do. In other words, you can (once you implement an internal command) soft- or hard-link that command to the "git" wrapper command, and it will do the right thing, whether you use the "git xxxx" or the "git-xxxx" format. There's one other change: the search order for external programs is modified slightly, so that the first entry remains GIT_EXEC_DIR, but the second entry is the same directory as the git wrapper itself was executed out of - if we can figure it out from argv[0], of course. Signed-off-by: Linus Torvalds <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 962554c commit 231af83

File tree

1 file changed

+116
-37
lines changed

1 file changed

+116
-37
lines changed

git.c

Lines changed: 116 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -230,62 +230,141 @@ static void show_man_page(char *git_cmd)
230230
execlp("man", "man", page, NULL);
231231
}
232232

233+
static int cmd_version(int argc, char **argv, char **envp)
234+
{
235+
printf("git version %s\n", GIT_VERSION);
236+
return 0;
237+
}
238+
239+
static int cmd_help(int argc, char **argv, char **envp)
240+
{
241+
char *help_cmd = argv[1];
242+
if (!help_cmd)
243+
cmd_usage(git_exec_path(), NULL);
244+
show_man_page(help_cmd);
245+
return 0;
246+
}
247+
248+
#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
249+
250+
static void handle_internal_command(int argc, char **argv, char **envp)
251+
{
252+
const char *cmd = argv[0];
253+
static struct cmd_struct {
254+
const char *cmd;
255+
int (*fn)(int, char **, char **);
256+
} commands[] = {
257+
{ "version", cmd_version },
258+
{ "help", cmd_help },
259+
};
260+
int i;
261+
262+
for (i = 0; i < ARRAY_SIZE(commands); i++) {
263+
struct cmd_struct *p = commands+i;
264+
if (strcmp(p->cmd, cmd))
265+
continue;
266+
exit(p->fn(argc, argv, envp));
267+
}
268+
}
269+
233270
int main(int argc, char **argv, char **envp)
234271
{
272+
char *cmd = argv[0];
273+
char *slash = strrchr(cmd, '/');
235274
char git_command[PATH_MAX + 1];
236-
char wd[PATH_MAX + 1];
237-
int i, show_help = 0;
238-
const char *exec_path;
275+
const char *exec_path = NULL;
276+
277+
/*
278+
* Take the basename of argv[0] as the command
279+
* name, and the dirname as the default exec_path
280+
* if it's an absolute path and we don't have
281+
* anything better.
282+
*/
283+
if (slash) {
284+
*slash++ = 0;
285+
if (*cmd == '/')
286+
exec_path = cmd;
287+
cmd = slash;
288+
}
239289

240-
getcwd(wd, PATH_MAX);
290+
/*
291+
* "git-xxxx" is the same as "git xxxx", but we obviously:
292+
*
293+
* - cannot take flags in between the "git" and the "xxxx".
294+
* - cannot execute it externally (since it would just do
295+
* the same thing over again)
296+
*
297+
* So we just directly call the internal command handler, and
298+
* die if that one cannot handle it.
299+
*/
300+
if (!strncmp(cmd, "git-", 4)) {
301+
cmd += 4;
302+
argv[0] = cmd;
303+
handle_internal_command(argc, argv, envp);
304+
die("cannot handle %s internally", cmd);
305+
}
241306

242-
for (i = 1; i < argc; i++) {
243-
char *arg = argv[i];
307+
/* Default command: "help" */
308+
cmd = "help";
244309

245-
if (!strcmp(arg, "help")) {
246-
show_help = 1;
247-
continue;
248-
}
310+
/* Look for flags.. */
311+
while (argc > 1) {
312+
cmd = *++argv;
313+
argc--;
249314

250-
if (strncmp(arg, "--", 2))
315+
if (strncmp(cmd, "--", 2))
251316
break;
252317

253-
arg += 2;
318+
cmd += 2;
319+
320+
/*
321+
* For legacy reasons, the "version" and "help"
322+
* commands can be written with "--" prepended
323+
* to make them look like flags.
324+
*/
325+
if (!strcmp(cmd, "help"))
326+
break;
327+
if (!strcmp(cmd, "version"))
328+
break;
254329

255-
if (!strncmp(arg, "exec-path", 9)) {
256-
arg += 9;
257-
if (*arg == '=') {
258-
exec_path = arg + 1;
259-
git_set_exec_path(exec_path);
260-
} else {
261-
puts(git_exec_path());
262-
exit(0);
330+
/*
331+
* Check remaining flags (which by now must be
332+
* "--exec-path", but maybe we will accept
333+
* other arguments some day)
334+
*/
335+
if (!strncmp(cmd, "exec-path", 9)) {
336+
cmd += 9;
337+
if (*cmd == '=') {
338+
git_set_exec_path(cmd + 1);
339+
continue;
263340
}
264-
}
265-
else if (!strcmp(arg, "version")) {
266-
printf("git version %s\n", GIT_VERSION);
341+
puts(git_exec_path());
267342
exit(0);
268343
}
269-
else if (!strcmp(arg, "help"))
270-
show_help = 1;
271-
else if (!show_help)
272-
cmd_usage(NULL, NULL);
273-
}
274-
275-
if (i >= argc || show_help) {
276-
if (i >= argc)
277-
cmd_usage(git_exec_path(), NULL);
278-
279-
show_man_page(argv[i]);
344+
cmd_usage(NULL, NULL);
280345
}
281-
346+
argv[0] = cmd;
347+
348+
/*
349+
* We search for git commands in the following order:
350+
* - git_exec_path()
351+
* - the path of the "git" command if we could find it
352+
* in $0
353+
* - the regular PATH.
354+
*/
355+
if (exec_path)
356+
prepend_to_path(exec_path, strlen(exec_path));
282357
exec_path = git_exec_path();
283358
prepend_to_path(exec_path, strlen(exec_path));
284359

285-
execv_git_cmd(argv + i);
360+
/* See if it's an internal command */
361+
handle_internal_command(argc, argv, envp);
362+
363+
/* .. then try the external ones */
364+
execv_git_cmd(argv);
286365

287366
if (errno == ENOENT)
288-
cmd_usage(exec_path, "'%s' is not a git-command", argv[i]);
367+
cmd_usage(exec_path, "'%s' is not a git-command", cmd);
289368

290369
fprintf(stderr, "Failed to run command '%s': %s\n",
291370
git_command, strerror(errno));

0 commit comments

Comments
 (0)