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];
48 * For legacy reasons, the "version" and "help"
49 * commands can be written with "--" prepended
50 * to make them look like flags.
52 if (!strcmp(cmd
, "--help") || !strcmp(cmd
, "--version"))
56 * Check remaining flags.
58 if (!strncmp(cmd
, "--exec-path", 11)) {
61 git_set_exec_path(cmd
+ 1);
63 puts(git_exec_path());
66 } else if (!strcmp(cmd
, "-p") || !strcmp(cmd
, "--paginate")) {
68 } else if (!strcmp(cmd
, "--git-dir")) {
71 setenv("GIT_DIR", (*argv
)[1], 1);
74 } else if (!strncmp(cmd
, "--git-dir=", 10)) {
75 setenv("GIT_DIR", cmd
+ 10, 1);
76 } else if (!strcmp(cmd
, "--bare")) {
77 static char git_dir
[1024];
78 setenv("GIT_DIR", getcwd(git_dir
, 1024), 1);
80 fprintf(stderr
, "Unknown option: %s\n", cmd
);
81 cmd_usage(0, NULL
, NULL
);
91 static const char *alias_command
;
92 static char *alias_string
= NULL
;
94 static int git_alias_config(const char *var
, const char *value
)
96 if (!strncmp(var
, "alias.", 6) && !strcmp(var
+ 6, alias_command
)) {
97 alias_string
= strdup(value
);
102 static int split_cmdline(char *cmdline
, const char ***argv
)
104 int src
, dst
, count
= 0, size
= 16;
107 *argv
= malloc(sizeof(char*) * size
);
109 /* split alias_string */
110 (*argv
)[count
++] = cmdline
;
111 for (src
= dst
= 0; cmdline
[src
];) {
112 char c
= cmdline
[src
];
113 if (!quoted
&& isspace(c
)) {
115 while (cmdline
[++src
]
116 && isspace(cmdline
[src
]))
120 *argv
= realloc(*argv
, sizeof(char*) * size
);
122 (*argv
)[count
++] = cmdline
+ dst
;
123 } else if(!quoted
&& (c
== '\'' || c
== '"')) {
126 } else if (c
== quoted
) {
130 if (c
== '\\' && quoted
!= '\'') {
136 return error("cmdline ends with \\");
149 return error("unclosed quote");
155 static int handle_alias(int *argcp
, const char ***argv
)
157 int nongit
= 0, ret
= 0, saved_errno
= errno
;
159 int count
, option_count
;
160 const char** new_argv
;
162 subdir
= setup_git_directory_gently(&nongit
);
164 alias_command
= (*argv
)[0];
165 git_config(git_alias_config
);
167 count
= split_cmdline(alias_string
, &new_argv
);
168 option_count
= handle_options(&new_argv
, &count
);
169 memmove(new_argv
- option_count
, new_argv
,
170 count
* sizeof(char *));
171 new_argv
-= option_count
;
174 die("empty alias for %s", alias_command
);
176 if (!strcmp(alias_command
, new_argv
[0]))
177 die("recursive alias: %s", alias_command
);
179 if (getenv("GIT_TRACE")) {
181 fprintf(stderr
, "trace: alias expansion: %s =>",
183 for (i
= 0; i
< count
; ++i
) {
185 sq_quote_print(stderr
, new_argv
[i
]);
191 new_argv
= realloc(new_argv
, sizeof(char*) *
192 (count
+ *argcp
+ 1));
193 /* insert after command name */
194 memcpy(new_argv
+ count
, *argv
+ 1, sizeof(char*) * *argcp
);
195 new_argv
[count
+*argcp
] = NULL
;
211 const char git_version_string
[] = GIT_VERSION
;
213 #define NEEDS_PREFIX 1
216 static void handle_internal_command(int argc
, const char **argv
, char **envp
)
218 const char *cmd
= argv
[0];
219 static struct cmd_struct
{
221 int (*fn
)(int, const char **, const char *);
224 { "version", cmd_version
},
225 { "help", cmd_help
},
226 { "log", cmd_log
, NEEDS_PREFIX
| USE_PAGER
},
227 { "whatchanged", cmd_whatchanged
, NEEDS_PREFIX
| USE_PAGER
},
228 { "show", cmd_show
, NEEDS_PREFIX
| USE_PAGER
},
229 { "push", cmd_push
},
230 { "format-patch", cmd_format_patch
, NEEDS_PREFIX
},
231 { "count-objects", cmd_count_objects
},
232 { "diff", cmd_diff
, NEEDS_PREFIX
},
233 { "grep", cmd_grep
, NEEDS_PREFIX
},
234 { "rm", cmd_rm
, NEEDS_PREFIX
},
235 { "add", cmd_add
, NEEDS_PREFIX
},
236 { "rev-list", cmd_rev_list
, NEEDS_PREFIX
},
237 { "init-db", cmd_init_db
},
238 { "get-tar-commit-id", cmd_get_tar_commit_id
},
239 { "upload-tar", cmd_upload_tar
},
240 { "check-ref-format", cmd_check_ref_format
},
241 { "ls-files", cmd_ls_files
, NEEDS_PREFIX
},
242 { "ls-tree", cmd_ls_tree
, NEEDS_PREFIX
},
243 { "tar-tree", cmd_tar_tree
, NEEDS_PREFIX
},
244 { "read-tree", cmd_read_tree
, NEEDS_PREFIX
},
245 { "commit-tree", cmd_commit_tree
, NEEDS_PREFIX
},
246 { "apply", cmd_apply
},
247 { "show-branch", cmd_show_branch
, NEEDS_PREFIX
},
248 { "diff-files", cmd_diff_files
, NEEDS_PREFIX
},
249 { "diff-index", cmd_diff_index
, NEEDS_PREFIX
},
250 { "diff-stages", cmd_diff_stages
, NEEDS_PREFIX
},
251 { "diff-tree", cmd_diff_tree
, NEEDS_PREFIX
},
252 { "cat-file", cmd_cat_file
, NEEDS_PREFIX
},
253 { "rev-parse", cmd_rev_parse
, NEEDS_PREFIX
},
254 { "write-tree", cmd_write_tree
, NEEDS_PREFIX
},
255 { "mailsplit", cmd_mailsplit
},
256 { "mailinfo", cmd_mailinfo
},
257 { "stripspace", cmd_stripspace
},
258 { "update-index", cmd_update_index
, NEEDS_PREFIX
},
259 { "update-ref", cmd_update_ref
, NEEDS_PREFIX
},
260 { "fmt-merge-msg", cmd_fmt_merge_msg
, NEEDS_PREFIX
},
261 { "prune", cmd_prune
, NEEDS_PREFIX
},
262 { "mv", cmd_mv
, NEEDS_PREFIX
},
266 /* Turn "git cmd --help" into "git help cmd" */
267 if (argc
> 1 && !strcmp(argv
[1], "--help")) {
269 argv
[0] = cmd
= "help";
272 for (i
= 0; i
< ARRAY_SIZE(commands
); i
++) {
273 struct cmd_struct
*p
= commands
+i
;
275 if (strcmp(p
->cmd
, cmd
))
279 if (p
->option
& NEEDS_PREFIX
)
280 prefix
= setup_git_directory();
281 if (p
->option
& USE_PAGER
)
283 if (getenv("GIT_TRACE")) {
285 fprintf(stderr
, "trace: built-in: git");
286 for (i
= 0; i
< argc
; ++i
) {
288 sq_quote_print(stderr
, argv
[i
]);
294 exit(p
->fn(argc
, argv
, prefix
));
298 int main(int argc
, const char **argv
, char **envp
)
300 const char *cmd
= argv
[0];
301 char *slash
= strrchr(cmd
, '/');
302 const char *exec_path
= NULL
;
306 * Take the basename of argv[0] as the command
307 * name, and the dirname as the default exec_path
308 * if it's an absolute path and we don't have
319 * "git-xxxx" is the same as "git xxxx", but we obviously:
321 * - cannot take flags in between the "git" and the "xxxx".
322 * - cannot execute it externally (since it would just do
323 * the same thing over again)
325 * So we just directly call the internal command handler, and
326 * die if that one cannot handle it.
328 if (!strncmp(cmd
, "git-", 4)) {
331 handle_internal_command(argc
, argv
, envp
);
332 die("cannot handle %s internally", cmd
);
335 /* Look for flags.. */
338 handle_options(&argv
, &argc
);
340 if (!strncmp(argv
[0], "--", 2))
343 /* Default command: "help" */
350 * We search for git commands in the following order:
352 * - the path of the "git" command if we could find it
354 * - the regular PATH.
357 prepend_to_path(exec_path
, strlen(exec_path
));
358 exec_path
= git_exec_path();
359 prepend_to_path(exec_path
, strlen(exec_path
));
362 /* See if it's an internal command */
363 handle_internal_command(argc
, argv
, envp
);
365 /* .. then try the external ones */
368 /* It could be an alias -- this works around the insanity
369 * of overriding "git log" with "git show" by having
372 if (done_alias
|| !handle_alias(&argc
, &argv
))
378 cmd_usage(0, exec_path
, "'%s' is not a git-command", cmd
);
380 fprintf(stderr
, "Failed to run command '%s': %s\n",
381 cmd
, strerror(errno
));