Skip to content
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

[bug] git.get_url_and_commit throws exception for different git configs #15261

Closed
Solviana opened this issue Dec 12, 2023 · 4 comments
Closed
Assignees
Milestone

Comments

@Solviana
Copy link
Contributor

Environment details

  • Operating System+version: Ubuntu 22.04
  • Compiler+version: n/a
  • Conan version: Found on 1.59.0, all versions are affected
  • Python version: 3.8.10

Steps to reproduce

1. Set git config "status.branch" to true:

git config --global status.branch true

2. Use Git.get_url_and_commit() in your conanfile:
`
from conan.tools.scm import Git

def export(self) -> None:
    git = Git(self, self.recipe_folder)
    scm_url, scm_commit = git.get_url_and_commit()   
   # Do something... 

3. Execute conan create. My example (copied from a python subprocess call):

'['conan', 'create', '-pr:b=ubuntu2004_x86_64_gcc8_debug', '-pr:h=ubuntu2004_x86_64_gcc8_debug', '-pr:h=cross_compiler_x86_64_linux_gcc8', '-pr:h=onep_bit', '/myrepo',
'0.20.2-alpha@user/dev'

Observed result (even though the repo is not dirty, I ran git clean -fd before):

Exporting package recipe
my_package/0.20.2-alpha@user/dev: Calling export()
ERROR: my_package/0.20.2-alpha@user/dev: Error in export() method, line 85
        scm_url, scm_commit = git.get_url_and_commit()
        ConanException: Repo is dirty, cannot capture url and commit: /myrepo

Logs

No response

@Solviana
Copy link
Contributor Author

Solviana commented Dec 12, 2023

The root cause is the is_dirty function inside conan.tools.scm.Git:

    def is_dirty(self):
        """
        Returns if the current folder is dirty, running ``git status -s``

        :return: True, if the current folder is dirty. Otherwise, False.
        """
        status = self.run("status -s").strip()
        return bool(status)

The problem is that the status.branch git config option will include the branch information in git status -s so the returned status will always contain at least one line. Because of this return bool(status) will always evalulate to True and this will raise an exception in get_url_and_commit

A solution would be to use git status --short --no-branch. The no-branch option takes precedence over git config status.branch so the function will perform as intended.

@conan-io/barbarians can you queue this? I'd like to raise a PR with the fix. Since I am already there I will consider all git status config entries to see if any problems like this can arise from other options.

@Solviana Solviana changed the title [bug] git [bug] git.get_url_and_commit throws for different git configs Dec 12, 2023
@Solviana Solviana changed the title [bug] git.get_url_and_commit throws for different git configs [bug] git.get_url_and_commit throws exception for different git configs Dec 12, 2023
@memsharded memsharded self-assigned this Dec 12, 2023
@memsharded
Copy link
Member

Hi @Solviana

Thanks very much for your report and your investigation.

It makes sense that there can be some scenarios and configurations that could make the is_dirty fail. It also makes sense to improve it so it works better and covers those use cases if possible. If you are willing to submit a PR, that would be great of course.

It seems the --short --no-branch should be relatively low risk. The main risk is that if it would be a very modern git feature, is this the case?

Solviana added a commit to Solviana/conan that referenced this issue Dec 13, 2023
@Solviana
Copy link
Contributor Author

@memsharded

Based on the Github Git mirror the status.branch was introduced in ec85d07, so the feature is at least 10 years old (Git 1.8.4). If this is acceptable for you I'd go ahead with the PR.

Going over the git-config documentation I'd include --untracked-files flag as well.

@memsharded
Copy link
Member

Sounds good, go for it, many thanks!

Solviana added a commit to Solviana/conan that referenced this issue Dec 13, 2023
Solviana added a commit to Solviana/conan that referenced this issue Dec 23, 2023
Solviana added a commit to Solviana/conan that referenced this issue Dec 23, 2023
Solviana added a commit to Solviana/conan that referenced this issue Dec 23, 2023
AbrilRBS pushed a commit that referenced this issue Dec 24, 2023
@AbrilRBS AbrilRBS added this to the 2.1 milestone Dec 25, 2023
memsharded pushed a commit to memsharded/conan that referenced this issue Dec 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants