Code

Don't access line[-1] for a zero-length "line" from fgets.
[git.git] / git-filter-branch.sh
index c166c978ec2a6e1dc26d8fb117c12ae59ca5b7fd..ae29f47e41cde04599c22603173c8aaa4ef533d9 100755 (executable)
@@ -8,15 +8,9 @@
 # a new branch. You can specify a number of filters to modify the commits,
 # files and trees.
 
-USAGE="[--env-filter <command>] [--tree-filter <command>] \
-[--index-filter <command>] [--parent-filter <command>] \
-[--msg-filter <command>] [--commit-filter <command>] \
-[--tag-name-filter <command>] [--subdirectory-filter <directory>] \
-[--original <namespace>] [-d <directory>] [-f | --force] \
-[<rev-list options>...]"
-
-. git-sh-setup
+# The following functions will also be available in the commit filter:
 
+functions=$(cat << \EOF
 warn () {
         echo "$*" >&2
 }
@@ -32,6 +26,20 @@ map()
        fi
 }
 
+# if you run 'skip_commit "$@"' in a commit filter, it will print
+# the (mapped) parents, effectively skipping the commit.
+
+skip_commit()
+{
+       shift;
+       while [ -n "$1" ];
+       do
+               shift;
+               map "$1";
+               shift;
+       done;
+}
+
 # override die(): this version puts in an extra line break, so that
 # the progress is still visible
 
@@ -41,6 +49,10 @@ die()
        echo "$*" >&2
        exit 1
 }
+EOF
+)
+
+eval "$functions"
 
 # When piped a commit, output a script to set the ident of either
 # "author" or "committer
@@ -54,17 +66,17 @@ set_ident () {
                        h
                        s/^'$lid' \([^<]*\) <[^>]*> .*$/\1/
                        s/'\''/'\''\'\'\''/g
-                       s/.*/export GIT_'$uid'_NAME='\''&'\''/p
+                       s/.*/GIT_'$uid'_NAME='\''&'\''; export GIT_'$uid'_NAME/p
 
                        g
                        s/^'$lid' [^<]* <\([^>]*\)> .*$/\1/
                        s/'\''/'\''\'\'\''/g
-                       s/.*/export GIT_'$uid'_EMAIL='\''&'\''/p
+                       s/.*/GIT_'$uid'_EMAIL='\''&'\''; export GIT_'$uid'_EMAIL/p
 
                        g
                        s/^'$lid' [^<]* <[^>]*> \(.*\)$/\1/
                        s/'\''/'\''\'\'\''/g
-                       s/.*/export GIT_'$uid'_DATE='\''&'\''/p
+                       s/.*/GIT_'$uid'_DATE='\''&'\''; export GIT_'$uid'_DATE/p
 
                        q
                }
@@ -72,9 +84,23 @@ set_ident () {
 
        LANG=C LC_ALL=C sed -ne "$pick_id_script"
        # Ensure non-empty id name.
-       echo "[ -n \"\$GIT_${uid}_NAME\" ] || export GIT_${uid}_NAME=\"\${GIT_${uid}_EMAIL%%@*}\""
+       echo "case \"\$GIT_${uid}_NAME\" in \"\") GIT_${uid}_NAME=\"\${GIT_${uid}_EMAIL%%@*}\" && export GIT_${uid}_NAME;; esac"
 }
 
+USAGE="[--env-filter <command>] [--tree-filter <command>] \
+[--index-filter <command>] [--parent-filter <command>] \
+[--msg-filter <command>] [--commit-filter <command>] \
+[--tag-name-filter <command>] [--subdirectory-filter <directory>] \
+[--original <namespace>] [-d <directory>] [-f | --force] \
+[<rev-list options>...]"
+
+OPTIONS_SPEC=
+. git-sh-setup
+
+git diff-files --quiet &&
+       git diff-index --cached --quiet HEAD -- ||
+       die "Cannot rewrite branch(es) with a dirty working directory."
+
 tempdir=.git-rewrite
 filter_env=
 filter_tree=
@@ -86,8 +112,9 @@ filter_tag_name=
 filter_subdir=
 orig_namespace=refs/original/
 force=
-while case "$#" in 0) usage;; esac
+while :
 do
+       test $# = 0 && usage
        case "$1" in
        --)
                shift
@@ -131,7 +158,7 @@ do
                filter_msg="$OPTARG"
                ;;
        --commit-filter)
-               filter_commit="$OPTARG"
+               filter_commit="$functions; $OPTARG"
                ;;
        --tag-name-filter)
                filter_tag_name="$OPTARG"
@@ -176,7 +203,11 @@ do
        esac
 done < "$tempdir"/backup-refs
 
-export GIT_DIR GIT_WORK_TREE=.
+ORIG_GIT_DIR="$GIT_DIR"
+ORIG_GIT_WORK_TREE="$GIT_WORK_TREE"
+ORIG_GIT_INDEX_FILE="$GIT_INDEX_FILE"
+GIT_WORK_TREE=.
+export GIT_DIR GIT_WORK_TREE
 
 # These refs should be updated if their heads were rewritten
 
@@ -201,7 +232,8 @@ done > "$tempdir"/heads
 test -s "$tempdir"/heads ||
        die "Which ref do you want to rewrite?"
 
-export GIT_INDEX_FILE="$(pwd)/../index"
+GIT_INDEX_FILE="$(pwd)/../index"
+export GIT_INDEX_FILE
 git read-tree || die "Could not seed the index"
 
 ret=0
@@ -237,7 +269,8 @@ while read commit parents; do
                git read-tree -i -m $commit:"$filter_subdir"
        esac || die "Could not initialize the index"
 
-       export GIT_COMMIT=$commit
+       GIT_COMMIT=$commit
+       export GIT_COMMIT
        git cat-file commit "$commit" >../commit ||
                die "Cannot read commit $commit"
 
@@ -257,7 +290,7 @@ while read commit parents; do
                eval "$filter_tree" < /dev/null ||
                        die "tree filter failed: $filter_tree"
 
-               git diff-index -r $commit | cut -f 2- | tr '\n' '\0' | \
+               git diff-index -r $commit | cut -f 2- | tr '\012' '\000' | \
                        xargs -0 git update-index --add --replace --remove
                git ls-files -z --others | \
                        xargs -0 git update-index --add --replace --remove
@@ -309,7 +342,6 @@ done < "$tempdir"/heads
 
 _x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
 _x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
-count=0
 echo
 while read ref
 do
@@ -347,7 +379,6 @@ do
        ;;
        esac
        git update-ref -m "filter-branch: backup" "$orig_namespace$ref" $sha1
-       count=$(($count+1))
 done < "$tempdir"/heads
 
 # TODO: This should possibly go, with the semantics that all positive given
@@ -371,7 +402,8 @@ if [ "$filter_tag_name" ]; then
 
                [ -f "../map/$sha1" ] || continue
                new_sha1="$(cat "../map/$sha1")"
-               export GIT_COMMIT="$sha1"
+               GIT_COMMIT="$sha1"
+               export GIT_COMMIT
                new_ref="$(echo "$ref" | eval "$filter_tag_name")" ||
                        die "tag name filter failed: $filter_tag_name"
 
@@ -389,8 +421,13 @@ fi
 
 cd ../..
 rm -rf "$tempdir"
-echo
-test $count -gt 0 && echo "These refs were rewritten:"
-git show-ref | grep ^"$orig_namespace"
+
+unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE
+test -z "$ORIG_GIT_DIR" || GIT_DIR="$ORIG_GIT_DIR" && export GIT_DIR
+test -z "$ORIG_GIT_WORK_TREE" || GIT_WORK_TREE="$ORIG_GIT_WORK_TREE" &&
+       export GIT_WORK_TREE
+test -z "$ORIG_GIT_INDEX_FILE" || GIT_INDEX_FILE="$ORIG_GIT_INDEX_FILE" &&
+       export GIT_INDEX_FILE
+git read-tree -u -m HEAD
 
 exit $ret