conan: [bug] Conan Git helper makes local repo commit grafted

Describe the bug

OS: Linux (WSL) Git: 2.34.1 Python: 3.10.12 Conan: 2.16.1

How to reproduce it

When running conan export with the Git credential helper and coordinates_to_conandata as described here: https://docs.conan.io/2/examples/tools/scm/git/capture_scm/git_capture_scm.html#examples-tools-scm-git-capture, the local repository (not the conan cache) can be polluted. Specifically, the issue I have found occurs when a git commit that is checked out is available in the origin remote, but is not included in a branch or tag. In this instance, the git fetch --dry-run --depth=1 command will make the current commit grafted and remove all the parent commits from it.

As an example, clone this repo and follow the instructions in README.md: https://github.com/aander80/conan-grafted. You should see a grafted commit which has its history lost after running all the commands.

Diving into the code a bit, it is this line in particular that causes the issue: https://github.com/conan-io/conan/blob/2.16.1/conan/tools/scm/git.py#L117C13-L117C79. The commit is not found in the branch -r --contains command a few lines above it.

This is a big issue for us, since we use Gerrit which will not include commits in any references in pre-merge changes, but still has fetchable commit hashes for all pre-merge commits.

I have tried using ls-remote instead but that did not work. Perhaps .git/ could be patched after the fetch --dry-run --depth=1 command is run?

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 17 (10 by maintainers)

Most upvoted comments

Thanks for testing it! https://github.com/conan-io/conan/pull/18315 has been merged, this will be in next Conan 2.17

I think I found an alternative, adding the fetch {remote} --refetch argument to force checking in the server

I tried this in my demo repo and that seems to still work! I also tried manually making the commit shallow and run with --refetch to see that the commit is not unshallowed, and that still seems to work!

The change was breaking other behavior and tests that relied on that --depth=1, to detect if the commits were in the server:

        try:
            # This will raise if commit not present.
            self.run(f"fetch {remote} --dry-run {commit}")
            return True
        except (Exception,):
            # Don't raise an error because the fetch could fail for many more reasons than the branch.
            return False

because that was not raising anymore. I think I found an alternative, adding the fetch {remote} --refetch argument to force checking in the server

Sounds good, lets remove the depth=1 then, I think this shouldn’t have a dramatic impact on performance, isn’t it?

I tested it out and it looks like dropping the depth=1 still works for my case. I would be okay removing it.