Skip to content

Instantly share code, notes, and snippets.

@houkanshan
Created October 9, 2013 15:14
Show Gist options
  • Save houkanshan/6902865 to your computer and use it in GitHub Desktop.
Save houkanshan/6902865 to your computer and use it in GitHub Desktop.
Clear "console.log" before `git commit`, and recover them after `git commit`, add them to git-hooks file: pre-commit & post-commit ~
#!/bin/sh
debug_rex='/console.log/'
debug_print_rex=$debug_rex'p'
debug_del_rex=$debug_rex' d'
add_debug_patch='.add-debug.patch'
del_debug_patch='.del-debug.patch'
git_work_dir="$(git rev-parse --show-toplevel)"
fake_file="$git_work_dir/.fake_file"
cur_dir="$(pwd)"
staged_files="$(git diff --name-only --cached | grep '.js$')"
debug_lines="$(sed -n $debug_print_rex $staged_files)"
function add_fake_file {
touch $fake_file
git add $fake_file
}
function clear_fake_file {
rm $fake_file
git rm -q $fake_file
}
if [ ! "$debug_lines" ]; then
exit 0
fi
cd $git_work_dir
#echo "save unstaged to stash"
git stash save --keep-index -q
#echo "clone staged change to stash"
add_fake_file
git stash save -q
git stash apply -q
if [ ! "$staged_files" ]; then
exit 0
fi
#echo "show console.log"
echo "[remove console.log]"
echo "$debug_lines"
#echo "delete console.log"
sed -i "$debug_del_rex" $staged_files
#echo "generate debug patch file"
git diff -R stash@{0} > $add_debug_patch
clear_fake_file
git add -u # for deleted file
git add $staged_files # for file new added
git stash drop -q
cd - > /dev/null 2>&1
#!/bin/sh
add_debug_patch='.add-debug.patch'
del_debug_patch='.del-debug.patch'
git_work_dir="$(git rev-parse --show-toplevel)"
fake_file="$git_work_dir/.fake_file"
cur_dir="$(pwd)"
function force_stash_pop {
git diff --binary stash@{0} | patch --binary -R -s
# for safety comment this line
git stash drop -q
}
if [ ! -e "$add_debug_patch" ]; then
exit 0
fi
cd $git_work_dir
patch -s -p1 < $add_debug_patch
rm $add_debug_patch
#echo "pop unstaged stash"
force_stash_pop
cd - > /dev/null 2>&1
@funkybunky
Copy link

This works well, thank you very much!
I just had to change the function keywords with the other syntax:
remove "function" and add "()" after the function name to make it work, if you encounter the same issue.
See this SO answer: https://stackoverflow.com/a/46784383

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment