diff --git a/README.md b/README.md index b9070ec..034b2bc 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ Change the dates of several git commits with a single command. -![alt tag](http://oi68.tinypic.com/3ud82.jpg) +![alt tag](https://i.stack.imgur.com/yE4cQ.gif) # Installation @@ -11,6 +11,8 @@ For homebrew users, you need to run `brew tap PotatoLabs/homebrew-git-redate` an If you're not using homebrew, you can clone this repo and move the `git-redate` file into any folders in your $PATH. Restart your terminal afterwards and you're good to go! +For window's users, you may paste the file into `${INSTALLATION_PATH}\mingw64\libexec\git-core`. Assuming you used the default settings the installation path will be `C:\Program Files\Git`. + # Usage Simply run: `git redate --commits [[number of commits to view]]`. You'll have to force push in order for your commit history to be rewritten. diff --git a/git-redate b/git-redate index 310c90e..d4a3399 100755 --- a/git-redate +++ b/git-redate @@ -11,7 +11,52 @@ is_git_repo() { is_git_repo +make_editor_choice() { + + echo "Which editor do you want to use for this repo?"; + echo "1. VI"; + echo "2. NANO"; + echo "3. Your own" + echo -n "You Choose: "; + + read CHOOSE_EDITOR +} + +get_editor_executable() { + + echo "What is the path to your prefered test editor?"; + read EDITOR_PATH +} + + +is_has_editor() { + SETTINGS_FILE="~/.redate-settings"; + if [ -f "$SETTINGS_FILE" ] + then + OUR_EDITOR=$(cat ${SETTINGS_FILE}); + elif [ ! -z "$EDITOR" ] + then + OUR_EDITOR="$EDITOR"; + else + make_editor_choice; + if [ ${CHOOSE_EDITOR} == 3 ] || [ ${CHOOSE_EDITOR} == "3" ]; then + get_editor_executable + OUR_EDITOR=${EDITOR_PATH} + elif [ ${CHOOSE_EDITOR} == 1 ] || [ ${CHOOSE_EDITOR} == "1" ]; then + OUR_EDITOR="vi"; + else + OUR_EDITOR="nano"; + fi + echo ${OUR_EDITOR} > ${SETTINGS_FILE} + fi +} + +is_has_editor + + ALL=0 +DEBUG=0 +LIMITCHUNKS=20 while [[ $# -ge 1 ]] do @@ -23,6 +68,15 @@ case $key in if [ -z "${COMMITS}" ]; then COMMITS="5"; fi; shift ;; + -l| --limit) + LIMITCHUNKS="$2" + if [ -z "${LIMITCHUNKS}" ]; then LIMITCHUNKS="20"; fi; + shift + ;; + -d| --debug) + DEBUG=1 + shift + ;; -a| --all) ALL=1 shift @@ -55,40 +109,98 @@ then git log --pretty=format:"$datefmt | %H | %s" > $tmpfile; else if [ -n "${COMMITS+set}" ]; - then git log -n $COMMITS --pretty=format:"$datefmt | %H | %s" > $tmpfile; + then git log -n ${COMMITS} --pretty=format:"$datefmt | %H | %s" > $tmpfile; else git log -n 5 --pretty=format:"$datefmt | %H | %s" > $tmpfile; fi fi -${VISUAL:-${EDITOR:-vi}} $tmpfile +${VISUAL:-${EDITOR:-${OUR_EDITOR}}} $tmpfile + +ITER=0 +COLITER=0 +declare -a COLLECTION + +COUNTCOMMITS=$(awk 'END {print NR}' $tmpfile) + +while read commit || [ -n "$commit" ]; do -ENVFILTER="" -while read commit; do IFS="|" read date hash message <<< "$commit" + shopt -s nocasematch + if [[ "$date" == 'now' ]]; then + date=$(date +%Y-%m-%dT%H:%M:%S%z); + fi + shopt -u nocasematch if [ "$datefmt" == "%cI" ] then DATE_NO_SPACE="$(echo "${date}" | tr -d '[[:space:]]')" else DATE_NO_SPACE="$(echo "${date}")" fi + + COMMIT_ENV=$(cat <<-END - if [ \$GIT_COMMIT = $hash ]; +if [ \$GIT_COMMIT = $hash ]; +then + export GIT_AUTHOR_DATE="$DATE_NO_SPACE" + export GIT_COMMITTER_DATE="$DATE_NO_SPACE"; +fi; +END +) + + ((ITER++)) + + if [ "${DEBUG}" -eq 1 ] && [ $((ITER % LIMITCHUNKS)) == $((LIMITCHUNKS - 1)) ]; + then + echo "Chunk $COLITER Finished" + fi + + if [ $((ITER % LIMITCHUNKS)) == 0 ] + then + ((COLITER++)) + + if [ "${DEBUG}" -eq 1 ]; then - export GIT_AUTHOR_DATE="$DATE_NO_SPACE" - export GIT_COMMITTER_DATE="$DATE_NO_SPACE"; - fi; - END - ) - ENVFILTER="$ENVFILTER$COMMIT_ENV" + echo "Chunk $COLITER Started" + fi + + fi + + COLLECTION[$COLITER]=${COLLECTION[COLITER]}"$COMMIT_ENV" + if [ "${DEBUG}" -eq 1 ] + then + echo "Commit $ITER/$COUNTCOMMITS Collected" + fi + done < $tmpfile -if [ "${ALL}" -eq 1 ]; -then - git filter-branch -f --env-filter "$ENVFILTER" -- --all >/dev/null -else - git filter-branch -f --env-filter "$ENVFILTER" HEAD~$COMMITS..HEAD >/dev/null -fi +ITERATOR=0 +for each in "${COLLECTION[@]}" +do + + ((ITERATOR++)) + + if [ "${ALL}" -eq 1 ]; + then + if [ "${DEBUG}" -eq 1 ]; + then + echo "Chunk $ITERATOR/"${#COLLECTION[@]}" Started" + git filter-branch -f --env-filter "$each" -- --all + echo "Chunk $ITERATOR/"${#COLLECTION[@]}" Finished" + else + git filter-branch -f --env-filter "$each" -- --all >/dev/null + fi + else + if [ "${DEBUG}" -eq 1 ]; + then + echo "Chunk $ITERATOR/"${#COLLECTION[@]}" Started" + git filter-branch -f --env-filter "$each" HEAD~${COMMITS}..HEAD + echo "Chunk $ITERATOR/"${#COLLECTION[@]}" Finished" + else + git filter-branch -f --env-filter "$each" HEAD~${COMMITS}..HEAD >/dev/null + fi + fi +done if [ $? = 0 ] ; then echo "Git commit dates updated. Run 'git push -f BRANCH_NAME' to push your changes."