74 lines
3.5 KiB
Bash
74 lines
3.5 KiB
Bash
#!/usr/bin/env bash
|
|||
|
|
# Fetches the latest commits from origin WITHOUT losing local work: the mutable revisions
|
||
|
|
# sitting on the old main are rebased on top of the fetched main@origin.
|
||
|
|
#
|
||
|
|
# - local work (mutable revisions between main@origin and @, including an empty working
|
||
|
|
# copy): rebased onto main@origin;
|
||
|
|
# - work sitting on a pushed revision not yet merged into main@origin (pending
|
||
|
|
# PR): left in place, with a warning (rebasing it would cut it off from that revision);
|
||
|
|
# - working copy already contained in main@origin: we start again from a new revision on top of it.
|
||
|
|
#
|
||
|
|
# JJ_BASE (default main@origin): the base to join.
|
||
|
|
set -euo pipefail
|
||
|
|
. "$(dirname "$0")/common.sh"
|
||
|
|
|
||
|
|
base="${JJ_BASE:-main@origin}"
|
||
|
|
|
||
|
|
info "→ Fetching the latest commits..."
|
||
|
|
jj git fetch
|
||
|
|
|
||
|
|
jj log -r "$base" --no-graph -T 'change_id' >/dev/null 2>&1 || {
|
||
|
|
err "✗ $base not found after the fetch"
|
||
|
|
exit 1
|
||
|
|
}
|
||
|
|
|
||
|
|
# Local work = mutable revisions between the base and @ that are NOT already published (reachable
|
||
|
|
# from a remote bookmark): we do not rewrite what origin knows. We only keep the roots
|
||
|
|
# whose parent is already in the base's history.
|
||
|
|
work="(mutable() & ($base..@)) ~ ::remote_bookmarks()"
|
||
|
|
|
||
|
|
# A local revision rewritten AFTER its push (describe, rebase…) then merged on the server side in its
|
||
|
|
# pushed version has a twin in $base: same change id, other commit, hence a divergent
|
||
|
|
# change. We never designate a revision by its change id (jj refuses a divergent
|
||
|
|
# identifier), and we do not rebase what is already merged:
|
||
|
|
# - same content as the merged twin: the local revision is abandoned (nothing is lost);
|
||
|
|
# - different content: we stop before rewriting anything, with the course of action to follow.
|
||
|
|
for c in $(jj log --no-graph -r "($work) & divergent()" -T 'commit_id ++ "\n"'); do
|
||
|
|
cid=$(jj log --no-graph -r "$c" -T 'change_id')
|
||
|
|
twin=$(jj log --no-graph -r "change_id($cid) & ::$base" -T 'commit_id ++ "\n"' | head -n 1)
|
||
|
|
[ -n "$twin" ] || continue
|
||
|
|
short=$(jj log --no-graph -r "$c" -T 'commit_id.short(12)')
|
||
|
|
if [ -z "$(jj interdiff --from "$twin" --to "$c" --summary)" ]; then
|
||
|
|
jj abandon "$c"
|
||
|
|
ok "✓ Revision $short already merged into $base (same content): abandoned"
|
||
|
|
else
|
||
|
|
err "✗ The local revision $short has already been merged into $base in another version"
|
||
|
|
err " (same change id, different content): nothing was rewritten."
|
||
|
|
err " See the difference: jj interdiff --from $twin --to $c"
|
||
|
|
err " Keep the merged version: jj abandon $c"
|
||
|
|
err " Keep the local version: jj metaedit --update-change-id $c, then rerun"
|
||
|
|
exit 1
|
||
|
|
fi
|
||
|
|
done
|
||
|
|
|
||
|
|
roots=$(jj log --no-graph -r "roots($work) & children(::$base)" -T 'commit_id ++ "\n"')
|
||
|
|
|
||
|
|
if [ -n "$roots" ]; then
|
||
|
|
args=()
|
||
|
|
for r in $roots; do args+=(-s "$r"); done
|
||
|
|
jj rebase "${args[@]}" -d "$base"
|
||
|
|
ok "✓ Local work rebased onto $base"
|
||
|
|
elif [ -n "$(jj log --no-graph -r "@ & ::$base" -T 'change_id')" ]; then
|
||
|
|
# The working copy is already part of the base: nothing to keep.
|
||
|
|
jj new "$base"
|
||
|
|
ok "✓ New revision on $base"
|
||
|
|
elif [ -n "$(jj log --no-graph -r "($base..@) & (::remote_bookmarks() | immutable())" -T 'change_id')" ]; then
|
||
|
|
warn "⚠ The work rests on pushed revisions not merged into $base (pending PR):"
|
||
|
|
warn " left in place. Merge the PR then rerun, if needed."
|
||
|
|
else
|
||
|
|
ok "✓ Already up to date on $base"
|
||
|
|
fi
|
||
|
|
|
||
|
|
if [ -n "$(jj log --no-graph -r 'conflicts() & ::@ & mutable()' -T 'change_id' 2>/dev/null)" ]; then
|
||
|
|
warn "⚠ Conflicts after the rebase: jj st, then jj resolve."
|
||
|
|
fi
|