Skip to content

Commit a87cd02

Browse files
Fredrik KuivinenJunio C Hamano
authored andcommitted
Nicer output from 'git'
[jc: with suggestions by Jan-Benedict Glaw] Signed-off-by: Fredrik Kuivinen <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b4f2a6a commit a87cd02

File tree

5 files changed

+96
-18
lines changed

5 files changed

+96
-18
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ git-write-tree
121121
git-core-*/?*
122122
test-date
123123
test-delta
124+
common-cmds.h
124125
*.tar.gz
125126
*.dsc
126127
*.deb

Documentation/git.txt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,16 @@ brings your stuff to the plumbing).
2020
OPTIONS
2121
-------
2222
--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.
2424

2525
--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.
2930

3031
--exec-path::
31-
path to wherever your core git programs are installed.
32+
Path to wherever your core git programs are installed.
3233
This can also be controlled by setting the GIT_EXEC_PATH
3334
environment variable. If no path is given 'git' will print
3435
the current setting and then exit.

Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -452,10 +452,13 @@ all:
452452
strip: $(PROGRAMS) git$X
453453
$(STRIP) $(STRIP_OPTS) $(PROGRAMS) git$X
454454

455-
git$X: git.c $(LIB_FILE)
455+
git$X: git.c common-cmds.h $(LIB_FILE)
456456
$(CC) -DGIT_VERSION='"$(GIT_VERSION)"' \
457457
$(ALL_CFLAGS) -o $@ $(filter %.c,$^) $(LIB_FILE) $(LIBS)
458458

459+
common-cmds.h: Documentation/git-*.txt
460+
./generate-cmdlist.sh > $@
461+
459462
$(patsubst %.sh,%,$(SCRIPT_SH)) : % : %.sh
460463
rm -f $@
461464
sed -e '1s|#!.*/sh|#!$(SHELL_PATH_SQ)|' \
@@ -612,7 +615,7 @@ rpm: dist
612615
clean:
613616
rm -f *.o mozilla-sha1/*.o arm/*.o ppc/*.o compat/*.o $(LIB_FILE)
614617
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
616619
rm -rf $(GIT_TARNAME)
617620
rm -f $(GIT_TARNAME).tar.gz git-core_$(GIT_VERSION)-*.tar.gz
618621
$(MAKE) -C Documentation/ clean

generate-cmdlist.sh

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <sys/ioctl.h>
1212
#include "git-compat-util.h"
1313
#include "exec_cmd.h"
14+
#include "common-cmds.h"
1415

1516
#include "cache.h"
1617
#include "commit.h"
@@ -171,11 +172,29 @@ static void list_commands(const char *exec_path, const char *pattern)
171172
putchar('\n');
172173
}
173174

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+
174193
#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__));
177196
#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, ...)
179198
{
180199
if (fmt) {
181200
va_list ap;
@@ -189,10 +208,13 @@ static void cmd_usage(const char *exec_path, const char *fmt, ...)
189208
else
190209
puts(git_usage);
191210

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+
}
196218

197219
exit(1);
198220
}
@@ -244,8 +266,11 @@ static int cmd_help(int argc, const char **argv, char **envp)
244266
{
245267
const char *help_cmd = argv[1];
246268
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);
249274
return 0;
250275
}
251276

@@ -418,7 +443,7 @@ int main(int argc, const char **argv, char **envp)
418443
puts(git_exec_path());
419444
exit(0);
420445
}
421-
cmd_usage(NULL, NULL);
446+
cmd_usage(0, NULL, NULL);
422447
}
423448
argv[0] = cmd;
424449

@@ -441,7 +466,7 @@ int main(int argc, const char **argv, char **envp)
441466
execv_git_cmd(argv);
442467

443468
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);
445470

446471
fprintf(stderr, "Failed to run command '%s': %s\n",
447472
git_command, strerror(errno));

0 commit comments

Comments
 (0)