From 068b659da26dab7241d5c89df88185494700356c Mon Sep 17 00:00:00 2001
From: Ori Livneh <ori@wikimedia.org>
Date: Tue, 28 May 2013 10:37:03 -0700
Subject: [PATCH] Provide usage help even if not in Git directory

If a new user installs git-review and invokes it with no arguments (or with
'-h' / '--help') outside of a Git working tree, all she gets is a terse error
message that offers no usage help.

With this change, git-review behaves in the following manner when invoked
outside a git working tree:

* If a command-line argument is present and it is not '-h' or '--help', it is
  assumed that the user was attempting some action, and so git-review fails
  with the same error message as before.
* If no command-line arguments are present, or if there is a single
  command-line argument that is either '-h' or '--help', usage information is
  printed and the program exits.

This patch also adds myself to AUTHORS and fixes a small typo in README.rst.

Change-Id: I6fda72bf5311e74318b42e2860e7742e07b515de
---
 AUTHORS    |  1 +
 README.rst |  2 +-
 git-review | 31 ++++++++++++++++++-------------
 3 files changed, 20 insertions(+), 14 deletions(-)

diff --git a/AUTHORS b/AUTHORS
index d651860..e9d54cf 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -6,3 +6,4 @@ Roan Kattouw <roan.kattouw@gmail.com>
 Antoine Musso <hashar@free.fr>
 Yuriy Taraday <yorik.sar@gmail.com>
 Pavel Sedlák <psedlak@redhat.com>
+Ori Livneh <ori@wikimedia.org>
diff --git a/README.rst b/README.rst
index 9301713..817cb4b 100644
--- a/README.rst
+++ b/README.rst
@@ -130,4 +130,4 @@ Use ``git review`` to submit patches (after creating a gerrit account that links
 
     # Do your commits
     git review
-    # Enter your username if promped
+    # Enter your username if prompted
diff --git a/git-review b/git-review
index 39abcae..16f481a 100755
--- a/git-review
+++ b/git-review
@@ -401,7 +401,7 @@ def check_color_support():
     return _has_color
 
 
-def get_config(config_file):
+def get_config(config_file=None):
     """Generate the configuration map by starting with some built-in defaults
     and then loading GLOBAL_CONFIG, USER_CONFIG, and a repository-specific
     .gitreview file, if they exist. In case of conflict, the configuration file
@@ -409,7 +409,7 @@ def get_config(config_file):
     """
     config = DEFAULTS.copy()
     for filename in (GLOBAL_CONFIG, USER_CONFIG, config_file):
-        if os.path.exists(filename):
+        if filename is not None and os.path.exists(filename):
             config.update(load_config_file(filename))
     return config
 
@@ -959,15 +959,6 @@ to ensure proper behavior with gerrit. Thanks!
 
 
 def main():
-
-    (top_dir, git_dir) = git_directories()
-    config = get_config(os.path.join(top_dir, ".gitreview"))
-    hook_file = os.path.join(git_dir, "hooks", "commit-msg")
-
-    defaultrebase = convert_bool(
-        git_config_get_value("gitreview", "rebase",
-                             default=str(config['defaultrebase'])))
-
     usage = "git review [OPTIONS] ... [BRANCH]"
 
     import argparse
@@ -1064,7 +1055,20 @@ def main():
     parser.add_argument("--version", action="version",
                         version='%s version %s' %
                         (os.path.split(sys.argv[0])[-1], version))
-    parser.add_argument("branch", nargs="?", default=config['defaultbranch'])
+    parser.add_argument("branch", nargs="?")
+
+    try:
+        (top_dir, git_dir) = git_directories()
+    except GitDirectoriesException:
+        if sys.argv[1:] in ([], ['-h'], ['--help']):
+            parser.print_help()
+            sys.exit(1)
+        raise
+
+    config = get_config(os.path.join(top_dir, ".gitreview"))
+    defaultrebase = convert_bool(
+        git_config_get_value("gitreview", "rebase",
+                             default=str(config['defaultrebase'])))
     parser.set_defaults(dry=False,
                         draft=False,
                         rebase=defaultrebase,
@@ -1073,8 +1077,8 @@ def main():
                         setup=False,
                         list=False,
                         yes=False,
+                        branch=config['defaultbranch'],
                         remote=config['defaultremote'])
-
     options = parser.parse_args()
 
     if options.license:
@@ -1117,6 +1121,7 @@ def main():
     if options.custom_script:
         run_custom_script("pre")
 
+    hook_file = os.path.join(git_dir, "hooks", "commit-msg")
     have_hook = os.path.exists(hook_file) and os.access(hook_file, os.X_OK)
 
     if not have_hook: