Skip to content

fix(operations.git): fetch target branch when switching branches during git.repo operation - #2094

Open
goetzk wants to merge 2 commits into
pyinfra-dev:3.xfrom
medeopolis:shallow-branch-fetch
Open

goetzk wants to merge 2 commits into
pyinfra-dev:3.xfrom
medeopolis:shallow-branch-fetch

Conversation

@goetzk

@goetzk goetzk commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

When using shallow clones it is possible for git to skip fetching the correct branch data. This change attempts to implement the fix to enable that to succeed.

I'm not happy that the command now always tries to fetch from origin but don't know if thats an issue or not - I'd appreciate guidance on this one.

Closes #2088

  • Pull request is based on the default branch (3.x at this time)
  • Pull request includes tests for any new/updated operations/facts
  • [NA] Pull request includes documentation for any new/updated operations/facts
  • Tests pass (see scripts/dev-test.sh)
  • Type checking & code style passes (see scripts/dev-lint.sh)
  • Pull request title follows the

…xisting repo

When switching branches in an existing git.repo, fetch the target branch
from its tracking remote instead of hardcoded 'origin'. This fixes shallow
clone fetches where the target branch is not available locally.

Key changes:
- Fetch the target branch from the tracking remote before checkout
- Use the tracking remote for the current branch instead of 'origin'
- Add --depth and --force flags to fetch for shallow clones
- Add --tags flag when fetch_tags is enabled

Co-authored-by: Junie <junie@jetbrains.com>
@wowi42 wowi42 added bug Label for all kind of bugs. operations Issues with operations. labels Nov 20, 2026

@wowi42 wowi42 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The branch-switching fix is not ready yet. The shallow single-branch failure and remote selection issue are inline. Maintainer edits are also disabled on the fork.

git_commands.append(StringCommand("fetch", *fetch_options, *force_flag_list))
# Determine the remote tracking the current branch; fall back to
# "origin" if the config key is absent (e.g. detached HEAD).
tracking_remote = git_config.get(f"branch.{current_branch}.remote", ["origin"])[0]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

git fetch origin mybranch does not create refs/remotes/origin/mybranch after a shallow single-branch clone, so the following checkout still fails. Please create or use a checkoutable ref and add a real shallow-clone regression test. Also, branch.<current_branch>.remote can select a repository unrelated to src, while the operation reconciles and checks origin. Please fetch the target from the intended source.

tracking_remote = git_config.get(f"branch.{current_branch}.remote", ["origin"])[0]
git_commands.append(
StringCommand(
"fetch", *fetch_options, *force_flag_list, tracking_remote, QuoteString(branch)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tracking_remote is passed as a raw string to StringCommand. Repository config can therefore inject shell syntax. Please wrap it with QuoteString and add a quoting fixture.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Label for all kind of bugs. operations Issues with operations.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Git operation does not fetch new branches with shallow clone

2 participants