]>
Commit | Line | Data |
---|---|---|
85023577 | 1 | #include "builtin.h" |
2b11e317 | 2 | #include "cache.h" |
fd5c363d TF |
3 | #include "exec_cmd.h" |
4 | #include "help.h" | |
575ba9d6 | 5 | #include "quote.h" |
d8e96fd8 | 6 | #include "run-command.h" |
6035d6aa | 7 | #include "commit.h" |
8e49d503 | 8 | |
822a7d50 | 9 | const char git_usage_string[] = |
03a0fb0c KB |
10 | "git [--version] [--help] [-c name=value]\n" |
11 | " [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]\n" | |
a1bea2c1 JT |
12 | " [-p|--paginate|--no-pager] [--no-replace-objects] [--bare]\n" |
13 | " [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]\n" | |
62b4698e | 14 | " <command> [<args>]"; |
822a7d50 | 15 | |
b7d96819 | 16 | const char git_more_info_string[] = |
73903d0b PO |
17 | N_("'git help -a' and 'git help -g' lists available subcommands and some\n" |
18 | "concept guides. See 'git help <command>' or 'git help <concept>'\n" | |
19 | "to read about a specific subcommand or concept."); | |
b7d96819 | 20 | |
e37c1329 | 21 | static struct startup_info git_startup_info; |
4e10738a | 22 | static int use_pager = -1; |
4e10738a JK |
23 | |
24 | static void commit_pager_choice(void) { | |
25 | switch (use_pager) { | |
26 | case 0: | |
27 | setenv("GIT_PAGER", "cat", 1); | |
28 | break; | |
29 | case 1: | |
30 | setup_pager(); | |
31 | break; | |
32 | default: | |
33 | break; | |
34 | } | |
35 | } | |
36 | ||
4b25d091 | 37 | static int handle_options(const char ***argv, int *argc, int *envchanged) |
4ab243a9 | 38 | { |
73546c08 | 39 | const char **orig_argv = *argv; |
4ab243a9 JS |
40 | |
41 | while (*argc > 0) { | |
42 | const char *cmd = (*argv)[0]; | |
43 | if (cmd[0] != '-') | |
44 | break; | |
45 | ||
6acbcb92 JS |
46 | /* |
47 | * For legacy reasons, the "version" and "help" | |
48 | * commands can be written with "--" prepended | |
49 | * to make them look like flags. | |
50 | */ | |
51 | if (!strcmp(cmd, "--help") || !strcmp(cmd, "--version")) | |
52 | break; | |
53 | ||
54 | /* | |
55 | * Check remaining flags. | |
56 | */ | |
cc44c765 | 57 | if (!prefixcmp(cmd, "--exec-path")) { |
6acbcb92 JS |
58 | cmd += 11; |
59 | if (*cmd == '=') | |
384df833 | 60 | git_set_argv_exec_path(cmd + 1); |
6acbcb92 JS |
61 | else { |
62 | puts(git_exec_path()); | |
63 | exit(0); | |
64 | } | |
89a56bfb MH |
65 | } else if (!strcmp(cmd, "--html-path")) { |
66 | puts(system_path(GIT_HTML_PATH)); | |
67 | exit(0); | |
f2dd8c37 JS |
68 | } else if (!strcmp(cmd, "--man-path")) { |
69 | puts(system_path(GIT_MAN_PATH)); | |
70 | exit(0); | |
71 | } else if (!strcmp(cmd, "--info-path")) { | |
72 | puts(system_path(GIT_INFO_PATH)); | |
73 | exit(0); | |
6acbcb92 | 74 | } else if (!strcmp(cmd, "-p") || !strcmp(cmd, "--paginate")) { |
4e10738a | 75 | use_pager = 1; |
463a849d | 76 | } else if (!strcmp(cmd, "--no-pager")) { |
4e10738a | 77 | use_pager = 0; |
463a849d MM |
78 | if (envchanged) |
79 | *envchanged = 1; | |
b0fa7ab5 CC |
80 | } else if (!strcmp(cmd, "--no-replace-objects")) { |
81 | read_replace_refs = 0; | |
6476b38b CC |
82 | setenv(NO_REPLACE_OBJECTS_ENVIRONMENT, "1", 1); |
83 | if (envchanged) | |
84 | *envchanged = 1; | |
6acbcb92 | 85 | } else if (!strcmp(cmd, "--git-dir")) { |
c321f00d BG |
86 | if (*argc < 2) { |
87 | fprintf(stderr, "No directory given for --git-dir.\n" ); | |
88 | usage(git_usage_string); | |
89 | } | |
45b09794 | 90 | setenv(GIT_DIR_ENVIRONMENT, (*argv)[1], 1); |
4394efec ML |
91 | if (envchanged) |
92 | *envchanged = 1; | |
6acbcb92 JS |
93 | (*argv)++; |
94 | (*argc)--; | |
cc44c765 | 95 | } else if (!prefixcmp(cmd, "--git-dir=")) { |
45b09794 | 96 | setenv(GIT_DIR_ENVIRONMENT, cmd + 10, 1); |
4394efec ML |
97 | if (envchanged) |
98 | *envchanged = 1; | |
a1bea2c1 JT |
99 | } else if (!strcmp(cmd, "--namespace")) { |
100 | if (*argc < 2) { | |
101 | fprintf(stderr, "No namespace given for --namespace.\n" ); | |
102 | usage(git_usage_string); | |
103 | } | |
104 | setenv(GIT_NAMESPACE_ENVIRONMENT, (*argv)[1], 1); | |
105 | if (envchanged) | |
106 | *envchanged = 1; | |
107 | (*argv)++; | |
108 | (*argc)--; | |
109 | } else if (!prefixcmp(cmd, "--namespace=")) { | |
110 | setenv(GIT_NAMESPACE_ENVIRONMENT, cmd + 12, 1); | |
111 | if (envchanged) | |
112 | *envchanged = 1; | |
892c41b9 ML |
113 | } else if (!strcmp(cmd, "--work-tree")) { |
114 | if (*argc < 2) { | |
115 | fprintf(stderr, "No directory given for --work-tree.\n" ); | |
116 | usage(git_usage_string); | |
117 | } | |
118 | setenv(GIT_WORK_TREE_ENVIRONMENT, (*argv)[1], 1); | |
4394efec ML |
119 | if (envchanged) |
120 | *envchanged = 1; | |
892c41b9 ML |
121 | (*argv)++; |
122 | (*argc)--; | |
123 | } else if (!prefixcmp(cmd, "--work-tree=")) { | |
124 | setenv(GIT_WORK_TREE_ENVIRONMENT, cmd + 12, 1); | |
4394efec ML |
125 | if (envchanged) |
126 | *envchanged = 1; | |
6acbcb92 | 127 | } else if (!strcmp(cmd, "--bare")) { |
ef5ddb2f | 128 | static char git_dir[PATH_MAX+1]; |
6adcca3f | 129 | is_bare_repository_cfg = 1; |
9277d602 | 130 | setenv(GIT_DIR_ENVIRONMENT, getcwd(git_dir, sizeof(git_dir)), 0); |
2cd83d10 | 131 | setenv(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT, "0", 1); |
4394efec ML |
132 | if (envchanged) |
133 | *envchanged = 1; | |
8b1fa778 AR |
134 | } else if (!strcmp(cmd, "-c")) { |
135 | if (*argc < 2) { | |
136 | fprintf(stderr, "-c expects a configuration string\n" ); | |
137 | usage(git_usage_string); | |
138 | } | |
2b64fc89 | 139 | git_config_push_parameter((*argv)[1]); |
8b1fa778 AR |
140 | (*argv)++; |
141 | (*argc)--; | |
823ab40f JK |
142 | } else if (!strcmp(cmd, "--literal-pathspecs")) { |
143 | setenv(GIT_LITERAL_PATHSPECS_ENVIRONMENT, "1", 1); | |
144 | if (envchanged) | |
145 | *envchanged = 1; | |
146 | } else if (!strcmp(cmd, "--no-literal-pathspecs")) { | |
147 | setenv(GIT_LITERAL_PATHSPECS_ENVIRONMENT, "0", 1); | |
148 | if (envchanged) | |
149 | *envchanged = 1; | |
6035d6aa NTND |
150 | } else if (!strcmp(cmd, "--shallow-file")) { |
151 | (*argv)++; | |
152 | (*argc)--; | |
153 | set_alternate_shallow_file((*argv)[0]); | |
154 | if (envchanged) | |
155 | *envchanged = 1; | |
6acbcb92 JS |
156 | } else { |
157 | fprintf(stderr, "Unknown option: %s\n", cmd); | |
822a7d50 | 158 | usage(git_usage_string); |
6acbcb92 | 159 | } |
4ab243a9 JS |
160 | |
161 | (*argv)++; | |
162 | (*argc)--; | |
4ab243a9 | 163 | } |
73546c08 | 164 | return (*argv) - orig_argv; |
4ab243a9 JS |
165 | } |
166 | ||
2b11e317 JS |
167 | static int handle_alias(int *argcp, const char ***argv) |
168 | { | |
af05d679 | 169 | int envchanged = 0, ret = 0, saved_errno = errno; |
2b11e317 | 170 | const char *subdir; |
0347a8c7 | 171 | int count, option_count; |
4b25d091 | 172 | const char **new_argv; |
94351118 JK |
173 | const char *alias_command; |
174 | char *alias_string; | |
e85dc0a3 | 175 | int unused_nongit; |
2b11e317 | 176 | |
e85dc0a3 | 177 | subdir = setup_git_directory_gently(&unused_nongit); |
0347a8c7 ML |
178 | |
179 | alias_command = (*argv)[0]; | |
94351118 | 180 | alias_string = alias_lookup(alias_command); |
0347a8c7 | 181 | if (alias_string) { |
dfd42a3c | 182 | if (alias_string[0] == '!') { |
7f51f8bc EFL |
183 | const char **alias_argv; |
184 | int argc = *argcp, i; | |
185 | ||
030149a4 | 186 | commit_pager_choice(); |
7f51f8bc EFL |
187 | |
188 | /* build alias_argv */ | |
189 | alias_argv = xmalloc(sizeof(*alias_argv) * (argc + 1)); | |
190 | alias_argv[0] = alias_string + 1; | |
191 | for (i = 1; i < argc; ++i) | |
192 | alias_argv[i] = (*argv)[i]; | |
193 | alias_argv[argc] = NULL; | |
194 | ||
26b05251 | 195 | ret = run_command_v_opt(alias_argv, RUN_USING_SHELL); |
7f51f8bc EFL |
196 | if (ret >= 0) /* normal exit */ |
197 | exit(ret); | |
198 | ||
199 | die_errno("While expanding alias '%s': '%s'", | |
200 | alias_command, alias_string + 1); | |
dfd42a3c | 201 | } |
0347a8c7 | 202 | count = split_cmdline(alias_string, &new_argv); |
dc4179f9 | 203 | if (count < 0) |
ad9ac6db GB |
204 | die("Bad alias.%s string: %s", alias_command, |
205 | split_cmdline_strerror(count)); | |
4394efec ML |
206 | option_count = handle_options(&new_argv, &count, &envchanged); |
207 | if (envchanged) | |
208 | die("alias '%s' changes environment variables\n" | |
209 | "You can use '!git' in the alias to do this.", | |
210 | alias_command); | |
0347a8c7 ML |
211 | memmove(new_argv - option_count, new_argv, |
212 | count * sizeof(char *)); | |
213 | new_argv -= option_count; | |
214 | ||
215 | if (count < 1) | |
216 | die("empty alias for %s", alias_command); | |
217 | ||
218 | if (!strcmp(alias_command, new_argv[0])) | |
219 | die("recursive alias: %s", alias_command); | |
220 | ||
b319ce4c | 221 | trace_argv_printf(new_argv, |
6ce4e61f CC |
222 | "trace: alias expansion: %s =>", |
223 | alias_command); | |
575ba9d6 | 224 | |
4b25d091 | 225 | new_argv = xrealloc(new_argv, sizeof(char *) * |
6167c136 | 226 | (count + *argcp)); |
0347a8c7 | 227 | /* insert after command name */ |
4b25d091 | 228 | memcpy(new_argv + count, *argv + 1, sizeof(char *) * *argcp); |
2b11e317 | 229 | |
0347a8c7 ML |
230 | *argv = new_argv; |
231 | *argcp += count - 1; | |
2b11e317 | 232 | |
0347a8c7 | 233 | ret = 1; |
2b11e317 JS |
234 | } |
235 | ||
7be77de2 | 236 | if (subdir && chdir(subdir)) |
d824cbba | 237 | die_errno("Cannot change to '%s'", subdir); |
2b11e317 | 238 | |
47e5c0ca JS |
239 | errno = saved_errno; |
240 | ||
2b11e317 JS |
241 | return ret; |
242 | } | |
243 | ||
ee38dfb8 NTND |
244 | #define RUN_SETUP (1<<0) |
245 | #define RUN_SETUP_GENTLY (1<<1) | |
246 | #define USE_PAGER (1<<2) | |
7eff28a9 SP |
247 | /* |
248 | * require working tree to be present -- anything uses this needs | |
249 | * RUN_SETUP for reading from the configuration file. | |
250 | */ | |
ee38dfb8 | 251 | #define NEED_WORK_TREE (1<<3) |
a633fca0 | 252 | |
47d0b4ff LT |
253 | struct cmd_struct { |
254 | const char *cmd; | |
255 | int (*fn)(int, const char **, const char *); | |
256 | int option; | |
257 | }; | |
258 | ||
f172f334 | 259 | static int run_builtin(struct cmd_struct *p, int argc, const char **argv) |
47d0b4ff | 260 | { |
99caeed0 | 261 | int status, help; |
0f157315 | 262 | struct stat st; |
47d0b4ff LT |
263 | const char *prefix; |
264 | ||
265 | prefix = NULL; | |
99caeed0 JN |
266 | help = argc == 2 && !strcmp(argv[1], "-h"); |
267 | if (!help) { | |
268 | if (p->option & RUN_SETUP) | |
269 | prefix = setup_git_directory(); | |
ee38dfb8 NTND |
270 | if (p->option & RUN_SETUP_GENTLY) { |
271 | int nongit_ok; | |
272 | prefix = setup_git_directory_gently(&nongit_ok); | |
273 | } | |
99caeed0 | 274 | |
ee38dfb8 | 275 | if (use_pager == -1 && p->option & (RUN_SETUP | RUN_SETUP_GENTLY)) |
99caeed0 JN |
276 | use_pager = check_pager_config(p->cmd); |
277 | if (use_pager == -1 && p->option & USE_PAGER) | |
278 | use_pager = 1; | |
a9ca8a85 NTND |
279 | |
280 | if ((p->option & (RUN_SETUP | RUN_SETUP_GENTLY)) && | |
281 | startup_info->have_repository) /* get_git_dir() may set up repo, avoid that */ | |
282 | trace_repo_setup(prefix); | |
99caeed0 | 283 | } |
4e10738a JK |
284 | commit_pager_choice(); |
285 | ||
99caeed0 | 286 | if (!help && p->option & NEED_WORK_TREE) |
59f0f2f3 MH |
287 | setup_work_tree(); |
288 | ||
b319ce4c | 289 | trace_argv_printf(argv, "trace: built-in: git"); |
47d0b4ff | 290 | |
0f157315 LT |
291 | status = p->fn(argc, argv, prefix); |
292 | if (status) | |
47e3de0e | 293 | return status; |
0f157315 LT |
294 | |
295 | /* Somebody closed stdout? */ | |
296 | if (fstat(fileno(stdout), &st)) | |
297 | return 0; | |
298 | /* Ignore write errors for pipes and sockets.. */ | |
299 | if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode)) | |
300 | return 0; | |
301 | ||
302 | /* Check for ENOSPC and EIO errors.. */ | |
0227f988 | 303 | if (fflush(stdout)) |
d824cbba | 304 | die_errno("write failure on standard output"); |
0227f988 LT |
305 | if (ferror(stdout)) |
306 | die("unknown write failure on standard output"); | |
307 | if (fclose(stdout)) | |
d824cbba | 308 | die_errno("close failed on standard output"); |
0f157315 | 309 | return 0; |
47d0b4ff LT |
310 | } |
311 | ||
312 | static void handle_internal_command(int argc, const char **argv) | |
231af832 LT |
313 | { |
314 | const char *cmd = argv[0]; | |
47d0b4ff | 315 | static struct cmd_struct commands[] = { |
7ae3df8c | 316 | { "add", cmd_add, RUN_SETUP | NEED_WORK_TREE }, |
d97bc5de | 317 | { "annotate", cmd_annotate, RUN_SETUP }, |
d1ea8962 | 318 | { "apply", cmd_apply, RUN_SETUP_GENTLY }, |
265d5280 | 319 | { "archive", cmd_archive }, |
24c51280 | 320 | { "bisect--helper", cmd_bisect__helper, RUN_SETUP }, |
4f0219a4 | 321 | { "blame", cmd_blame, RUN_SETUP }, |
1da1b3a3 | 322 | { "branch", cmd_branch, RUN_SETUP }, |
2cb60093 | 323 | { "bundle", cmd_bundle, RUN_SETUP_GENTLY }, |
efffea03 | 324 | { "cat-file", cmd_cat_file, RUN_SETUP }, |
ef5dd8a9 | 325 | { "check-attr", cmd_check_attr, RUN_SETUP }, |
368aa529 | 326 | { "check-ignore", cmd_check_ignore, RUN_SETUP | NEED_WORK_TREE }, |
226ad348 | 327 | { "check-mailmap", cmd_check_mailmap, RUN_SETUP }, |
ef5dd8a9 | 328 | { "check-ref-format", cmd_check_ref_format }, |
782c2d65 | 329 | { "checkout", cmd_checkout, RUN_SETUP | NEED_WORK_TREE }, |
4465f410 JS |
330 | { "checkout-index", cmd_checkout_index, |
331 | RUN_SETUP | NEED_WORK_TREE}, | |
e827633a | 332 | { "cherry", cmd_cherry, RUN_SETUP }, |
7ae3df8c | 333 | { "cherry-pick", cmd_cherry_pick, RUN_SETUP | NEED_WORK_TREE }, |
113f10f2 | 334 | { "clean", cmd_clean, RUN_SETUP | NEED_WORK_TREE }, |
ef5dd8a9 | 335 | { "clone", cmd_clone }, |
7e29b825 | 336 | { "column", cmd_column, RUN_SETUP_GENTLY }, |
f5bbc322 | 337 | { "commit", cmd_commit, RUN_SETUP | NEED_WORK_TREE }, |
efffea03 | 338 | { "commit-tree", cmd_commit_tree, RUN_SETUP }, |
3ba7e6e2 | 339 | { "config", cmd_config, RUN_SETUP_GENTLY }, |
8112894d | 340 | { "count-objects", cmd_count_objects, RUN_SETUP }, |
e30b2feb | 341 | { "credential", cmd_credential, RUN_SETUP_GENTLY }, |
9a0eaf83 | 342 | { "describe", cmd_describe, RUN_SETUP }, |
89d07f75 | 343 | { "diff", cmd_diff }, |
4f38f6b5 | 344 | { "diff-files", cmd_diff_files, RUN_SETUP | NEED_WORK_TREE }, |
efffea03 | 345 | { "diff-index", cmd_diff_index, RUN_SETUP }, |
efffea03 | 346 | { "diff-tree", cmd_diff_tree, RUN_SETUP }, |
f2dc849e | 347 | { "fast-export", cmd_fast_export, RUN_SETUP }, |
b888d61c | 348 | { "fetch", cmd_fetch, RUN_SETUP }, |
2d4177c0 | 349 | { "fetch-pack", cmd_fetch_pack, RUN_SETUP }, |
efffea03 | 350 | { "fmt-merge-msg", cmd_fmt_merge_msg, RUN_SETUP }, |
9f613ddd | 351 | { "for-each-ref", cmd_for_each_ref, RUN_SETUP }, |
efffea03 | 352 | { "format-patch", cmd_format_patch, RUN_SETUP }, |
b4dfefe0 MW |
353 | { "fsck", cmd_fsck, RUN_SETUP }, |
354 | { "fsck-objects", cmd_fsck, RUN_SETUP }, | |
6757ada4 | 355 | { "gc", cmd_gc, RUN_SETUP }, |
f754fa9c | 356 | { "get-tar-commit-id", cmd_get_tar_commit_id }, |
ff38d1a9 | 357 | { "grep", cmd_grep, RUN_SETUP_GENTLY }, |
b28a1ce0 | 358 | { "hash-object", cmd_hash_object }, |
f754fa9c | 359 | { "help", cmd_help }, |
e0fce074 | 360 | { "index-pack", cmd_index_pack, RUN_SETUP_GENTLY }, |
515377ea | 361 | { "init", cmd_init_db }, |
f754fa9c | 362 | { "init-db", cmd_init_db }, |
1fda91b5 | 363 | { "log", cmd_log, RUN_SETUP }, |
efffea03 | 364 | { "ls-files", cmd_ls_files, RUN_SETUP }, |
f0ef6a6e | 365 | { "ls-remote", cmd_ls_remote, RUN_SETUP_GENTLY }, |
ef5dd8a9 | 366 | { "ls-tree", cmd_ls_tree, RUN_SETUP }, |
f754fa9c JH |
367 | { "mailinfo", cmd_mailinfo }, |
368 | { "mailsplit", cmd_mailsplit }, | |
1c7b76be | 369 | { "merge", cmd_merge, RUN_SETUP | NEED_WORK_TREE }, |
71dfbf22 | 370 | { "merge-base", cmd_merge_base, RUN_SETUP }, |
3668d423 | 371 | { "merge-file", cmd_merge_file, RUN_SETUP_GENTLY }, |
0ecace72 | 372 | { "merge-index", cmd_merge_index, RUN_SETUP }, |
a00a42ae | 373 | { "merge-ours", cmd_merge_ours, RUN_SETUP }, |
e1b3a2ca | 374 | { "merge-recursive", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE }, |
8cc5b290 AP |
375 | { "merge-recursive-ours", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE }, |
376 | { "merge-recursive-theirs", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE }, | |
1736855c | 377 | { "merge-subtree", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE }, |
907a7cb5 | 378 | { "merge-tree", cmd_merge_tree, RUN_SETUP }, |
112dd514 | 379 | { "mktag", cmd_mktag, RUN_SETUP }, |
633e3556 | 380 | { "mktree", cmd_mktree, RUN_SETUP }, |
7ae3df8c | 381 | { "mv", cmd_mv, RUN_SETUP | NEED_WORK_TREE }, |
efffea03 | 382 | { "name-rev", cmd_name_rev, RUN_SETUP }, |
cd067d3b | 383 | { "notes", cmd_notes, RUN_SETUP }, |
efffea03 | 384 | { "pack-objects", cmd_pack_objects, RUN_SETUP }, |
377d0276 | 385 | { "pack-redundant", cmd_pack_redundant, RUN_SETUP }, |
ef5dd8a9 | 386 | { "pack-refs", cmd_pack_refs, RUN_SETUP }, |
dedc0ec5 | 387 | { "patch-id", cmd_patch_id }, |
f0ef6a6e | 388 | { "peek-remote", cmd_ls_remote, RUN_SETUP_GENTLY }, |
d97bc5de | 389 | { "pickaxe", cmd_blame, RUN_SETUP }, |
efffea03 JH |
390 | { "prune", cmd_prune, RUN_SETUP }, |
391 | { "prune-packed", cmd_prune_packed, RUN_SETUP }, | |
102cb085 | 392 | { "push", cmd_push, RUN_SETUP }, |
efffea03 | 393 | { "read-tree", cmd_read_tree, RUN_SETUP }, |
be5908ae | 394 | { "receive-pack", cmd_receive_pack }, |
4264dc15 | 395 | { "reflog", cmd_reflog, RUN_SETUP }, |
211c8968 | 396 | { "remote", cmd_remote, RUN_SETUP }, |
7f3ecebf | 397 | { "remote-ext", cmd_remote_ext }, |
3a9ed4bd | 398 | { "remote-fd", cmd_remote_fd }, |
54b0c1e0 | 399 | { "replace", cmd_replace, RUN_SETUP }, |
6390c905 | 400 | { "repo-config", cmd_repo_config, RUN_SETUP_GENTLY }, |
658f3650 | 401 | { "rerere", cmd_rerere, RUN_SETUP }, |
0e5a7faa | 402 | { "reset", cmd_reset, RUN_SETUP }, |
efffea03 | 403 | { "rev-list", cmd_rev_list, RUN_SETUP }, |
5410a02a | 404 | { "rev-parse", cmd_rev_parse }, |
7ae3df8c | 405 | { "revert", cmd_revert, RUN_SETUP | NEED_WORK_TREE }, |
271bb087 | 406 | { "rm", cmd_rm, RUN_SETUP }, |
96249c04 | 407 | { "send-pack", cmd_send_pack, RUN_SETUP }, |
773b69bf | 408 | { "shortlog", cmd_shortlog, RUN_SETUP_GENTLY | USE_PAGER }, |
1fda91b5 | 409 | { "show", cmd_show, RUN_SETUP }, |
ef5dd8a9 NTND |
410 | { "show-branch", cmd_show_branch, RUN_SETUP }, |
411 | { "show-ref", cmd_show_ref, RUN_SETUP }, | |
412 | { "stage", cmd_add, RUN_SETUP | NEED_WORK_TREE }, | |
10762323 | 413 | { "status", cmd_status, RUN_SETUP | NEED_WORK_TREE }, |
f754fa9c | 414 | { "stripspace", cmd_stripspace }, |
efffea03 | 415 | { "symbolic-ref", cmd_symbolic_ref, RUN_SETUP }, |
62e09ce9 | 416 | { "tag", cmd_tag, RUN_SETUP }, |
9cb90b80 | 417 | { "tar-tree", cmd_tar_tree }, |
b5325818 | 418 | { "unpack-file", cmd_unpack_file, RUN_SETUP }, |
efffea03 JH |
419 | { "unpack-objects", cmd_unpack_objects, RUN_SETUP }, |
420 | { "update-index", cmd_update_index, RUN_SETUP }, | |
421 | { "update-ref", cmd_update_ref, RUN_SETUP }, | |
53a1116c | 422 | { "update-server-info", cmd_update_server_info, RUN_SETUP }, |
39345a21 | 423 | { "upload-archive", cmd_upload_archive }, |
1bc01efe | 424 | { "upload-archive--writer", cmd_upload_archive_writer }, |
2bc8c1a8 | 425 | { "var", cmd_var, RUN_SETUP_GENTLY }, |
ef5dd8a9 | 426 | { "verify-pack", cmd_verify_pack }, |
2ae68fcb | 427 | { "verify-tag", cmd_verify_tag, RUN_SETUP }, |
f754fa9c | 428 | { "version", cmd_version }, |
1fda91b5 | 429 | { "whatchanged", cmd_whatchanged, RUN_SETUP }, |
efffea03 | 430 | { "write-tree", cmd_write_tree, RUN_SETUP }, |
231af832 LT |
431 | }; |
432 | int i; | |
23326d14 JS |
433 | static const char ext[] = STRIP_EXTENSION; |
434 | ||
435 | if (sizeof(ext) > 1) { | |
436 | i = strlen(argv[0]) - strlen(ext); | |
437 | if (i > 0 && !strcmp(argv[0] + i, ext)) { | |
e8eec71d | 438 | char *argv0 = xstrdup(argv[0]); |
23326d14 JS |
439 | argv[0] = cmd = argv0; |
440 | argv0[i] = '\0'; | |
441 | } | |
442 | } | |
231af832 | 443 | |
1cd95087 LT |
444 | /* Turn "git cmd --help" into "git help cmd" */ |
445 | if (argc > 1 && !strcmp(argv[1], "--help")) { | |
446 | argv[1] = argv[0]; | |
447 | argv[0] = cmd = "help"; | |
448 | } | |
449 | ||
231af832 LT |
450 | for (i = 0; i < ARRAY_SIZE(commands); i++) { |
451 | struct cmd_struct *p = commands+i; | |
452 | if (strcmp(p->cmd, cmd)) | |
453 | continue; | |
f172f334 | 454 | exit(run_builtin(p, argc, argv)); |
231af832 LT |
455 | } |
456 | } | |
457 | ||
7550be0a JH |
458 | static void execv_dashed_external(const char **argv) |
459 | { | |
f285a2d7 | 460 | struct strbuf cmd = STRBUF_INIT; |
7550be0a | 461 | const char *tmp; |
d8e96fd8 | 462 | int status; |
7550be0a | 463 | |
92058e4d JK |
464 | if (use_pager == -1) |
465 | use_pager = check_pager_config(argv[0]); | |
030149a4 JN |
466 | commit_pager_choice(); |
467 | ||
7550be0a JH |
468 | strbuf_addf(&cmd, "git-%s", argv[0]); |
469 | ||
470 | /* | |
471 | * argv[0] must be the git command, but the argv array | |
472 | * belongs to the caller, and may be reused in | |
473 | * subsequent loop iterations. Save argv[0] and | |
474 | * restore it on error. | |
475 | */ | |
476 | tmp = argv[0]; | |
477 | argv[0] = cmd.buf; | |
478 | ||
479 | trace_argv_printf(argv, "trace: exec:"); | |
480 | ||
d8e96fd8 JK |
481 | /* |
482 | * if we fail because the command is not found, it is | |
483 | * OK to return. Otherwise, we just pass along the status code. | |
484 | */ | |
10c6cddd | 485 | status = run_command_v_opt(argv, RUN_SILENT_EXEC_FAILURE | RUN_CLEAN_ON_EXIT); |
0ac77ec3 | 486 | if (status >= 0 || errno != ENOENT) |
5709e036 | 487 | exit(status); |
7550be0a JH |
488 | |
489 | argv[0] = tmp; | |
490 | ||
491 | strbuf_release(&cmd); | |
492 | } | |
493 | ||
a907e1b6 AS |
494 | static int run_argv(int *argcp, const char ***argv) |
495 | { | |
496 | int done_alias = 0; | |
497 | ||
498 | while (1) { | |
499 | /* See if it's an internal command */ | |
500 | handle_internal_command(*argcp, *argv); | |
501 | ||
502 | /* .. then try the external ones */ | |
503 | execv_dashed_external(*argv); | |
504 | ||
505 | /* It could be an alias -- this works around the insanity | |
506 | * of overriding "git log" with "git show" by having | |
507 | * alias.log = show | |
508 | */ | |
509 | if (done_alias || !handle_alias(argcp, argv)) | |
510 | break; | |
511 | done_alias = 1; | |
512 | } | |
513 | ||
514 | return done_alias; | |
515 | } | |
516 | ||
7550be0a | 517 | |
84d32bf7 | 518 | int main(int argc, char **av) |
8e49d503 | 519 | { |
84d32bf7 | 520 | const char **argv = (const char **) av; |
4dd47c3b | 521 | const char *cmd; |
231af832 | 522 | |
e37c1329 NTND |
523 | startup_info = &git_startup_info; |
524 | ||
2cd72b0b SP |
525 | cmd = git_extract_argv0_path(argv[0]); |
526 | if (!cmd) | |
4dd47c3b | 527 | cmd = "git-help"; |
8e49d503 | 528 | |
a11c3964 TR |
529 | /* |
530 | * Always open file descriptors 0/1/2 to avoid clobbering files | |
531 | * in die(). It also avoids messing up when the pipes are dup'ed | |
532 | * onto stdin/stdout/stderr in the child processes we spawn. | |
533 | */ | |
534 | sanitize_stdfds(); | |
535 | ||
5e9637c6 ÆAB |
536 | git_setup_gettext(); |
537 | ||
231af832 LT |
538 | /* |
539 | * "git-xxxx" is the same as "git xxxx", but we obviously: | |
540 | * | |
541 | * - cannot take flags in between the "git" and the "xxxx". | |
542 | * - cannot execute it externally (since it would just do | |
543 | * the same thing over again) | |
544 | * | |
545 | * So we just directly call the internal command handler, and | |
546 | * die if that one cannot handle it. | |
547 | */ | |
cc44c765 | 548 | if (!prefixcmp(cmd, "git-")) { |
231af832 LT |
549 | cmd += 4; |
550 | argv[0] = cmd; | |
47d0b4ff | 551 | handle_internal_command(argc, argv); |
231af832 LT |
552 | die("cannot handle %s internally", cmd); |
553 | } | |
8e49d503 | 554 | |
231af832 | 555 | /* Look for flags.. */ |
6acbcb92 JS |
556 | argv++; |
557 | argc--; | |
4394efec | 558 | handle_options(&argv, &argc, NULL); |
6acbcb92 | 559 | if (argc > 0) { |
cc44c765 | 560 | if (!prefixcmp(argv[0], "--")) |
6acbcb92 JS |
561 | argv[0] += 2; |
562 | } else { | |
3d7e2d85 | 563 | /* The user didn't specify a command; give them help */ |
73e25e7c | 564 | commit_pager_choice(); |
3d7e2d85 SP |
565 | printf("usage: %s\n\n", git_usage_string); |
566 | list_common_cmds_help(); | |
421a9769 | 567 | printf("\n%s\n", _(git_more_info_string)); |
3d7e2d85 | 568 | exit(1); |
97fc6c5f | 569 | } |
6acbcb92 | 570 | cmd = argv[0]; |
231af832 LT |
571 | |
572 | /* | |
511707d4 | 573 | * We use PATH to find git commands, but we prepend some higher |
3ea3c215 | 574 | * precedence paths: the "--exec-path" option, the GIT_EXEC_PATH |
511707d4 SP |
575 | * environment, and the $(gitexecdir) from the Makefile at build |
576 | * time. | |
231af832 | 577 | */ |
e1464ca7 | 578 | setup_path(); |
8e49d503 | 579 | |
a025463b | 580 | while (1) { |
a907e1b6 AS |
581 | static int done_help = 0; |
582 | static int was_alias = 0; | |
583 | was_alias = run_argv(&argc, &argv); | |
584 | if (errno != ENOENT) | |
a025463b | 585 | break; |
a907e1b6 | 586 | if (was_alias) { |
f3dd015c | 587 | fprintf(stderr, "Expansion of alias '%s' failed; " |
7283bbc7 | 588 | "'%s' is not a git command\n", |
f3dd015c TT |
589 | cmd, argv[0]); |
590 | exit(1); | |
591 | } | |
a907e1b6 AS |
592 | if (!done_help) { |
593 | cmd = argv[0] = help_unknown_cmd(cmd); | |
594 | done_help = 1; | |
595 | } else | |
596 | break; | |
f3dd015c | 597 | } |
10b15b86 AR |
598 | |
599 | fprintf(stderr, "Failed to run command '%s': %s\n", | |
33ebb871 | 600 | cmd, strerror(errno)); |
8e49d503 AE |
601 | |
602 | return 1; | |
603 | } |