11 #include "git-compat-util.h"
17 static void prepend_to_path(const char *dir
, int len
)
19 char *path
, *old_path
= getenv("PATH");
23 old_path
= "/usr/local/bin:/usr/bin:/bin";
25 path_len
= len
+ strlen(old_path
) + 1;
27 path
= malloc(path_len
+ 1);
29 memcpy(path
, dir
, len
);
31 memcpy(path
+ len
+ 1, old_path
, path_len
- len
);
33 setenv("PATH", path
, 1);
36 static const char *alias_command
;
37 static char *alias_string
= NULL
;
39 static int git_alias_config(const char *var
, const char *value
)
41 if (!strncmp(var
, "alias.", 6) && !strcmp(var
+ 6, alias_command
)) {
42 alias_string
= strdup(value
);
47 static int split_cmdline(char *cmdline
, const char ***argv
)
49 int src
, dst
, count
= 0, size
= 16;
52 *argv
= malloc(sizeof(char*) * size
);
54 /* split alias_string */
55 (*argv
)[count
++] = cmdline
;
56 for (src
= dst
= 0; cmdline
[src
];) {
57 char c
= cmdline
[src
];
58 if (!quoted
&& isspace(c
)) {
61 && isspace(cmdline
[src
]))
65 *argv
= realloc(*argv
, sizeof(char*) * size
);
67 (*argv
)[count
++] = cmdline
+ dst
;
68 } else if(!quoted
&& (c
== '\'' || c
== '"')) {
71 } else if (c
== quoted
) {
75 if (c
== '\\' && quoted
!= '\'') {
81 return error("cmdline ends with \\");
94 return error("unclosed quote");
100 static int handle_alias(int *argcp
, const char ***argv
)
102 int nongit
= 0, ret
= 0;
105 subdir
= setup_git_directory_gently(&nongit
);
108 const char** new_argv
;
110 alias_command
= (*argv
)[0];
111 git_config(git_alias_config
);
114 count
= split_cmdline(alias_string
, &new_argv
);
117 die("empty alias for %s", alias_command
);
119 if (!strcmp(alias_command
, new_argv
[0]))
120 die("recursive alias: %s", alias_command
);
122 /* insert after command name */
124 new_argv
= realloc(new_argv
, sizeof(char*) *
125 (count
+ *argcp
- 1));
126 memcpy(new_argv
+ count
, *argv
, sizeof(char*) *
143 const char git_version_string
[] = GIT_VERSION
;
145 static void handle_internal_command(int argc
, const char **argv
, char **envp
)
147 const char *cmd
= argv
[0];
148 static struct cmd_struct
{
150 int (*fn
)(int, const char **, char **);
152 { "version", cmd_version
},
153 { "help", cmd_help
},
155 { "whatchanged", cmd_whatchanged
},
156 { "show", cmd_show
},
157 { "push", cmd_push
},
158 { "format-patch", cmd_format_patch
},
159 { "count-objects", cmd_count_objects
},
160 { "diff", cmd_diff
},
161 { "grep", cmd_grep
},
164 { "rev-list", cmd_rev_list
},
165 { "init-db", cmd_init_db
},
166 { "tar-tree", cmd_tar_tree
},
167 { "upload-tar", cmd_upload_tar
},
168 { "check-ref-format", cmd_check_ref_format
},
169 { "ls-files", cmd_ls_files
},
170 { "ls-tree", cmd_ls_tree
},
171 { "tar-tree", cmd_tar_tree
},
172 { "read-tree", cmd_read_tree
},
173 { "commit-tree", cmd_commit_tree
},
174 { "apply", cmd_apply
},
175 { "show-branch", cmd_show_branch
},
176 { "diff-files", cmd_diff_files
},
177 { "diff-index", cmd_diff_index
},
178 { "diff-stages", cmd_diff_stages
},
179 { "diff-tree", cmd_diff_tree
},
180 { "cat-file", cmd_cat_file
},
181 { "rev-parse", cmd_rev_parse
}
185 /* Turn "git cmd --help" into "git help cmd" */
186 if (argc
> 1 && !strcmp(argv
[1], "--help")) {
188 argv
[0] = cmd
= "help";
191 for (i
= 0; i
< ARRAY_SIZE(commands
); i
++) {
192 struct cmd_struct
*p
= commands
+i
;
193 if (strcmp(p
->cmd
, cmd
))
195 exit(p
->fn(argc
, argv
, envp
));
199 int main(int argc
, const char **argv
, char **envp
)
201 const char *cmd
= argv
[0];
202 char *slash
= strrchr(cmd
, '/');
203 char git_command
[PATH_MAX
+ 1];
204 const char *exec_path
= NULL
;
208 * Take the basename of argv[0] as the command
209 * name, and the dirname as the default exec_path
210 * if it's an absolute path and we don't have
221 * "git-xxxx" is the same as "git xxxx", but we obviously:
223 * - cannot take flags in between the "git" and the "xxxx".
224 * - cannot execute it externally (since it would just do
225 * the same thing over again)
227 * So we just directly call the internal command handler, and
228 * die if that one cannot handle it.
230 if (!strncmp(cmd
, "git-", 4)) {
233 handle_internal_command(argc
, argv
, envp
);
234 die("cannot handle %s internally", cmd
);
237 /* Default command: "help" */
240 /* Look for flags.. */
245 if (strncmp(cmd
, "--", 2))
251 * For legacy reasons, the "version" and "help"
252 * commands can be written with "--" prepended
253 * to make them look like flags.
255 if (!strcmp(cmd
, "help"))
257 if (!strcmp(cmd
, "version"))
261 * Check remaining flags (which by now must be
262 * "--exec-path", but maybe we will accept
263 * other arguments some day)
265 if (!strncmp(cmd
, "exec-path", 9)) {
268 git_set_exec_path(cmd
+ 1);
271 puts(git_exec_path());
274 cmd_usage(0, NULL
, NULL
);
279 * We search for git commands in the following order:
281 * - the path of the "git" command if we could find it
283 * - the regular PATH.
286 prepend_to_path(exec_path
, strlen(exec_path
));
287 exec_path
= git_exec_path();
288 prepend_to_path(exec_path
, strlen(exec_path
));
291 /* See if it's an internal command */
292 handle_internal_command(argc
, argv
, envp
);
294 /* .. then try the external ones */
297 /* It could be an alias -- this works around the insanity
298 * of overriding "git log" with "git show" by having
301 if (done_alias
|| !handle_alias(&argc
, &argv
))
307 cmd_usage(0, exec_path
, "'%s' is not a git-command", cmd
);
309 fprintf(stderr
, "Failed to run command '%s': %s\n",
310 git_command
, strerror(errno
));