Skip to content

fix: update_from_template.sh fails on first run#156

Merged
blooop merged 2 commits intomainfrom
fix/update-from-template-script
Apr 25, 2026
Merged

fix: update_from_template.sh fails on first run#156
blooop merged 2 commits intomainfrom
fix/update-from-template-script

Conversation

@blooop
Copy link
Copy Markdown
Owner

@blooop blooop commented Apr 25, 2026

Summary

  • Remove git pull after git checkout -B — on a newly created branch with no upstream, git pull fails and set -eo pipefail kills the script before the template merge ever runs. This was the root cause of the template history not being merged into downstream repos.
  • Idempotent remote handling — remove the template remote before adding it, so re-runs after a crash don't fail at git remote add.
  • Guard git checkout --ours pixi.lock — suppress errors when there's no merge conflict on that file.
  • Extract template URL and branch name into variables.

Test plan

  • Create a new repo from the template (fresh "Initial commit", no shared history)
  • Run scripts/update_from_template.sh — should complete and produce a merge commit with --allow-unrelated-histories
  • Run the script a second time to verify idempotency

🤖 Generated with Claude Code

Summary by Sourcery

Fix the template update script so it reliably creates and updates the template merge branch, even on first run against a repo with no shared history.

Bug Fixes:

  • Prevent update_from_template.sh from failing on first run due to pulling on a new local branch without an upstream.
  • Ensure repeated runs of update_from_template.sh do not fail when the template remote already exists.
  • Avoid errors when updating pixi.lock if there is no merge conflict for that file.

Enhancements:

  • Introduce variables for the template repository URL and update branch name to simplify configuration and reuse.

Three bugs prevented the script from working correctly:

1. `git pull` after `git checkout -B` fails on a newly created branch
   with no upstream tracking, killing the script before the template
   merge ever executes (the critical bug).

2. `git remote add template` fails if the remote already exists from a
   previous crashed run, since cleanup at the end was never reached.

3. `git checkout --ours pixi.lock` errors when there is no merge
   conflict on that file.

Also extract the template URL and branch name into variables to reduce
duplication.
@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented Apr 25, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Makes the template update script robust on first and repeated runs by removing a failing pull on a new branch, making the template remote handling idempotent, guarding a conflict-resolution command, and extracting the template URL/branch into variables for reuse.

File-Level Changes

Change Details Files
Make template remote handling idempotent and configurable.
  • Introduce TEMPLATE_URL variable holding the template repository URL.
  • Remove any existing template remote before re‑adding it, ignoring errors.
  • Use the TEMPLATE_URL variable when adding the template remote.
scripts/update_from_template.sh
Fix branch creation/update flow so the script works on a fresh repository and is configurable.
  • Introduce BRANCH variable holding the update branch name.
  • Stop running git pull immediately after git checkout -B, avoiding failure on a new branch with no upstream.
  • Use the BRANCH variable when creating the branch and when pushing with --set-upstream.
scripts/update_from_template.sh
Harden merge and conflict-resolution steps to avoid spurious failures.
  • Leave the main-branch update flow intact (git checkout main && git pull origin main).
  • Guard git checkout --ours pixi.lock so it succeeds even when there is no conflict by redirecting errors and allowing failure.
  • Keep the merge from template/main with --allow-unrelated-histories and subsequent pixi.lock regeneration and commit logic unchanged.
scripts/update_from_template.sh

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

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

Hey - I've found 1 issue, and left some high level feedback:

  • Consider scoping git config pull.rebase false to the local repo (e.g. git config pull.rebase false) instead of using --global, to avoid surprising side effects on the developer's global Git configuration.
  • Since TEMPLATE_URL and BRANCH are now variables, you may want to allow overriding them via environment variables (e.g. ${TEMPLATE_URL:-default}) so the script can be reused with different templates/branches without editing the file.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider scoping `git config pull.rebase false` to the local repo (e.g. `git config pull.rebase false`) instead of using `--global`, to avoid surprising side effects on the developer's global Git configuration.
- Since `TEMPLATE_URL` and `BRANCH` are now variables, you may want to allow overriding them via environment variables (e.g. `${TEMPLATE_URL:-default}`) so the script can be reused with different templates/branches without editing the file.

## Individual Comments

### Comment 1
<location path="scripts/update_from_template.sh" line_range="7" />
<code_context>
+TEMPLATE_URL="https://github.com/blooop/python_template.git"
+BRANCH="feature/update_from_template"
+
 git config --global pull.rebase false
-git remote add template https://github.com/blooop/python_template.git
+git remote remove template 2>/dev/null || true
</code_context>
<issue_to_address>
**issue (bug_risk):** Avoid mutating the user’s global git config from this script

Changing `~/.gitconfig` from a project script can unexpectedly affect all of the user’s repositories. Please restrict this setting to the local repo (e.g. `git config pull.rebase false` without `--global`) or require users to configure it themselves instead of forcing a global change.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread scripts/update_from_template.sh Outdated
- Drop --global from `git config pull.rebase false` to avoid mutating
  the user's global git configuration.
- Use ${VAR:-default} for TEMPLATE_URL and BRANCH so they can be
  overridden via environment variables without editing the script.
@blooop blooop merged commit d873838 into main Apr 25, 2026
7 checks passed
@blooop blooop deleted the fix/update-from-template-script branch April 25, 2026 10:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant