Skip to content

git commit fails with hidden files #1757

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
vaskokj opened this issue Dec 4, 2023 · 3 comments
Closed

git commit fails with hidden files #1757

vaskokj opened this issue Dec 4, 2023 · 3 comments

Comments

@vaskokj
Copy link

vaskokj commented Dec 4, 2023

It seems that repo.git.commit("testing hidden file commit") fails with

stderr: 'error: pathspec 'testing hidden file commit' did not match any file(s) known to git' even though the file is properly added to be committed.

import git
repo = git.Repo("C:\\dev\myrepo.git")
repo.git.add("C:\\dev\\myrepo\\.hiddenfile")
repo.git.commit("testing hidden file commit")

Results in error

git.exc.GitCommandError: Cmd('git') failed due to: exit code(1)
  cmdline: git commit testing hidden file commit
  stderr: 'error: pathspec 'testing hidden file commit' did not match any file(s) known to git'

It seems like repo.git.add() doesn't work?

@vaskokj
Copy link
Author

vaskokj commented Dec 4, 2023

I think I found the issue...

repo.git.commit("testing hidden file commit") needs to be repo.git.commit("-m", "testing hidden file commit")

@EliahKagan
Copy link
Member

EliahKagan commented Dec 4, 2023

repo.git.commit("testing hidden file commit") needs to be repo.git.commit("-m", "testing hidden file commit")

Yes, or the option and its operand can be specified as a keyword argument, by either:

repo.git.commit(message="testing hidden file commit")
repo.git.commit(m='testing hidden file commit')

This is because calling repo.git.commit("testing hidden file commit") runs a command like

git commit 'testing hidden file commit'

without -m/--message, because methods on a Git object are dynamic ways of invoking the git executable with arguments synthesized directly from the arguments from the caller. The interpretation of arguments passed to those methods on a Git object is not specialized by git subcommand.

Omitting -m/--message causes the intended commit message instead to be an argument git-commit(1) documents as <pathspec>: it takes it to be a path and tries to commit only files under that path.

However, this shouldn't differ based on whether any files are hidden, nor otherwise based on their names. So if it does--that is, if the steps you had originally described had worked when the staged file's name did not start with .--then that would be a bug of its own.

In addition, if repo.git.add failed, it should have raised GitCommandError (since with_exceptions=False was not passed). So if it failed but gave no such indication, that would be a bug. But it sounds like that's not the case, if passing -m worked and the commit has the hidden file.

@Byron
Copy link
Member

Byron commented Dec 4, 2023

Closing, as the solution was found and further clarified in the comment above. Thanks everyone!

@Byron Byron closed this as completed Dec 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants