Skip to content

Handle stale inventory git conflicts and add clean-reset action#12

Merged
skokec merged 1 commit into
masterfrom
codex/handle-git-merge-conflicts-automatically
Jun 15, 2026
Merged

Handle stale inventory git conflicts and add clean-reset action#12
skokec merged 1 commit into
masterfrom
codex/handle-git-merge-conflicts-automatically

Conversation

@skokec

@skokec skokec commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

Motivation

  • The UI save flow could fail with merge/rebase conflicts when the remote inventory changed between fetch and user commit, producing errors like Patch failed or Could not pull with rebase.
  • The intent is to attempt automatic synchronization and patch application where possible and provide a safe, user-confirmed fallback that discards local inventory changes when automatic recovery is not possible.

Description

  • Make commit.sh more robust by using an inventory_dir variable, adding set -u, a cleanup trap, and a reset_worktree helper that aborts in-progress operations and hard-resets the worktree.
  • Synchronize with the upstream before applying patches by fetching/rebasing the branch when an upstream is configured and then use git apply --3way to handle non-conflicting remote edits automatically; on failure the script shows conflict output and performs a safe reset.
  • Add clean_inventory.sh which aborts rebase/merge, fetches the remote, hard-resets the local branch to the upstream (or origin/$CCC_INVENTORY_BRANCH), and removes untracked files to provide a deterministic clean state.
  • Update the Streamlit dialog in commit.py to surface a clearer conflict message, persist command output in st.session_state for display, and offer a confirmed Clean git clone / discard local inventory changes action that runs the new clean_inventory.sh.

Testing

  • PYTHONDONTWRITEBYTECODE=1 python3 -m py_compile commit.py completed successfully.
  • bash -n commit.sh clean_inventory.sh (shell syntax check) completed successfully.
  • python3 -m unittest test_logs.py ran 9 tests and reported OK.

Codex Task

Copilot AI review requested due to automatic review settings June 15, 2026 09:27
@skokec skokec merged commit f0b7bfc into master Jun 15, 2026
1 check passed
@skokec skokec deleted the codex/handle-git-merge-conflicts-automatically branch June 15, 2026 09:28

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR hardens the inventory “save/commit” workflow against stale-clone git conflicts by syncing with the upstream before applying UI-generated patches, and adds a user-confirmed fallback to reset the inventory repository to a clean remote state when automatic recovery isn’t possible.

Changes:

  • Updated commit.sh to synchronize with upstream and apply patches using git apply --3way, with added cleanup/reset helpers.
  • Added clean_inventory.sh to abort in-progress operations and hard-reset/clean the inventory repo back to the remote branch.
  • Updated commit.py to persist/show conflict output and offer a confirmed “clean reset” action from the UI.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 7 comments.

File Description
commit.sh Adds upstream sync + 3-way patch apply and more robust cleanup/reset logic during failures.
commit.py Improves conflict UX by persisting command output and adding a clean-reset action that runs the new script.
clean_inventory.sh New script to force-reset the inventory repo to a deterministic clean state from remote.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread commit.sh
Comment on lines +23 to +27
if [[ -e "$lockfile" ]]; then
error 'Could not acquire lock. Try again.'
fi
touch $lockfile
touch "$lockfile"
trap cleanup EXIT
Comment thread commit.sh
Comment on lines 29 to 33
patchfile=$1
sed -e 's/\x1b\[[0-9;]*m//g' -i $patchfile
sed -e 's/\x1b\[[0-9;]*m//g' -i "$patchfile"

if [[ -z $2 ]]; then
if [[ -z ${2:-} ]]; then
error 'Commit message not provided'
Comment thread commit.sh
Comment on lines +17 to +21
function reset_worktree() {
git rebase --abort >/dev/null 2>&1 || true
git merge --abort >/dev/null 2>&1 || true
git reset --hard HEAD >/dev/null 2>&1 || true
}
Comment thread commit.sh
Comment on lines 55 to +56
git add inventory/group_vars/ccc-cluster/{user-list,user-containers}.yml
git commit -m "$2"
git commit -m "$2" || error 'Commit failed'
Comment thread commit.py
Comment on lines +68 to +72
p = subprocess.run(['bash', 'clean_inventory.sh'], capture_output=True)
if p.returncode == 0:
discard()
st.rerun()

Comment thread clean_inventory.sh
Comment on lines +4 to +21
lockfile=/opt/ccc-manager/commit.lock
inventory_dir=${CCC_INVENTORY_DIR:-/opt/ccc-inventory}

function cleanup() {
rm -f "$lockfile"
}

function error() {
echo "$1"
cleanup
exit 1
}

if [[ -e "$lockfile" ]]; then
error 'Could not acquire lock. Try again.'
fi
touch "$lockfile"
trap cleanup EXIT
Comment thread commit.py
load_containers(st.session_state)

def clean_inventory():
p = subprocess.run(['bash', 'clean_inventory.sh'], capture_output=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants