@matheusbsbs / matheus-bagliano / commits / a87cd02ce0
Fork 0

Nicer output from 'git'

[jc: with suggestions by Jan-Benedict Glaw] Signed-off-by: Fredrik Kuivinen <freku045@student.liu.se> Signed-off-by: Junio C Hamano <junkio@cox.net>

Fredrik Kuivinen committed Mar 9, 2006 at 17:24 UTC a87cd02ce02e97083eb76eb8b9bfeb2e46800fd7
5 files changed +96 -18
.gitignore
+1
@@ -121,6 +121,7 @@ git-write-tree
121 git-core-*/?*
122 test-date
123 test-delta
124 +common-cmds.h
125 *.tar.gz
126 *.dsc
127 *.deb
Documentation/git.txt
+6 -5
@@ -20,15 +20,16 @@ brings your stuff to the plumbing).
20 OPTIONS
21 -------
22 --version::
23 - prints the git suite version that the 'git' program came from.
23 + Prints the git suite version that the 'git' program came from.
24
25 --help::
26 - prints the synopsis and a list of available commands.
27 - If a git command is named this option will bring up the
28 - man-page for that command.
26 + Prints the synopsis and a list of the most commonly used
27 + commands. If a git command is named this option will bring up
28 + the man-page for that command. If the option '--all' or '-a' is
29 + given then all available commands are printed.
30
31 --exec-path::
31 - path to wherever your core git programs are installed.
32 + Path to wherever your core git programs are installed.
33 This can also be controlled by setting the GIT_EXEC_PATH
34 environment variable. If no path is given 'git' will print
35 the current setting and then exit.
Makefile
+5 -2
@@ -452,10 +452,13 @@ all:
452 strip: $(PROGRAMS) git$X
453 $(STRIP) $(STRIP_OPTS) $(PROGRAMS) git$X
454
455 -git$X: git.c $(LIB_FILE)
455 +git$X: git.c common-cmds.h $(LIB_FILE)
456 $(CC) -DGIT_VERSION='"$(GIT_VERSION)"' \
457 $(ALL_CFLAGS) -o $@ $(filter %.c,$^) $(LIB_FILE) $(LIBS)
458
459 +common-cmds.h: Documentation/git-*.txt
460 + ./generate-cmdlist.sh > $@
461 +
462 $(patsubst %.sh,%,$(SCRIPT_SH)) : % : %.sh
463 rm -f $@
464 sed -e '1s|#!.*/sh|#!$(SHELL_PATH_SQ)|' \
@@ -612,7 +615,7 @@ rpm: dist
615 clean:
616 rm -f *.o mozilla-sha1/*.o arm/*.o ppc/*.o compat/*.o $(LIB_FILE)
617 rm -f $(ALL_PROGRAMS) git$X
615 - rm -f *.spec *.pyc *.pyo */*.pyc */*.pyo
618 + rm -f *.spec *.pyc *.pyo */*.pyc */*.pyo common-cmds.h
619 rm -rf $(GIT_TARNAME)
620 rm -f $(GIT_TARNAME).tar.gz git-core_$(GIT_VERSION)-*.tar.gz
621 $(MAKE) -C Documentation/ clean
generate-cmdlist.sh new
+48
@@ -0,0 +1,48 @@
1 +#!/bin/sh
2 +
3 +echo "/* Automatically generated by $0 */
4 +struct cmdname_help
5 +{
6 + char name[16];
7 + char help[64];
8 +};
9 +
10 +struct cmdname_help common_cmds[] = {"
11 +
12 +sort <<\EOF |
13 +add
14 +apply
15 +bisect
16 +branch
17 +checkout
18 +cherry-pick
19 +clone
20 +commit
21 +diff
22 +fetch
23 +grep
24 +init-db
25 +log
26 +merge
27 +mv
28 +prune
29 +pull
30 +push
31 +rebase
32 +reset
33 +revert
34 +rm
35 +show
36 +show-branch
37 +status
38 +tag
39 +verify-tag
40 +whatchanged
41 +EOF
42 +while read cmd
43 +do
44 + sed -n "/NAME/,/git-$cmd/H;
45 + \$ {x; s/.*git-$cmd - \\(.*\\)/ {\"$cmd\", \"\1\"},/; p}" \
46 + "Documentation/git-$cmd.txt"
47 +done
48 +echo "};"
git.c
+36 -11
@@ -11,6 +11,7 @@
11 #include <sys/ioctl.h>
12 #include "git-compat-util.h"
13 #include "exec_cmd.h"
14 +#include "common-cmds.h"
15
16 #include "cache.h"
17 #include "commit.h"
@@ -171,11 +172,29 @@ static void list_commands(const char *exec_path, const char *pattern)
172 putchar('\n');
173 }
174
175 +static void list_common_cmds_help()
176 +{
177 + int i, longest = 0;
178 +
179 + for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
180 + if (longest < strlen(common_cmds[i].name))
181 + longest = strlen(common_cmds[i].name);
182 + }
183 +
184 + puts("The most commonly used git commands are:");
185 + for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
186 + printf(" %s", common_cmds[i].name);
187 + mput_char(' ', longest - strlen(common_cmds[i].name) + 4);
188 + puts(common_cmds[i].help);
189 + }
190 + puts("(use 'git help -a' to get a list of all installed git commands)");
191 +}
192 +
193 #ifdef __GNUC__
175 -static void cmd_usage(const char *exec_path, const char *fmt, ...)
176 - __attribute__((__format__(__printf__, 2, 3), __noreturn__));
194 +static void cmd_usage(int show_all, const char *exec_path, const char *fmt, ...)
195 + __attribute__((__format__(__printf__, 3, 4), __noreturn__));
196 #endif
178 -static void cmd_usage(const char *exec_path, const char *fmt, ...)
197 +static void cmd_usage(int show_all, const char *exec_path, const char *fmt, ...)
198 {
199 if (fmt) {
200 va_list ap;
@@ -189,10 +208,13 @@ static void cmd_usage(const char *exec_path, const char *fmt, ...)
208 else
209 puts(git_usage);
210
192 - putchar('\n');
193 -
194 - if(exec_path)
195 - list_commands(exec_path, "git-*");
211 + if (exec_path) {
212 + putchar('\n');
213 + if (show_all)
214 + list_commands(exec_path, "git-*");
215 + else
216 + list_common_cmds_help();
217 + }
218
219 exit(1);
220 }
@@ -244,8 +266,11 @@ static int cmd_help(int argc, const char **argv, char **envp)
266 {
267 const char *help_cmd = argv[1];
268 if (!help_cmd)
247 - cmd_usage(git_exec_path(), NULL);
248 - show_man_page(help_cmd);
269 + cmd_usage(0, git_exec_path(), NULL);
270 + else if (!strcmp(help_cmd, "--all") || !strcmp(help_cmd, "-a"))
271 + cmd_usage(1, git_exec_path(), NULL);
272 + else
273 + show_man_page(help_cmd);
274 return 0;
275 }
276
@@ -418,7 +443,7 @@ int main(int argc, const char **argv, char **envp)
443 puts(git_exec_path());
444 exit(0);
445 }
421 - cmd_usage(NULL, NULL);
446 + cmd_usage(0, NULL, NULL);
447 }
448 argv[0] = cmd;
449
@@ -441,7 +466,7 @@ int main(int argc, const char **argv, char **envp)
466 execv_git_cmd(argv);
467
468 if (errno == ENOENT)
444 - cmd_usage(exec_path, "'%s' is not a git-command", cmd);
469 + cmd_usage(0, exec_path, "'%s' is not a git-command", cmd);
470
471 fprintf(stderr, "Failed to run command '%s': %s\n",
472 git_command, strerror(errno));