@matheusbsbs / matheus-bagliano / commits / 47d0b4ff57
Fork 0

Clean up internal command handling

This should change no code at all, it just moves the definition of "struct cmd_struct" out, and then splits out the running of the right command into the "run_command()" function. It also removes the long-unused 'envp' pointer passing. This is just preparation for adding some more error checking. Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Linus Torvalds committed Jun 24, 2007 at 10:10 UTC 47d0b4ff57f391786ed050f38c0de51462eda97a
1 file changed +30 -22
git.c
+30 -22
@@ -216,14 +216,34 @@ const char git_version_string[] = GIT_VERSION;
216 */
217 #define NOT_BARE (1<<2)
218
219 -static void handle_internal_command(int argc, const char **argv, char **envp)
219 +struct cmd_struct {
220 + const char *cmd;
221 + int (*fn)(int, const char **, const char *);
222 + int option;
223 +};
224 +
225 +static int run_command(struct cmd_struct *p, int argc, const char **argv)
226 +{
227 + const char *prefix;
228 +
229 + prefix = NULL;
230 + if (p->option & RUN_SETUP)
231 + prefix = setup_git_directory();
232 + if (p->option & USE_PAGER)
233 + setup_pager();
234 + if (p->option & NOT_BARE) {
235 + if (is_bare_repository() || is_inside_git_dir())
236 + die("%s must be run in a work tree", p->cmd);
237 + }
238 + trace_argv_printf(argv, argc, "trace: built-in: git");
239 +
240 + return p->fn(argc, argv, prefix);
241 +}
242 +
243 +static void handle_internal_command(int argc, const char **argv)
244 {
245 const char *cmd = argv[0];
222 - static struct cmd_struct {
223 - const char *cmd;
224 - int (*fn)(int, const char **, const char *);
225 - int option;
226 - } commands[] = {
246 + static struct cmd_struct commands[] = {
247 { "add", cmd_add, RUN_SETUP | NOT_BARE },
248 { "annotate", cmd_annotate, RUN_SETUP | USE_PAGER },
249 { "apply", cmd_apply },
@@ -307,25 +327,13 @@ static void handle_internal_command(int argc, const char **argv, char **envp)
327
328 for (i = 0; i < ARRAY_SIZE(commands); i++) {
329 struct cmd_struct *p = commands+i;
310 - const char *prefix;
330 if (strcmp(p->cmd, cmd))
331 continue;
313 -
314 - prefix = NULL;
315 - if (p->option & RUN_SETUP)
316 - prefix = setup_git_directory();
317 - if (p->option & USE_PAGER)
318 - setup_pager();
319 - if ((p->option & NOT_BARE) &&
320 - (is_bare_repository() || is_inside_git_dir()))
321 - die("%s must be run in a work tree", cmd);
322 - trace_argv_printf(argv, argc, "trace: built-in: git");
323 -
324 - exit(p->fn(argc, argv, prefix));
332 + exit(run_command(p, argc, argv));
333 }
334 }
335
328 -int main(int argc, const char **argv, char **envp)
336 +int main(int argc, const char **argv)
337 {
338 const char *cmd = argv[0] ? argv[0] : "git-help";
339 char *slash = strrchr(cmd, '/');
@@ -358,7 +366,7 @@ int main(int argc, const char **argv, char **envp)
366 if (!prefixcmp(cmd, "git-")) {
367 cmd += 4;
368 argv[0] = cmd;
361 - handle_internal_command(argc, argv, envp);
369 + handle_internal_command(argc, argv);
370 die("cannot handle %s internally", cmd);
371 }
372
@@ -390,7 +398,7 @@ int main(int argc, const char **argv, char **envp)
398
399 while (1) {
400 /* See if it's an internal command */
393 - handle_internal_command(argc, argv, envp);
401 + handle_internal_command(argc, argv);
402
403 /* .. then try the external ones */
404 execv_git_cmd(argv);