From: Jonathan Nieder <jrnieder@gmail.com>
To: git@vger.kernel.org
Cc: Gerfried Fuchs <rhonda@debian.at>, 462557@bugs.debian.org
Subject: [PATCH] Let 'git <command> -h' show usage without a git dir
Date: Sun, 8 Nov 2009 01:11:52 -0600 [thread overview]
Message-ID: <20091108071152.GA20741@progeny.tock> (raw)
In-Reply-To: <20080125173149.GA10287@edna.gwendoline.at>
Hi,
Gerfried Fuchs wrote:
> I really wonder why "git <command> -h" depends on being inside a
> repository. I noticed it with "git diff -h" (add, branch does that, too):
>
> #v+
> ~/git> git tag -h
> usage: git-tag [-n [<num>]] -l [<pattern>] | [-a | -s | -u <key-id>] [-f | -d | -v] [-m <msg> | -F <file>] <tagname> [<head>]
> ~/git> cd
> ~> git tag -h
> fatal: Not a git repository
> ~>
> #v-
This is a nuisance, I agree.
So how about something like this patch? This just avoids looking for
a .git directory if the only option to a subcommand is '-h'.
-- %< --
Subject: [PATCH] Let 'git <command> -h' show usage without a git dir
There is no need for "git <command> -h" to depend on being inside
a repository.
Reported by Gerfried Fuchs through http://bugs.debian.org/462557
Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
---
Tested with all builtins and non-builtins written in C. Some commands
do not show usage with '-h' and have been left unchanged.
git.c | 48 ++++++++++++++++++++++++++++++------------------
http-fetch.c | 13 ++++++++++++-
index-pack.c | 5 +++++
pack-redundant.c | 5 +++++
4 files changed, 52 insertions(+), 19 deletions(-)
diff --git a/git.c b/git.c
index bd2c5fe..bfa9518 100644
--- a/git.c
+++ b/git.c
@@ -220,6 +220,11 @@ const char git_version_string[] = GIT_VERSION;
* RUN_SETUP for reading from the configuration file.
*/
#define NEED_WORK_TREE (1<<2)
+/*
+ * Let RUN_SETUP, USE_PAGER, and NEED_WORK_TREE take effect even if
+ * passed the -h option.
+ */
+#define H_IS_NOT_HELP (1<<3)
struct cmd_struct {
const char *cmd;
@@ -229,21 +234,25 @@ struct cmd_struct {
static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
{
- int status;
+ int status, help;
struct stat st;
const char *prefix;
prefix = NULL;
- if (p->option & RUN_SETUP)
- prefix = setup_git_directory();
-
- if (use_pager == -1 && p->option & RUN_SETUP)
- use_pager = check_pager_config(p->cmd);
- if (use_pager == -1 && p->option & USE_PAGER)
- use_pager = 1;
+ help = argc == 2 && !(p->option & H_IS_NOT_HELP) &&
+ !strcmp(argv[1], "-h");
+ if (!help) {
+ if (p->option & RUN_SETUP && !help)
+ prefix = setup_git_directory();
+
+ if (use_pager == -1 && p->option & RUN_SETUP)
+ use_pager = check_pager_config(p->cmd);
+ if (use_pager == -1 && p->option & USE_PAGER)
+ use_pager = 1;
+ }
commit_pager_choice();
- if (p->option & NEED_WORK_TREE)
+ if (!help && p->option & NEED_WORK_TREE)
setup_work_tree();
trace_argv_printf(argv, "trace: built-in: git");
@@ -278,7 +287,8 @@ static void handle_internal_command(int argc, const char **argv)
{ "annotate", cmd_annotate, RUN_SETUP },
{ "apply", cmd_apply },
{ "archive", cmd_archive },
- { "bisect--helper", cmd_bisect__helper, RUN_SETUP | NEED_WORK_TREE },
+ { "bisect--helper", cmd_bisect__helper,
+ RUN_SETUP | NEED_WORK_TREE },
{ "blame", cmd_blame, RUN_SETUP },
{ "branch", cmd_branch, RUN_SETUP },
{ "bundle", cmd_bundle },
@@ -288,12 +298,12 @@ static void handle_internal_command(int argc, const char **argv)
RUN_SETUP | NEED_WORK_TREE},
{ "check-ref-format", cmd_check_ref_format },
{ "check-attr", cmd_check_attr, RUN_SETUP },
- { "cherry", cmd_cherry, RUN_SETUP },
+ { "cherry", cmd_cherry, RUN_SETUP | H_IS_NOT_HELP },
{ "cherry-pick", cmd_cherry_pick, RUN_SETUP | NEED_WORK_TREE },
{ "clone", cmd_clone },
{ "clean", cmd_clean, RUN_SETUP | NEED_WORK_TREE },
{ "commit", cmd_commit, RUN_SETUP | NEED_WORK_TREE },
- { "commit-tree", cmd_commit_tree, RUN_SETUP },
+ { "commit-tree", cmd_commit_tree, RUN_SETUP | H_IS_NOT_HELP },
{ "config", cmd_config },
{ "count-objects", cmd_count_objects, RUN_SETUP },
{ "describe", cmd_describe, RUN_SETUP },
@@ -304,7 +314,7 @@ static void handle_internal_command(int argc, const char **argv)
{ "fast-export", cmd_fast_export, RUN_SETUP },
{ "fetch", cmd_fetch, RUN_SETUP },
{ "fetch-pack", cmd_fetch_pack, RUN_SETUP },
- { "fetch--tool", cmd_fetch__tool, RUN_SETUP },
+ { "fetch--tool", cmd_fetch__tool, RUN_SETUP | H_IS_NOT_HELP },
{ "fmt-merge-msg", cmd_fmt_merge_msg, RUN_SETUP },
{ "for-each-ref", cmd_for_each_ref, RUN_SETUP },
{ "format-patch", cmd_format_patch, RUN_SETUP },
@@ -312,7 +322,7 @@ static void handle_internal_command(int argc, const char **argv)
{ "fsck-objects", cmd_fsck, RUN_SETUP },
{ "gc", cmd_gc, RUN_SETUP },
{ "get-tar-commit-id", cmd_get_tar_commit_id },
- { "grep", cmd_grep, RUN_SETUP | USE_PAGER },
+ { "grep", cmd_grep, RUN_SETUP | USE_PAGER | H_IS_NOT_HELP },
{ "help", cmd_help },
{ "init", cmd_init_db },
{ "init-db", cmd_init_db },
@@ -325,9 +335,11 @@ static void handle_internal_command(int argc, const char **argv)
{ "merge", cmd_merge, RUN_SETUP | NEED_WORK_TREE },
{ "merge-base", cmd_merge_base, RUN_SETUP },
{ "merge-file", cmd_merge_file },
- { "merge-ours", cmd_merge_ours, RUN_SETUP },
- { "merge-recursive", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
- { "merge-subtree", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
+ { "merge-ours", cmd_merge_ours, RUN_SETUP | H_IS_NOT_HELP },
+ { "merge-recursive", cmd_merge_recursive,
+ RUN_SETUP | NEED_WORK_TREE | H_IS_NOT_HELP },
+ { "merge-subtree", cmd_merge_recursive,
+ RUN_SETUP | NEED_WORK_TREE | H_IS_NOT_HELP },
{ "mktree", cmd_mktree, RUN_SETUP },
{ "mv", cmd_mv, RUN_SETUP | NEED_WORK_TREE },
{ "name-rev", cmd_name_rev, RUN_SETUP },
@@ -368,7 +380,7 @@ static void handle_internal_command(int argc, const char **argv)
{ "whatchanged", cmd_whatchanged, RUN_SETUP | USE_PAGER },
{ "write-tree", cmd_write_tree, RUN_SETUP },
{ "verify-pack", cmd_verify_pack },
- { "show-ref", cmd_show_ref, RUN_SETUP },
+ { "show-ref", cmd_show_ref, RUN_SETUP | H_IS_NOT_HELP },
{ "pack-refs", cmd_pack_refs, RUN_SETUP },
};
int i;
diff --git a/http-fetch.c b/http-fetch.c
index e8f44ba..85f5338 100644
--- a/http-fetch.c
+++ b/http-fetch.c
@@ -1,6 +1,10 @@
#include "cache.h"
+#include "exec_cmd.h"
#include "walker.h"
+static const char http_fetch_usage[] = "git http-fetch "
+ "[-c] [-t] [-a] [-v] [--recover] [-w ref] [--stdin] commit-id url";
+
int main(int argc, const char **argv)
{
const char *prefix;
@@ -19,6 +23,13 @@ int main(int argc, const char **argv)
int get_verbosely = 0;
int get_recover = 0;
+ git_extract_argv0_path(argv[0]);
+
+ if (argc == 2 && !strcmp(argv[1], "-h")) {
+ fprintf(stderr, "%s\n", http_fetch_usage);
+ return 0;
+ }
+
prefix = setup_git_directory();
git_config(git_default_config, NULL);
@@ -45,7 +56,7 @@ int main(int argc, const char **argv)
arg++;
}
if (argc < arg + 2 - commits_on_stdin) {
- usage("git http-fetch [-c] [-t] [-a] [-v] [--recover] [-w ref] [--stdin] commit-id url");
+ usage(http_fetch_usage);
return 1;
}
if (commits_on_stdin) {
diff --git a/index-pack.c b/index-pack.c
index b4f8278..4a7d405 100644
--- a/index-pack.c
+++ b/index-pack.c
@@ -882,6 +882,11 @@ int main(int argc, char **argv)
git_extract_argv0_path(argv[0]);
+ if (argc == 2 && !strcmp(argv[1], "-h")) {
+ fprintf(stderr, "usage: %s\n", index_pack_usage);
+ return 0;
+ }
+
/*
* We wish to read the repository's config file if any, and
* for that it is necessary to call setup_git_directory_gently().
diff --git a/pack-redundant.c b/pack-redundant.c
index 69a7ab2..24d59f9 100644
--- a/pack-redundant.c
+++ b/pack-redundant.c
@@ -603,6 +603,11 @@ int main(int argc, char **argv)
git_extract_argv0_path(argv[0]);
+ if (argc == 2 && !strcmp(argv[1], "-h")) {
+ fprintf(stderr, "usage: %s\n", pack_redundant_usage);
+ return 0;
+ }
+
setup_git_directory();
for (i = 1; i < argc; i++) {
--
1.6.5.2
next parent reply other threads:[~2009-11-08 7:02 UTC|newest]
Thread overview: 39+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20080125173149.GA10287@edna.gwendoline.at>
2009-11-08 7:11 ` Jonathan Nieder [this message]
2009-11-08 7:26 ` [PATCH] Show usage string for 'git http-push -h' Jonathan Nieder
2009-11-09 8:52 ` Tay Ray Chuan
2009-11-09 10:47 ` [PATCH v2] " Jonathan Nieder
2009-11-09 13:56 ` Tay Ray Chuan
2009-11-08 9:21 ` [PATCH] Let 'git <command> -h' show usage without a git dir Junio C Hamano
2009-11-08 11:03 ` Jonathan Nieder
2009-11-09 15:02 ` [PATCH 00/24] " Jonathan Nieder
2009-11-09 15:04 ` [PATCH 01/24] Retire fetch--tool helper to contrib/examples Jonathan Nieder
2009-11-09 15:04 ` [PATCH 02/24] Show usage string for 'git grep -h' Jonathan Nieder
2009-11-09 15:04 ` [PATCH 03/24] Show usage string for 'git cherry -h' Jonathan Nieder
2009-11-09 15:04 ` [PATCH 04/24] Show usage string for 'git commit-tree -h' Jonathan Nieder
2009-11-09 15:04 ` [PATCH 05/24] Show usage string for 'git merge-ours -h' Jonathan Nieder
2009-11-09 15:04 ` [PATCH/RFC 06/24] Show usage string for 'git show-ref -h' Jonathan Nieder
2009-11-09 15:04 ` [PATCH 07/24] check-ref-format: update usage string Jonathan Nieder
2009-11-10 20:11 ` Junio C Hamano
2009-11-09 15:04 ` [PATCH 08/24] Show usage string for 'git check-ref-format -h' Jonathan Nieder
2009-11-09 15:04 ` [PATCH 09/24] Show usage string for 'git fast-import -h' Jonathan Nieder
2009-11-09 15:04 ` [PATCH 10/24] Show usage string for 'git get-tar-commit-id -h' Jonathan Nieder
2009-11-09 15:04 ` [PATCH 11/24] Show usage string for 'git imap-send -h' Jonathan Nieder
2009-11-09 15:04 ` [PATCH 12/24] Show usage string for 'git mailsplit -h' Jonathan Nieder
2009-11-09 15:04 ` [PATCH 13/24] Show usage string for 'git merge-one-file -h' Jonathan Nieder
2009-11-09 15:04 ` [PATCH 14/24] Show usage string for 'git rev-parse -h' Jonathan Nieder
2009-11-09 15:04 ` [PATCH 15/24] Show usage string for 'git show-index -h' Jonathan Nieder
2009-11-09 15:04 ` [PATCH 16/24] Show usage string for 'git unpack-file -h' Jonathan Nieder
2009-11-09 15:04 ` [PATCH 17/24] Show usage string for 'git stripspace -h' Jonathan Nieder
2009-11-09 15:04 ` [PATCH 18/24] merge: do not setup worktree twice Jonathan Nieder
2009-11-10 20:11 ` Junio C Hamano
2009-11-11 1:58 ` Jonathan Nieder
2009-11-09 15:04 ` [PATCH 19/24] Let 'git http-fetch -h' show usage outside any git repository Jonathan Nieder
2009-11-09 15:05 ` [PATCH 20/24] http-fetch: add missing initialization of argv0_path Jonathan Nieder
2009-11-10 20:12 ` Junio C Hamano
2009-11-10 21:56 ` Johannes Sixt
2009-11-11 1:52 ` Jonathan Nieder
2009-11-09 15:05 ` [PATCH 21/24] Let 'git <command> -h' show usage without a git dir Jonathan Nieder
2009-11-09 15:05 ` [PATCH 22/24] Let usage() take a printf-style format Jonathan Nieder
2009-11-10 20:16 ` Junio C Hamano
2009-11-09 15:05 ` [PATCH 23/24] merge-{recursive,subtree}: use usage() to print usage Jonathan Nieder
2009-11-09 15:05 ` [PATCH 24/24] diff --no-index: make the usage string less scary Jonathan Nieder
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20091108071152.GA20741@progeny.tock \
--to=jrnieder@gmail.com \
--cc=462557@bugs.debian.org \
--cc=git@vger.kernel.org \
--cc=rhonda@debian.at \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).