Better determine git directories
- Call "git-rev-parse" only once, not twice
- In case where current directory is unreadable,
the current message is:
No '.gitreview' file found in this repository.
We don't know where your gerrit is. Please manually create
a remote named gerrit and try again.
(which is not true, because .gitreview *is* present)
After this change it fails a bit better:
Cannot determine where .git directory is
The following command failed with exit code 128
    "git rev-parse --show-toplevel --git-dir"
-----------------------
fatal: Unable to read current working directory: No such
file or directory
-----------------------
Change-Id: Idbb1e68b689f226d9434db131db5d9765752a315
			
			
This commit is contained in:
		
							
								
								
									
										32
									
								
								git-review
									
									
									
									
									
								
							
							
						
						
									
										32
									
								
								git-review
									
									
									
									
									
								
							@@ -163,10 +163,19 @@ def latest_is_newer():
 | 
				
			|||||||
    return False
 | 
					    return False
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def get_hooks_target_file():
 | 
					def git_directories():
 | 
				
			||||||
    top_dir = run_command('git rev-parse --git-dir')
 | 
					    """Determine (absolute git work directory path, .git subdirectory path)"""
 | 
				
			||||||
    hooks_dir = os.path.join(top_dir, "hooks")
 | 
					    cmd = ("git", "rev-parse", "--show-toplevel", "--git-dir")
 | 
				
			||||||
    return os.path.join(hooks_dir, "commit-msg")
 | 
					    out = run_command_exc(GitDirectoriesException, *cmd)
 | 
				
			||||||
 | 
					    try:
 | 
				
			||||||
 | 
					        return out.split()
 | 
				
			||||||
 | 
					    except ValueError:
 | 
				
			||||||
 | 
					        raise GitDirectoriesException(0, out, cmd, {})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class GitDirectoriesException(CommandFailed):
 | 
				
			||||||
 | 
					    "Cannot determine where .git directory is"
 | 
				
			||||||
 | 
					    EXIT_CODE = 70
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class CannotInstallHook(CommandFailed):
 | 
					class CannotInstallHook(CommandFailed):
 | 
				
			||||||
@@ -331,18 +340,17 @@ def check_color_support():
 | 
				
			|||||||
    return _has_color
 | 
					    return _has_color
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def get_config():
 | 
					def get_config(config_file):
 | 
				
			||||||
    """Get the configuration options set in the .gitremote file, if present."""
 | 
					    """Get the configuration options set in the .gitremote file, if present."""
 | 
				
			||||||
    """Returns a hashmap with hostname, port, project and defaultbranch."""
 | 
					    """Returns a hashmap with hostname, port, project and defaultbranch."""
 | 
				
			||||||
    """If there is no .gitremote file, default values will be used."""
 | 
					    """If there is no .gitremote file, default values will be used."""
 | 
				
			||||||
    config = dict(hostname=False, port='29418', project=False,
 | 
					    config = dict(hostname=False, port='29418', project=False,
 | 
				
			||||||
                  defaultbranch='master', defaultremote="gerrit",
 | 
					                  defaultbranch='master', defaultremote="gerrit",
 | 
				
			||||||
                  defaultrebase="1")
 | 
					                  defaultrebase="1")
 | 
				
			||||||
    top_dir = run_command('git rev-parse --show-toplevel')
 | 
					
 | 
				
			||||||
    target_file = os.path.join(top_dir, ".gitreview")
 | 
					    if os.path.exists(config_file):
 | 
				
			||||||
    if os.path.exists(target_file):
 | 
					 | 
				
			||||||
        configParser = ConfigParser.ConfigParser(config)
 | 
					        configParser = ConfigParser.ConfigParser(config)
 | 
				
			||||||
        configParser.read(target_file)
 | 
					        configParser.read(config_file)
 | 
				
			||||||
        config['hostname'] = configParser.get("gerrit", "host")
 | 
					        config['hostname'] = configParser.get("gerrit", "host")
 | 
				
			||||||
        config['port'] = configParser.get("gerrit", "port")
 | 
					        config['port'] = configParser.get("gerrit", "port")
 | 
				
			||||||
        config['project'] = configParser.get("gerrit", "project")
 | 
					        config['project'] = configParser.get("gerrit", "project")
 | 
				
			||||||
@@ -799,7 +807,9 @@ to ensure proper behavior with gerrit. Thanks!
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
def main():
 | 
					def main():
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    config = get_config()
 | 
					    (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")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    usage = "git review [OPTIONS] ... [BRANCH]"
 | 
					    usage = "git review [OPTIONS] ... [BRANCH]"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -931,8 +941,6 @@ def main():
 | 
				
			|||||||
        list_reviews(remote)
 | 
					        list_reviews(remote)
 | 
				
			||||||
        return
 | 
					        return
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    hook_file = get_hooks_target_file()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    have_hook = os.path.exists(hook_file) and os.access(hook_file, os.X_OK)
 | 
					    have_hook = os.path.exists(hook_file) and os.access(hook_file, os.X_OK)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if not have_hook:
 | 
					    if not have_hook:
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -195,6 +195,8 @@ status will be returned when there are merge conflicts with the current branch.
 | 
				
			|||||||
Possible reasons include an attempt to apply patchset from the different branch
 | 
					Possible reasons include an attempt to apply patchset from the different branch
 | 
				
			||||||
or code.  This exit status will also be returned if the patchset is already
 | 
					or code.  This exit status will also be returned if the patchset is already
 | 
				
			||||||
applied to the current branch.
 | 
					applied to the current branch.
 | 
				
			||||||
 | 
					.It 70
 | 
				
			||||||
 | 
					Cannot determine top level Git directory or .git subdirectory path.
 | 
				
			||||||
.El
 | 
					.El
 | 
				
			||||||
.Pp
 | 
					.Pp
 | 
				
			||||||
Exit status larger than 31 indicates problem with
 | 
					Exit status larger than 31 indicates problem with
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user