11 #include "git-compat-util.h"
17 static void prepend_to_path(const char *dir
, int len
)
19 const char *old_path
= getenv("PATH");
24 old_path
= "/usr/local/bin:/usr/bin:/bin";
26 path_len
= len
+ strlen(old_path
) + 1;
28 path
= malloc(path_len
+ 1);
30 memcpy(path
, dir
, len
);
32 memcpy(path
+ len
+ 1, old_path
, path_len
- len
);
34 setenv("PATH", path
, 1);
37 static const char *alias_command
;
38 static char *alias_string
= NULL
;
40 static int git_alias_config(const char *var
, const char *value
)
42 if (!strncmp(var
, "alias.", 6) && !strcmp(var
+ 6, alias_command
)) {
43 alias_string
= strdup(value
);
48 static int split_cmdline(char *cmdline
, const char ***argv
)
50 int src
, dst
, count
= 0, size
= 16;
53 *argv
= malloc(sizeof(char*) * size
);
55 /* split alias_string */
56 (*argv
)[count
++] = cmdline
;
57 for (src
= dst
= 0; cmdline
[src
];) {
58 char c
= cmdline
[src
];
59 if (!quoted
&& isspace(c
)) {
62 && isspace(cmdline
[src
]))
66 *argv
= realloc(*argv
, sizeof(char*) * size
);
68 (*argv
)[count
++] = cmdline
+ dst
;
69 } else if(!quoted
&& (c
== '\'' || c
== '"')) {
72 } else if (c
== quoted
) {
76 if (c
== '\\' && quoted
!= '\'') {
82 return error("cmdline ends with \\");
95 return error("unclosed quote");
101 static int handle_alias(int *argcp
, const char ***argv
)
103 int nongit
= 0, ret
= 0;
106 subdir
= setup_git_directory_gently(&nongit
);
109 const char** new_argv
;
111 alias_command
= (*argv
)[0];
112 git_config(git_alias_config
);
115 count
= split_cmdline(alias_string
, &new_argv
);
118 die("empty alias for %s", alias_command
);
120 if (!strcmp(alias_command
, new_argv
[0]))
121 die("recursive alias: %s", alias_command
);
123 /* insert after command name */
125 new_argv
= realloc(new_argv
, sizeof(char*) *
127 memcpy(new_argv
+ count
, *argv
+ 1,
128 sizeof(char*) * *argcp
);
144 const char git_version_string
[] = GIT_VERSION
;
146 static void handle_internal_command(int argc
, const char **argv
, char **envp
)
148 const char *cmd
= argv
[0];
149 static struct cmd_struct
{
151 int (*fn
)(int, const char **, char **);
153 { "version", cmd_version
},
154 { "help", cmd_help
},
156 { "whatchanged", cmd_whatchanged
},
157 { "show", cmd_show
},
158 { "push", cmd_push
},
159 { "format-patch", cmd_format_patch
},
160 { "count-objects", cmd_count_objects
},
161 { "diff", cmd_diff
},
162 { "grep", cmd_grep
},
165 { "rev-list", cmd_rev_list
},
166 { "init-db", cmd_init_db
},
167 { "get-tar-commit-id", cmd_get_tar_commit_id
},
168 { "upload-tar", cmd_upload_tar
},
169 { "check-ref-format", cmd_check_ref_format
},
170 { "ls-files", cmd_ls_files
},
171 { "ls-tree", cmd_ls_tree
},
172 { "tar-tree", cmd_tar_tree
},
173 { "read-tree", cmd_read_tree
},
174 { "commit-tree", cmd_commit_tree
},
175 { "apply", cmd_apply
},
176 { "show-branch", cmd_show_branch
},
177 { "diff-files", cmd_diff_files
},
178 { "diff-index", cmd_diff_index
},
179 { "diff-stages", cmd_diff_stages
},
180 { "diff-tree", cmd_diff_tree
},
181 { "cat-file", cmd_cat_file
},
182 { "rev-parse", cmd_rev_parse
},
183 { "write-tree", cmd_write_tree
},
184 { "mailsplit", cmd_mailsplit
},
185 { "mailinfo", cmd_mailinfo
},
186 { "stripspace", cmd_stripspace
},
187 { "update-index", cmd_update_index
},
188 { "update-ref", cmd_update_ref
}
192 /* Turn "git cmd --help" into "git help cmd" */
193 if (argc
> 1 && !strcmp(argv
[1], "--help")) {
195 argv
[0] = cmd
= "help";
198 for (i
= 0; i
< ARRAY_SIZE(commands
); i
++) {
199 struct cmd_struct
*p
= commands
+i
;
200 if (strcmp(p
->cmd
, cmd
))
202 exit(p
->fn(argc
, argv
, envp
));
206 int main(int argc
, const char **argv
, char **envp
)
208 const char *cmd
= argv
[0];
209 char *slash
= strrchr(cmd
, '/');
210 const char *exec_path
= NULL
;
214 * Take the basename of argv[0] as the command
215 * name, and the dirname as the default exec_path
216 * if it's an absolute path and we don't have
227 * "git-xxxx" is the same as "git xxxx", but we obviously:
229 * - cannot take flags in between the "git" and the "xxxx".
230 * - cannot execute it externally (since it would just do
231 * the same thing over again)
233 * So we just directly call the internal command handler, and
234 * die if that one cannot handle it.
236 if (!strncmp(cmd
, "git-", 4)) {
239 handle_internal_command(argc
, argv
, envp
);
240 die("cannot handle %s internally", cmd
);
243 /* Default command: "help" */
246 /* Look for flags.. */
251 if (strncmp(cmd
, "--", 2))
257 * For legacy reasons, the "version" and "help"
258 * commands can be written with "--" prepended
259 * to make them look like flags.
261 if (!strcmp(cmd
, "help"))
263 if (!strcmp(cmd
, "version"))
267 * Check remaining flags (which by now must be
268 * "--exec-path", but maybe we will accept
269 * other arguments some day)
271 if (!strncmp(cmd
, "exec-path", 9)) {
274 git_set_exec_path(cmd
+ 1);
277 puts(git_exec_path());
280 cmd_usage(0, NULL
, NULL
);
285 * We search for git commands in the following order:
287 * - the path of the "git" command if we could find it
289 * - the regular PATH.
292 prepend_to_path(exec_path
, strlen(exec_path
));
293 exec_path
= git_exec_path();
294 prepend_to_path(exec_path
, strlen(exec_path
));
297 /* See if it's an internal command */
298 handle_internal_command(argc
, argv
, envp
);
300 /* .. then try the external ones */
303 /* It could be an alias -- this works around the insanity
304 * of overriding "git log" with "git show" by having
307 if (done_alias
|| !handle_alias(&argc
, &argv
))
313 cmd_usage(0, exec_path
, "'%s' is not a git-command", cmd
);
315 fprintf(stderr
, "Failed to run command '%s': %s\n",
316 cmd
, strerror(errno
));