Created
November 5, 2010 19:57
-
-
Save StanAngeloff/664680 to your computer and use it in GitHub Desktop.
Get those changed files out of Git
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
BOLD="\033[1m" | |
_BOLD="\033[22m" | |
RED="\033[31m" | |
YELLOW="\033[33m" | |
GREEN="\033[32m" | |
RESET="\033[39m" | |
range=$1 | |
if [ -z "$range" ]; then | |
echo -e "${BOLD}${RED}You must specify a '<since>..<until>' argument.${RESET}${_BOLD}" | |
exit 1 | |
fi | |
if [ -z "$2" ]; then | |
target="$( pwd )/.deployments" | |
else | |
target=$( echo "$2" | sed -e 's#/\+$##g' ) | |
fi | |
if [ -d "$target" ]; then | |
echo -ne "Do you wish to remove '$target' first? [Y/n] " | |
read prompt | |
if [ -z "$prompt" ] || [ "$prompt" == "Y" ] || [ "$prompt" == "y" ]; then | |
echo -e " ${YELLOW}Purging '$target'...${RESET}" | |
if [ -d "$target" ]; then | |
rm -Rf "$target" | |
fi | |
echo -e " ${GREEN}Done.${RESET}" | |
fi | |
fi | |
mkdir -p "$target" | |
LOG=$( git whatchanged -m --oneline "$range" | awk '{ | |
if ($1 ~ /^:/) { | |
print $5 ":" $6 | |
} | |
}' | tac ) | |
length=$( echo "$LOG" | wc -l ) | |
manual='' | |
index=0 | |
for command in $LOG; do | |
operation=${command:0:1} | |
filepath=${command:2} | |
case $operation in | |
"A" | "M") | |
if [ -f "$filepath" ]; then | |
destination=$( dirname "$target/$filepath" ) | |
filename=$( basename "$filepath" ) | |
mkdir -p "$destination" | |
cp -f "$filepath" "$target/$filepath" | |
fi | |
;; | |
"D") | |
manual="$manual\n$filepath" | |
;; | |
*) | |
echo -e "${BOLD}${RED}Unknown operation $operation on file $filepath.${RESET}${_BOLD}" | |
exit 4 | |
;; | |
esac | |
let "index++" | |
echo -ne "\r${YELLOW}Processing ${length} files...${RESET} $( echo "scale=2; ( $index / $length ) * 100.00" | bc )%" | |
done | |
echo | |
if [ ! -z "$manual" ]; then | |
echo $( echo -e "$manual" | sed -e 's/^\s\+//g' | sort -u ) > "$target/.delete" | |
echo -e "[WARN] Please manually delete the files listed in '${BOLD}.delete${_BOLD}'" | |
fi | |
echo -e "${GREEN}Done.${RESET}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
I just used your script and it's quite amazing ! One little bug that I could'nt wrap my head around was the fact that the files listed in the .delete file were not listed one per line.
The fix is (line 72) to apply the -e flag on the first echo and not the second :
echo -e $( echo "$manual" | sed -e 's/^\s\+//g' | sort -u ) > "$target/.delete"
.Thanks again for the script !