11 #include "git-compat-util.h"
18 static void prepend_to_path(const char *dir
, int len
)
20 const char *old_path
= getenv("PATH");
25 old_path
= "/usr/local/bin:/usr/bin:/bin";
27 path_len
= len
+ strlen(old_path
) + 1;
29 path
= malloc(path_len
+ 1);
31 memcpy(path
, dir
, len
);
33 memcpy(path
+ len
+ 1, old_path
, path_len
- len
);
35 setenv("PATH", path
, 1);
38 static int handle_options(const char*** argv
, int* argc
)
43 const char *cmd
= (*argv
)[0];
47 if (!strcmp(cmd
, "-p") || !strcmp(cmd
, "--paginate")) {
50 die ("Unknown option: %s", cmd
);
59 static const char *alias_command
;
60 static char *alias_string
= NULL
;
62 static int git_alias_config(const char *var
, const char *value
)
64 if (!strncmp(var
, "alias.", 6) && !strcmp(var
+ 6, alias_command
)) {
65 alias_string
= strdup(value
);
70 static int split_cmdline(char *cmdline
, const char ***argv
)
72 int src
, dst
, count
= 0, size
= 16;
75 *argv
= malloc(sizeof(char*) * size
);
77 /* split alias_string */
78 (*argv
)[count
++] = cmdline
;
79 for (src
= dst
= 0; cmdline
[src
];) {
80 char c
= cmdline
[src
];
81 if (!quoted
&& isspace(c
)) {
84 && isspace(cmdline
[src
]))
88 *argv
= realloc(*argv
, sizeof(char*) * size
);
90 (*argv
)[count
++] = cmdline
+ dst
;
91 } else if(!quoted
&& (c
== '\'' || c
== '"')) {
94 } else if (c
== quoted
) {
98 if (c
== '\\' && quoted
!= '\'') {
104 return error("cmdline ends with \\");
117 return error("unclosed quote");
123 static int handle_alias(int *argcp
, const char ***argv
)
125 int nongit
= 0, ret
= 0, saved_errno
= errno
;
128 subdir
= setup_git_directory_gently(&nongit
);
130 int count
, option_count
;
131 const char** new_argv
;
133 alias_command
= (*argv
)[0];
134 git_config(git_alias_config
);
137 count
= split_cmdline(alias_string
, &new_argv
);
138 option_count
= handle_options(&new_argv
, &count
);
139 memmove(new_argv
- option_count
, new_argv
,
140 count
* sizeof(char *));
141 new_argv
-= option_count
;
144 die("empty alias for %s", alias_command
);
146 if (!strcmp(alias_command
, new_argv
[0]))
147 die("recursive alias: %s", alias_command
);
149 if (getenv("GIT_TRACE")) {
151 fprintf(stderr
, "trace: alias expansion: %s =>",
153 for (i
= 0; i
< count
; ++i
) {
155 sq_quote_print(stderr
, new_argv
[i
]);
161 new_argv
= realloc(new_argv
, sizeof(char*) *
162 (count
+ *argcp
+ 1));
163 /* insert after command name */
164 memcpy(new_argv
+ count
, *argv
+ 1,
165 sizeof(char*) * *argcp
);
166 new_argv
[count
+*argcp
] = NULL
;
183 const char git_version_string
[] = GIT_VERSION
;
185 static void handle_internal_command(int argc
, const char **argv
, char **envp
)
187 const char *cmd
= argv
[0];
188 static struct cmd_struct
{
190 int (*fn
)(int, const char **, char **);
192 { "version", cmd_version
},
193 { "help", cmd_help
},
195 { "whatchanged", cmd_whatchanged
},
196 { "show", cmd_show
},
197 { "push", cmd_push
},
198 { "format-patch", cmd_format_patch
},
199 { "count-objects", cmd_count_objects
},
200 { "diff", cmd_diff
},
201 { "grep", cmd_grep
},
204 { "rev-list", cmd_rev_list
},
205 { "init-db", cmd_init_db
},
206 { "get-tar-commit-id", cmd_get_tar_commit_id
},
207 { "upload-tar", cmd_upload_tar
},
208 { "check-ref-format", cmd_check_ref_format
},
209 { "ls-files", cmd_ls_files
},
210 { "ls-tree", cmd_ls_tree
},
211 { "tar-tree", cmd_tar_tree
},
212 { "read-tree", cmd_read_tree
},
213 { "commit-tree", cmd_commit_tree
},
214 { "apply", cmd_apply
},
215 { "show-branch", cmd_show_branch
},
216 { "diff-files", cmd_diff_files
},
217 { "diff-index", cmd_diff_index
},
218 { "diff-stages", cmd_diff_stages
},
219 { "diff-tree", cmd_diff_tree
},
220 { "cat-file", cmd_cat_file
},
221 { "rev-parse", cmd_rev_parse
},
222 { "write-tree", cmd_write_tree
},
223 { "mailsplit", cmd_mailsplit
},
224 { "mailinfo", cmd_mailinfo
},
225 { "stripspace", cmd_stripspace
},
226 { "update-index", cmd_update_index
},
227 { "update-ref", cmd_update_ref
},
228 { "fmt-merge-msg", cmd_fmt_merge_msg
},
229 { "prune", cmd_prune
},
233 /* Turn "git cmd --help" into "git help cmd" */
234 if (argc
> 1 && !strcmp(argv
[1], "--help")) {
236 argv
[0] = cmd
= "help";
239 for (i
= 0; i
< ARRAY_SIZE(commands
); i
++) {
240 struct cmd_struct
*p
= commands
+i
;
241 if (strcmp(p
->cmd
, cmd
))
244 if (getenv("GIT_TRACE")) {
246 fprintf(stderr
, "trace: built-in: git");
247 for (i
= 0; i
< argc
; ++i
) {
249 sq_quote_print(stderr
, argv
[i
]);
255 exit(p
->fn(argc
, argv
, envp
));
259 int main(int argc
, const char **argv
, char **envp
)
261 const char *cmd
= argv
[0];
262 char *slash
= strrchr(cmd
, '/');
263 const char *exec_path
= NULL
;
267 * Take the basename of argv[0] as the command
268 * name, and the dirname as the default exec_path
269 * if it's an absolute path and we don't have
280 * "git-xxxx" is the same as "git xxxx", but we obviously:
282 * - cannot take flags in between the "git" and the "xxxx".
283 * - cannot execute it externally (since it would just do
284 * the same thing over again)
286 * So we just directly call the internal command handler, and
287 * die if that one cannot handle it.
289 if (!strncmp(cmd
, "git-", 4)) {
292 handle_internal_command(argc
, argv
, envp
);
293 die("cannot handle %s internally", cmd
);
296 /* Default command: "help" */
299 /* Look for flags.. */
304 handle_options(&argv
, &argc
);
308 if (strncmp(cmd
, "--", 2))
314 * For legacy reasons, the "version" and "help"
315 * commands can be written with "--" prepended
316 * to make them look like flags.
318 if (!strcmp(cmd
, "help"))
320 if (!strcmp(cmd
, "version"))
324 * Check remaining flags (which by now must be
325 * "--exec-path", but maybe we will accept
326 * other arguments some day)
328 if (!strncmp(cmd
, "exec-path", 9)) {
331 git_set_exec_path(cmd
+ 1);
334 puts(git_exec_path());
337 cmd_usage(0, NULL
, NULL
);
342 * We search for git commands in the following order:
344 * - the path of the "git" command if we could find it
346 * - the regular PATH.
349 prepend_to_path(exec_path
, strlen(exec_path
));
350 exec_path
= git_exec_path();
351 prepend_to_path(exec_path
, strlen(exec_path
));
354 /* See if it's an internal command */
355 handle_internal_command(argc
, argv
, envp
);
357 /* .. then try the external ones */
360 /* It could be an alias -- this works around the insanity
361 * of overriding "git log" with "git show" by having
364 if (done_alias
|| !handle_alias(&argc
, &argv
))
370 cmd_usage(0, exec_path
, "'%s' is not a git-command", cmd
);
372 fprintf(stderr
, "Failed to run command '%s': %s\n",
373 cmd
, strerror(errno
));