MestreLion/git-tools

Problem with default value of --git-dir parameter

Closed this issue · 3 comments

According to git-restore-mtime --git-dir parameter help, its default is <work-tree-root>/.git
In case running the utility not from a git work-tree and supplying --work-tree parameter, current logic still tries to find the git repository under the current dir instead of <work-tree-root>/.git
To fix this i changed the last line of parse_args() function from "return parser.parse_args()" to the following lines:

args = parser.parse_args()
if args.workdir and not args.gitdir:
    # set default git repository path
    args.gitdir = os.path.join(args.workdir, '.git')

return args

This seem to be a bug in git itself, as I'm using git --work-tree <work-tree-root> --git-dir <gitdir> rev-parse --show-toplevel --absolute-git-dir to get the repository workdir and gitdir absolute paths.

I'll investigate this. Thanks for reporting!

In any case, if this a bug in restore-mtime, I believe the fix is better suited in class Git.__init__(), not parse_args() or main()

It seems git is working as intended, according to the documentation on --git-dir and --work-tree, if --git-dir is not set, <gitdir> is auto-discovered from the current directory upwards, regardless of <worktree> path! So restore-mtime documentation is wrong, and could be improved to clarify this.

For completeness, this is how git handles its default paths in all scenarios:

  • If --git-dir (or $GIT_DIR) is set:
    • Auto-discovery is disabled, <gitdir> is set as specified.
    • <worktree> is assumed to be the current directory, unless told otherwise by --work-tree.
  • Otherwise:
    • <gitdir> is auto-discovered by looking for a .git/ subdir in current directory and recursively in any of its parent dirs.
    • The first directory found is assumed to be the worktree, unless told otherwise by --work-tree.

Likewise, from the point of view of --work-tree:

  • If --work-tree (or $GIT_WORK_TREE) is set:
    • <workdir> is set as specified.
    • Auto-discovery for <gitdir> is still performed as usual, unless told otherwise by --git-dir. Unlike what my documentation says, it is not set to <workdir>/.git.
  • Otherwise:
    • If <gitdir> was auto-discovered, <worktree> is set to its parent directory.
    • Otherwise, (i.e. <gitdir> was specified), <worktree> is set to the current directory.