@matheusbsbs / matheus-bagliano / commits / 822a7d5071
Fork 0

Remove cmd_usage() routine and re-organize the help/usage code.

The cmd_usage() routine was causing warning messages due to a NULL format parameter being passed in three out of four calls. This is a problem if you want to compile with -Werror. A simple solution is to simply remove the GNU __attribute__ format pragma from the cmd_usage() declaration in the header file. The function interface was somewhat muddled anyway, so re-write the code to finesse the problem. [jc: this incidentally revealed that t9100 test assumed that the output from "git help" to be fixed in stone, but this patch lower-cases "Usage" to "usage". Update the test not to rely on "git help" output.] Signed-off-by: Ramsay Allan Jones <ramsay@ramsay1.demon.co.uk> Signed-off-by: Junio C Hamano <junkio@cox.net>

Ramsay Allan Jones committed Jul 30, 2006 at 22:42 UTC 822a7d507151e1d6310f52d5b05234d65db11a88
4 files changed +33 -42
builtin-help.c
+23 -31
@@ -9,8 +9,6 @@
9 #include "exec_cmd.h"
10 #include "common-cmds.h"
11
12 -static const char git_usage[] =
13 - "Usage: git [--version] [--exec-path[=GIT_EXEC_PATH]] [--help] COMMAND [ ARGS ]";
12
13 /* most GUI terminals set COLUMNS (although some don't export it) */
14 static int term_columns(void)
@@ -178,31 +176,6 @@ static void list_common_cmds_help(void)
176 puts("(use 'git help -a' to get a list of all installed git commands)");
177 }
178
181 -void cmd_usage(int show_all, const char *exec_path, const char *fmt, ...)
182 -{
183 - if (fmt) {
184 - va_list ap;
185 -
186 - va_start(ap, fmt);
187 - printf("git: ");
188 - vprintf(fmt, ap);
189 - va_end(ap);
190 - putchar('\n');
191 - }
192 - else
193 - puts(git_usage);
194 -
195 - if (exec_path) {
196 - putchar('\n');
197 - if (show_all)
198 - list_commands(exec_path, "git-*");
199 - else
200 - list_common_cmds_help();
201 - }
202 -
203 - exit(1);
204 -}
205 -
179 static void show_man_page(const char *git_cmd)
180 {
181 const char *page;
@@ -221,6 +194,13 @@ static void show_man_page(const char *git_cmd)
194 execlp("man", "man", page, NULL);
195 }
196
197 +void help_unknown_cmd(const char *cmd)
198 +{
199 + printf("git: '%s' is not a git-command\n\n", cmd);
200 + list_common_cmds_help();
201 + exit(1);
202 +}
203 +
204 int cmd_version(int argc, const char **argv, const char *prefix)
205 {
206 printf("git version %s\n", git_version_string);
@@ -230,12 +210,24 @@ int cmd_version(int argc, const char **argv, const char *prefix)
210 int cmd_help(int argc, const char **argv, const char *prefix)
211 {
212 const char *help_cmd = argc > 1 ? argv[1] : NULL;
233 - if (!help_cmd)
234 - cmd_usage(0, git_exec_path(), NULL);
235 - else if (!strcmp(help_cmd, "--all") || !strcmp(help_cmd, "-a"))
236 - cmd_usage(1, git_exec_path(), NULL);
213 + const char *exec_path = git_exec_path();
214 +
215 + if (!help_cmd) {
216 + printf("usage: %s\n\n", git_usage_string);
217 + list_common_cmds_help();
218 + exit(1);
219 + }
220 +
221 + else if (!strcmp(help_cmd, "--all") || !strcmp(help_cmd, "-a")) {
222 + printf("usage: %s\n\n", git_usage_string);
223 + if(exec_path)
224 + list_commands(exec_path, "git-*");
225 + exit(1);
226 + }
227 +
228 else
229 show_man_page(help_cmd);
230 +
231 return 0;
232 }
233
builtin.h
+2 -5
@@ -5,12 +5,9 @@
5 #include <limits.h>
6
7 extern const char git_version_string[];
8 +extern const char git_usage_string[];
9
9 -void cmd_usage(int show_all, const char *exec_path, const char *fmt, ...)
10 -#ifdef __GNUC__
11 - __attribute__((__format__(__printf__, 3, 4), __noreturn__))
12 -#endif
13 - ;
10 +extern void help_unknown_cmd(const char *cmd);
11
12 extern int cmd_help(int argc, const char **argv, const char *prefix);
13 extern int cmd_version(int argc, const char **argv, const char *prefix);
git.c
+5 -2
@@ -15,6 +15,9 @@
15
16 #include "builtin.h"
17
18 +const char git_usage_string[] =
19 + "git [--version] [--exec-path[=GIT_EXEC_PATH]] [--help] COMMAND [ ARGS ]";
20 +
21 static void prepend_to_path(const char *dir, int len)
22 {
23 const char *old_path = getenv("PATH");
@@ -78,7 +81,7 @@ static int handle_options(const char*** argv, int* argc)
81 setenv("GIT_DIR", getcwd(git_dir, 1024), 1);
82 } else {
83 fprintf(stderr, "Unknown option: %s\n", cmd);
81 - cmd_usage(0, NULL, NULL);
84 + usage(git_usage_string);
85 }
86
87 (*argv)++;
@@ -375,7 +378,7 @@ int main(int argc, const char **argv, char **envp)
378 }
379
380 if (errno == ENOENT)
378 - cmd_usage(0, exec_path, "'%s' is not a git-command", cmd);
381 + help_unknown_cmd(cmd);
382
383 fprintf(stderr, "Failed to run command '%s': %s\n",
384 cmd, strerror(errno));
t/t9100-git-svn-basic.sh
+3 -4
@@ -170,7 +170,7 @@ then
170 test -L $SVN_TREE/exec-2.sh"
171
172 name='modify a symlink to become a file'
173 - git help > help || true
173 + echo git help > help || true
174 rm exec-2.sh
175 cp help exec-2.sh
176 git update-index exec-2.sh
@@ -217,10 +217,10 @@ name='check imported tree checksums expected tree checksums'
217 rm -f expected
218 if test "$have_utf8" = t
219 then
220 - echo tree f735671b89a7eb30cab1d8597de35bd4271ab813 > expected
220 + echo tree bf522353586b1b883488f2bc73dab0d9f774b9a9 > expected
221 fi
222 cat >> expected <<\EOF
223 -tree 4b9af72bb861eaed053854ec502cf7df72618f0f
223 +tree 83654bb36f019ae4fe77a0171f81075972087624
224 tree 031b8d557afc6fea52894eaebb45bec52f1ba6d1
225 tree 0b094cbff17168f24c302e297f55bfac65eb8bd3
226 tree d667270a1f7b109f5eb3aaea21ede14b56bfdd6e
@@ -231,4 +231,3 @@ EOF
231 test_expect_success "$name" "diff -u a expected"
232
233 test_done
234 -