Closed
Description
This change in 2.0.9
is a bit problematic:
- Even if a
path
is explicitly provided to the constructor, theGIT_DIR
environment variable is used instead - For things like git hooks,
GIT_DIR
is set to.git
by default. This will result inself.git_dir
being set to.git
(os.path.abspath
instead ofos.path.normpath
would fix this). Consequently, subsequent commands fails (eg:repo.git.status()
)
Metadata
Metadata
Assignees
Type
Projects
Relationships
Development
No branches or pull requests
Activity
ankostis commentedon Oct 17, 2016
That should be the expected behavior, env-variables taking precedence over code, correct?
Can you clarify a bit on that?
Who sets that by default?
By looking at the code I cannot find gitpython doing such a thing before invoking a hook - actually nowhere else the
GIT_DIR
var is modified, it is used as a read-only variable.ethereon commentedon Oct 17, 2016
I'm not so sure. Code is more explicit than an implicit override by an environment variable. Also, with the env var taking precedence over code, you can't do things like have multiple Repo instances with different paths.
git
sets this. For instance, if you have a pre-commit hook at[your-repo]/.git/hooks/pre-commit
.The following pre-commit script would have previously worked on
2.0.8
but not2.0.9
:cheshirekow commentedon Oct 17, 2016
Here's a demonstration of
GIT_DIR
being set bygit
during apre-commit
hook.git version 2.10.1
.FIX gitpython-developers#535: expand also GIT_DIR var on Repo-construct
ankostis commentedon Oct 19, 2016
Indeed 26bb778 introduced a bug in the
Repo.__init__()
, because the expansion of the path happens earlier, before the use of theGIT_DIR
variable.FIX gitpython-developers#535: expand also GIT_DIR var on Repo-construct
ankostis commentedon Oct 19, 2016
Concerning @cheshirekow useful example, it demonstrates that the
git
command setsGIT_DIR
, so any hook-scripts relying on the gitpython library, indeed, would no work (until #537 is merged).But there is another issue here:
git 2.10.1
cmd set 7 env-vars for executing hook-commits:the gitpython library sets [edit]probably only 2 env-vars:
The git-docs are not explicit on that, as confirmed by many sources[1][2][3].
Is that a bug, and should gitpython replicate
git
's behavior for hook env-vars?Or we will be hunting a moving target?
(@Byron any input on that?)
[1] https://longair.net/blog/2011/04/09/missing-git-hooks-documentation/
[2] https://www.digitalocean.com/community/tutorials/how-to-use-git-hooks-to-automate-development-and-deployment-tasks
[3] http://stackoverflow.com/questions/5531309/reset-hard-on-git-push
ankostis commentedon Oct 20, 2016
@ethereon on #537 wrote:
ankostis commentedon Oct 20, 2016
I have a conflicting gut-feeling there...
The general idea for the priority of the configuration params is borrowed from the way command-line tools configure themselves; usually there is a list of configuration sources (rough description follows):
So based on the list above,
5. env-vars
override2. source-code kwds
which is at the bottom.BUT gitpython is a not a Cmd-line tool but a Library, and function arguments (
1.
&2.
) can be though of as the equivalent of the6. cmd-line args
, so they must have the top-most priority, and what @ethereon asks makes sense. [edit:] That becomes even more obvious with interactive python-REPL, as it is hinted by ethereon's latest example.Taking a strict stance on that would mean that env-vars (such as
GIT_DIR
) are not expanded by the the library-code, and they should be left for the end-user to decide. Unfortunately there are places where this decision (for the the var-expansion and override) happens internally, in code-paths that are not under the direct control of the end-user, so this "strict stance" is too idealistic to be functioning.Any ideas on this?
[-]Issue with how Repo path is set in 2.0.9[/-][+]Use of GIT_REPO env-var for setting Repo path in 2.0.9[/+]Byron commentedon Oct 22, 2016
@ethereon Thanks for all the effort you put into this issue ! Personally I believe that the current behaviour is unintuitive, as the path specifically provided of course has the final say. Ideally, GitPython would not have started adding so much magic as to expand
GIT_DIR
itself internally, maybe it has gotten off-track. Also I am unsure which intend the offending commit might have had, and I would be fine usingGIT_DIR
only if path isNone
, if that is needed to keep things in working order.@ankostis regarding the env-vars that are set when GitPython executes hooks: Maybe this is another instance of GitPython getting into its own way as it now has to chase a flying, and in this case, undocumented bullet. However, to me it looks like another issue that should probably be addressed independently of this one if there is any need.
fix(repo): Use GIT_DIR only if no repo-path given
ankostis commentedon Oct 22, 2016
Done, with a slight twist: env-var used if path
is None
or empty.Of course; and is not urgent at all.
I would say that this depends on situation:
The solution you both described is respecting both cases, and have implemented in 5fac8d4.
Merge pull request #537 from ankostis/exp_git_dir