Skip to content

Instantly share code, notes, and snippets.

@pclouds
Last active April 19, 2018 08:39
Show Gist options
  • Save pclouds/3f474bd8c1beb222c7277aa00bcea4be to your computer and use it in GitHub Desktop.
Save pclouds/3f474bd8c1beb222c7277aa00bcea4be to your computer and use it in GitHub Desktop.
#!/bin/sh
# git config alias.lc='branch --list --remote --verbose c/*'
# git config alias.fc="!/path/to/this/script'
#
# Do "git fc <change id>" to get one change, the change will be put in remotes namespace c/. You can
# also do "git fc c/<change id>" if you copy paste the ref name. Do "git fc --all" to update all
# tracked gerrit changes.
fetch() {
local id="$(echo "$1" | sed 's|^c/||')"
case $id in
*/*)
change_id=${id%%/*}
patchset_id=${id##*/}
tryagain=f
;;
*)
change_id=$id
patchset_id="$(git config gerrit.c$change_id)"
if [ -n "$patchset_id" ]; then
patchset_id=$(($patchset_id+1))
else
patchset_id=1
fi
tryagain=t
;;
esac
local hash=$(($change_id % 100))
case $hash in
[0-9]) hash="0$hash" ;;
esac
local last_good_patchset_id=
echo -n "Updating c/$change_id ">&2
while true; do
refspec="refs/changes/$hash/$change_id/$patchset_id:refs/remotes/c/$change_id"
git fetch -n origin +"$refspec" 2>/dev/null || break
echo -n . >&2
last_good_patchset_id=$patchset_id
test $tryagain = f && break
patchset_id=$((patchset_id+1))
done
if [ -n "$last_good_patchset_id" ]; then
git config gerrit.c$change_id $last_good_patchset_id
echo " --> $(git rev-list --oneline -1 refs/remotes/c/$change_id)" >&2
else
echo >&2
fi
}
if [ -z "$1" ]; then
echo "Give me a change id!" >&2
exit 1
fi
if [ "$1" = "--all" ]; then
git branch --list --remote 'c/*' |
while read i; do fetch "$i"; done
else
fetch "$1"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment