Skip to content

Commit

Permalink
Merge pull request #18 from AndrewHastings/bugfix/handle-renames
Browse files Browse the repository at this point in the history
Bugfix: ignore names that were removed and re-added with a new name
  • Loading branch information
brettlangdon authored May 22, 2021
2 parents 7eb70a2 + 9d3deac commit 54dbca4
Showing 1 changed file with 44 additions and 5 deletions.
49 changes: 44 additions & 5 deletions bin/git-vendor
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ if [ "$1" = "--prefix" ]; then
shift; shift
fi

# Simulate an associative array (for older versions of bash)
var_name()
{
echo -n "name_$1" | tr -c '[A-Za-z0-9]' '_'
}

vendor_names_from_log()
{
name=
Expand All @@ -50,8 +56,11 @@ vendor_names_from_log()
git-vendor-dir:) dir="$b" ;;
END)
# Only consider dependencies which still exist on disk
if [ -d "$dir" ]; then
# and haven't been renamed
eval `echo -n val='$'$(var_name "$dir")`
if [ -d "$dir" -a -z "$val" ]; then
echo "$name"
eval `echo -n $(var_name "$dir")=1`
fi
name=
dir=
Expand All @@ -60,12 +69,30 @@ vendor_names_from_log()
done | sort -u
}

vendor_git_log_first()
vendor_git_log_from_name()
{
name="$1"
git log -1 --grep="^git-vendor-name: $name\$" --pretty=format:'START %H%n%s%n%n%b%nEND%n' HEAD
}

vendor_name_from_dir()
{
name=
dir="$1"
git log -1 --grep="^git-vendor-dir: $dir\$" --pretty=format:'START %H%n%s%n%n%b%nEND%n' HEAD |
while read a b junk; do
case "$a" in
git-vendor-name:) name="$b" ;;
END)
if [ -n "$name" ]; then
echo "$name"
fi
name=
;;
esac
done
}

cmd_add()
{
require_clean_work_tree
Expand Down Expand Up @@ -98,7 +125,7 @@ cmd_list()
showOnly="$1"
for name in $(vendor_names_from_log);
do
vendor_git_log_first "$name" |
vendor_git_log_from_name "$name" |
while read a b junk; do
case "$a" in
START) commit="$b" ;;
Expand Down Expand Up @@ -142,7 +169,7 @@ cmd_update()
if [ $# -lt 1 ]; then
die "Incorrect options provided: git vendor update <name> [<ref>]"
fi
vendor_git_log_first "$name" |
vendor_git_log_from_name "$name" |
while read a b junk; do
case "$a" in
START) ;;
Expand All @@ -154,6 +181,12 @@ cmd_update()
die "Dependency \"$1\" is missing from \"$dir\""
fi

# And hasn't been renamed
logname=$(vendor_name_from_dir "$dir")
if [ "$name" != "$logname" ]; then
die "Dependency \"$1\" was renamed \"$logname\""
fi

if [ ! -z "$repository" ];
then
message="\
Expand All @@ -179,7 +212,7 @@ cmd_remove()
if [ $# -lt 1 ]; then
die "Incorrect options provided: git vendor remove <name>"
fi
vendor_git_log_first "$name" |
vendor_git_log_from_name "$name" |
while read a b junk; do
case "$a" in
START) ;;
Expand All @@ -189,6 +222,12 @@ cmd_remove()
if [ ! -d "$dir" ]; then
die "Dependency \"$1\" is missing from \"$dir\""
fi
# And hasn't been renamed
logname=$(vendor_name_from_dir "$dir")
if [ "$name" != "$logname" ]; then
die "Dependency \"$1\" was renamed \"$logname\""
fi

git rm -rf "$dir"
git commit --message "Removing \"$name\" from \"$dir\""
break
Expand Down

0 comments on commit 54dbca4

Please sign in to comment.