Skip to content

Instantly share code, notes, and snippets.

@sorohan
Created February 23, 2015 12:43
Show Gist options
  • Save sorohan/27f083c818715e754ccd to your computer and use it in GitHub Desktop.
Save sorohan/27f083c818715e754ccd to your computer and use it in GitHub Desktop.
Track folder renames in git.
#!/bin/bash
d=$1
function first_commit_on_dir() {
d=$1
echo $(git log --follow --oneline --pretty=format:'%H' --reverse -- "$d" | head -1)
}
function get_renames() {
d=$1
first_commit=$(first_commit_on_dir "$d")
#echo "first commit is $first_commit"
tree=$(git ls-tree -r -d "$first_commit" -- "$d" | grep "${d}\$" | awk '{print $3}')
#echo "tree at first commit is $tree"
old_dir=$(git ls-tree -r -d "${first_commit}^" | grep "$tree" | awk '{print $4}')
if [ ! -z "$old_dir" ]; then
renames=$(get_renames "$old_dir")
if [ ! -z "$renames" ]; then
printf "$renames\n"
fi
printf "$old_dir $d $first_commit\n"
fi
}
get_renames "$d"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment