Re: [PATCH] checkout: implement "-" shortcut name for last branch
From: Thomas Rast <hidden>
Date: 2016-06-15 22:45:58
Johannes Schindelin wrote:
- AFAICT your version could never be convinced to resurrect deleted branches, without resorting to reflogs anyway.
Speaking of resurrection, there are other possible sources that a branch tip could be gleaned from. How about the script below? The advantage is that it can even be used to recover Junio's topic branches by looking at the merges in 'pu'. (I'll answer the rest later.)
--- 8< ---#!/bin/sh
. git-sh-setup
USAGE="<branch>"
test "$#" = 1 || usage
branch="$1"
candidates=
search_reflog () {
next=
git reflog show HEAD |
while read sha ref msg; do
if test -n "$next"; then
next=
echo ${sha%...}
fi
if echo "$msg" | grep -q "^checkout: moving from $branch "; then
next=t
fi
if echo "$msg" | grep -q "^merge $branch:"; then
git rev-list --parents -1 ${sha%...} \
| cut -d' ' -f3
fi
done
}
search_merges () {
git rev-list --pretty=tformat:"%h %p:%s" --all |
grep "Merge branch.*'$branch'.*into" |
while read sha rest; do
parents="$(echo "$rest" | cut -d: -f1)"
case "$parents" in
*' '*' '*)
warn "$branch took part in octopus merge $sha"
warn "check manually!"
;;
*' '*)
echo "$parents" | cut -d' ' -f2
;;
esac
done
}
search_merge_targets () {
git rev-list --pretty=tformat:"%h %s" --all |
grep "Merge branch '[^']*' into $branch$" |
cut -d' ' -f1
}
candidates="$(search_reflog | sort -u)"
if test -z "$candidates"; then
echo "** Searching merges... **"
candidates="$( (search_merges;search_merge_targets) | sort -u)"
fi
echo "** Candidates **"
for cmt in $candidates; do
git --no-pager log --pretty=oneline --abbrev-commit -1 $cmt
done
newest=$(git rev-list -1 $candidates)
if ! git rev-parse --verify --quiet $branch >/dev/null; then
printf "** Restoring $branch to "
git --no-pager log -1 --pretty=tformat:"%h %s" $newest
git branch $branch $newest
else
printf "Most recent among them: "
git --no-pager log -1 --pretty=tformat:"%h %s" $newest
echo "** $branch already exists, doing nothing"
fi
Attachments
- signature.asc [application/pgp-signature] 197 bytes