From 8f321a39257a06db014a3b6ae5dce839821cdb16 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Tue, 6 Nov 2007 01:50:02 -0800 Subject: scripts: Add placeholders for OPTIONS_SPEC --text follows this line-- These commands currently lack OPTIONS_SPEC; allow people to easily list with "git grep 'OPTIONS_SPEC=$'" what they can help improving. Signed-off-by: Junio C Hamano --- git-filter-branch.sh | 1 + 1 file changed, 1 insertion(+) (limited to 'git-filter-branch.sh') diff --git a/git-filter-branch.sh b/git-filter-branch.sh index ffcc408ee5..c9f515d0ee 100755 --- a/git-filter-branch.sh +++ b/git-filter-branch.sh @@ -92,6 +92,7 @@ USAGE="[--env-filter ] [--tree-filter ] \ [--original ] [-d ] [-f | --force] \ [...]" +OPTIONS_SPEC= . git-sh-setup git diff-files --quiet && -- cgit v1.2.3 From 16ed34ad35d9a991a72b2db420ff95bc260990cc Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Wed, 28 Nov 2007 15:50:38 +0000 Subject: filter-branch: fix dirty way to provide the helpers to commit filters The helper functions 'map' and 'skip_commit' were provided to commit filters by sourcing filter-branch itself. This was done with a certain environment variable set to indicate that only the functions should be defined, and the script should return then. This was really hacky, and it did not work all that well, since the full path to git-filter-branch was not known at all times. Avoid that by putting the functions into a variable, and eval'ing that variable. The commit filter gets these functions by prepending the variable to the specified commands. Signed-off-by: Johannes Schindelin Signed-off-by: Junio C Hamano --- git-filter-branch.sh | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'git-filter-branch.sh') diff --git a/git-filter-branch.sh b/git-filter-branch.sh index c9f515d0ee..19cab5a9a8 100755 --- a/git-filter-branch.sh +++ b/git-filter-branch.sh @@ -8,6 +8,9 @@ # a new branch. You can specify a number of filters to modify the commits, # files and trees. +# The following functions will also be available in the commit filter: + +functions=$(cat << \EOF warn () { echo "$*" >&2 } @@ -46,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 @@ -80,11 +87,6 @@ set_ident () { echo "[ -n \"\$GIT_${uid}_NAME\" ] || export GIT_${uid}_NAME=\"\${GIT_${uid}_EMAIL%%@*}\"" } -# This script can be sourced by the commit filter to get the functions -test "a$SOURCE_FUNCTIONS" = a1 && return -this_script="$(cd "$(dirname "$0")"; pwd)"/$(basename "$0") -export this_script - USAGE="[--env-filter ] [--tree-filter ] \ [--index-filter ] [--parent-filter ] \ [--msg-filter ] [--commit-filter ] \ @@ -156,7 +158,7 @@ do filter_msg="$OPTARG" ;; --commit-filter) - filter_commit='SOURCE_FUNCTIONS=1 . "$this_script";'" $OPTARG" + filter_commit="$functions; $OPTARG" ;; --tag-name-filter) filter_tag_name="$OPTARG" -- cgit v1.2.3 From 38762c47d6442dc0ce0f45533f9151877c485337 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Wed, 28 Nov 2007 16:15:04 -0800 Subject: scripts: do not get confused with HEAD in work tree When you have a file called HEAD in your work tree, many commands that our scripts feed "HEAD" to would complain about the rev vs path ambiguity. A solution is to form command line more carefully by appending -- to them, which makes it clear that we mean HEAD rev not HEAD file. This patch would apply to maint. Signed-off-by: Junio C Hamano --- git-filter-branch.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'git-filter-branch.sh') diff --git a/git-filter-branch.sh b/git-filter-branch.sh index ffcc408ee5..dbab1a9a4a 100755 --- a/git-filter-branch.sh +++ b/git-filter-branch.sh @@ -95,7 +95,7 @@ USAGE="[--env-filter ] [--tree-filter ] \ . git-sh-setup git diff-files --quiet && - git diff-index --cached --quiet HEAD || + git diff-index --cached --quiet HEAD -- || die "Cannot rewrite branch(es) with a dirty working directory." tempdir=.git-rewrite -- cgit v1.2.3 From 3addc94aff0b10730c6b023e1a4a5d677e53d5a7 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Wed, 28 Nov 2007 15:56:11 +0000 Subject: Replace instances of export VAR=VAL with VAR=VAL; export VAR It might be POSIX, but there are shells that do not like the expression 'export VAR=VAL'. To be on the safe side, rewrite them into 'VAR=VAL' and 'export VAR'. Signed-off-by: Johannes Schindelin Signed-off-by: Junio C Hamano --- git-filter-branch.sh | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) (limited to 'git-filter-branch.sh') diff --git a/git-filter-branch.sh b/git-filter-branch.sh index 674a25d27e..29d35fd27c 100755 --- a/git-filter-branch.sh +++ b/git-filter-branch.sh @@ -66,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 } @@ -84,7 +84,7 @@ 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 ] [--tree-filter ] \ @@ -206,7 +206,8 @@ done < "$tempdir"/backup-refs ORIG_GIT_DIR="$GIT_DIR" ORIG_GIT_WORK_TREE="$GIT_WORK_TREE" ORIG_GIT_INDEX_FILE="$GIT_INDEX_FILE" -export GIT_DIR GIT_WORK_TREE=. +GIT_WORK_TREE=. +export GIT_DIR GIT_WORK_TREE # These refs should be updated if their heads were rewritten @@ -231,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 @@ -267,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" @@ -401,7 +404,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" -- cgit v1.2.3 From 3175b0cfc1392de1ff00c01796f85b92df317cc8 Mon Sep 17 00:00:00 2001 From: "H.Merijn Brand" Date: Mon, 17 Dec 2007 23:28:46 +0100 Subject: the use of 'tr' in the test suite isn't really portable Some versions of 'tr' only accept octal codes if entered with three digits, and therefor misinterpret the '\0' in the test suite. Some versions of 'tr' reject the (needless) use of character classes. Signed-off-by: H.Merijn Brand Signed-off-by: Junio C Hamano --- git-filter-branch.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'git-filter-branch.sh') diff --git a/git-filter-branch.sh b/git-filter-branch.sh index 29d35fd27c..3bb2f676bd 100755 --- a/git-filter-branch.sh +++ b/git-filter-branch.sh @@ -290,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 '\n' '\000' | \ xargs -0 git update-index --add --replace --remove git ls-files -z --others | \ xargs -0 git update-index --add --replace --remove -- cgit v1.2.3 From 91c3aced4c9b961e162f9acd706f4b03a85aed2c Mon Sep 17 00:00:00 2001 From: Johannes Sixt Date: Tue, 18 Dec 2007 08:30:47 +0100 Subject: filter-branch: Remove broken and unnecessary summary of rewritten refs. There was an attempt to list the refs that were rewritten by filtering the output of 'git show-ref' for 'refs/original'. But it got the grep argument wrong, which did not account for the SHA1 that is listed before the ref. Moreover, right before this summary is the loop that actually does the rewriting, and the rewritten refs are listed there anyway. So this extra summary is plainly too verbose. Signed-off-by: Johannes Sixt Signed-off-by: Junio C Hamano --- git-filter-branch.sh | 5 ----- 1 file changed, 5 deletions(-) (limited to 'git-filter-branch.sh') diff --git a/git-filter-branch.sh b/git-filter-branch.sh index 3bb2f676bd..7f52b591a3 100755 --- a/git-filter-branch.sh +++ b/git-filter-branch.sh @@ -342,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 @@ -380,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 @@ -423,9 +421,6 @@ 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 -- cgit v1.2.3 From e8a3f90994067a154fafcec9bbb18cf7865cd94c Mon Sep 17 00:00:00 2001 From: Jim Meyering Date: Tue, 18 Dec 2007 11:03:23 +0100 Subject: git-filter-branch.sh: more portable tr usage: use \012, not \n. I hesitate to suggest this, since GNU tr has accepted \n for 15 years, but there are supposedly a few crufty vendor-supplied versions of tr still in use. Also, all of the other uses of tr-with-newline in git use \012. Signed-off-by: Jim Meyering Signed-off-by: Junio C Hamano --- git-filter-branch.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'git-filter-branch.sh') diff --git a/git-filter-branch.sh b/git-filter-branch.sh index 7f52b591a3..ae29f47e41 100755 --- a/git-filter-branch.sh +++ b/git-filter-branch.sh @@ -290,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' '\000' | \ + 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 -- cgit v1.2.3 From 418fa3a5c9111e675d8fcd3271315c38cdb1f16b Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Sat, 5 Jan 2008 12:18:43 -0800 Subject: filter-branch: work correctly with ambiguous refnames 'git-filter-branch branch' could fail producing the error: "Which ref do you want to rewrite?" if existed another branch or tag, which name was 'branch-something' or 'something/branch'. [jc: original report and fix were done between Dmitry Potapov and Dscho; I rewrote it using "rev-parse --symbolic-full-name"] Signed-off-by: Junio C Hamano --- git-filter-branch.sh | 22 +++------------------- 1 file changed, 3 insertions(+), 19 deletions(-) (limited to 'git-filter-branch.sh') diff --git a/git-filter-branch.sh b/git-filter-branch.sh index ae29f47e41..ebf05ca600 100755 --- a/git-filter-branch.sh +++ b/git-filter-branch.sh @@ -209,25 +209,9 @@ 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 - -git rev-parse --revs-only --symbolic "$@" | -while read ref -do - # normalize ref - case "$ref" in - HEAD) - ref="$(git symbolic-ref "$ref")" - ;; - refs/*) - ;; - *) - ref="$(git for-each-ref --format='%(refname)' | - grep /"$ref")" - esac - - git check-ref-format "$ref" && echo "$ref" -done > "$tempdir"/heads +# The refs should be updated if their heads were rewritten +git rev-parse --no-flags --revs-only --symbolic-full-name "$@" | +sed -e '/^^/d' >"$tempdir"/heads test -s "$tempdir"/heads || die "Which ref do you want to rewrite?" -- cgit v1.2.3 From def16e71819833d507a97a645366c569939dce73 Mon Sep 17 00:00:00 2001 From: Brandon Casey Date: Mon, 28 Jan 2008 15:16:02 -0600 Subject: filter-branch.sh: remove temporary directory on failure One of the first things filter-branch does is to create a temporary directory. This directory is eventually removed by the script during normal operation, but is not removed if the script encounters an error. Set a trap to remove it when the script terminates for any reason. Signed-off-by: Brandon Casey Acked-by: Johannes Schindelin Signed-off-by: Junio C Hamano --- git-filter-branch.sh | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'git-filter-branch.sh') diff --git a/git-filter-branch.sh b/git-filter-branch.sh index ebf05ca600..fbb948d6fd 100755 --- a/git-filter-branch.sh +++ b/git-filter-branch.sh @@ -189,6 +189,9 @@ cd "$tempdir/t" && workdir="$(pwd)" || die "" +# Remove tempdir on exit +trap 'cd ../..; rm -rf "$tempdir"' 0 + # Make sure refs/original is empty git for-each-ref > "$tempdir"/backup-refs while read sha1 type name @@ -406,6 +409,8 @@ fi cd ../.. rm -rf "$tempdir" +trap - 0 + 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" && -- cgit v1.2.3 From c41b439244c51b30c60953192816afc91e552578 Mon Sep 17 00:00:00 2001 From: Brandon Casey Date: Wed, 30 Jan 2008 18:41:25 -0600 Subject: filter-branch docs: remove brackets so not to imply revision arg is optional Signed-off-by: Brandon Casey Signed-off-by: Junio C Hamano --- git-filter-branch.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'git-filter-branch.sh') diff --git a/git-filter-branch.sh b/git-filter-branch.sh index fbb948d6fd..753bf50c6d 100755 --- a/git-filter-branch.sh +++ b/git-filter-branch.sh @@ -92,7 +92,7 @@ USAGE="[--env-filter ] [--tree-filter ] \ [--msg-filter ] [--commit-filter ] \ [--tag-name-filter ] [--subdirectory-filter ] \ [--original ] [-d ] [-f | --force] \ -[...]" +" OPTIONS_SPEC= . git-sh-setup -- cgit v1.2.3 From 0f047f3b47693e8f80a00bcf4af421d9d2fc7efd Mon Sep 17 00:00:00 2001 From: Brandon Casey Date: Wed, 30 Jan 2008 13:33:04 -0600 Subject: filter-branch: assume HEAD if no revision supplied filter-branch previously took the first non-option argument as the name for a new branch. Since dfd05e38, it now takes a revision or a revision range and modifies the current branch. Update to operate on HEAD by default to conform with standard git interface practice. Signed-off-by: Brandon Casey Signed-off-by: Junio C Hamano --- git-filter-branch.sh | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'git-filter-branch.sh') diff --git a/git-filter-branch.sh b/git-filter-branch.sh index 753bf50c6d..ed54e20b2f 100755 --- a/git-filter-branch.sh +++ b/git-filter-branch.sh @@ -114,7 +114,6 @@ orig_namespace=refs/original/ force= while : do - test $# = 0 && usage case "$1" in --) shift @@ -213,7 +212,7 @@ GIT_WORK_TREE=. export GIT_DIR GIT_WORK_TREE # The refs should be updated if their heads were rewritten -git rev-parse --no-flags --revs-only --symbolic-full-name "$@" | +git rev-parse --no-flags --revs-only --symbolic-full-name --default HEAD "$@" | sed -e '/^^/d' >"$tempdir"/heads test -s "$tempdir"/heads || -- cgit v1.2.3 From bb8eebb94f0acf9a7ec19a1815bff7c9a2d3e427 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Thu, 31 Jan 2008 13:51:42 -0800 Subject: Revert "filter-branch docs: remove brackets so not to imply revision arg is optional" This reverts commit c41b439244c51b30c60953192816afc91e552578, as we decided to default to HEAD when revision parameters are missing and they are no longer mandatory. --- git-filter-branch.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'git-filter-branch.sh') diff --git a/git-filter-branch.sh b/git-filter-branch.sh index ed54e20b2f..ff716cabb0 100755 --- a/git-filter-branch.sh +++ b/git-filter-branch.sh @@ -92,7 +92,7 @@ USAGE="[--env-filter ] [--tree-filter ] \ [--msg-filter ] [--commit-filter ] \ [--tag-name-filter ] [--subdirectory-filter ] \ [--original ] [-d ] [-f | --force] \ -" +[...]" OPTIONS_SPEC= . git-sh-setup -- cgit v1.2.3 From 1fe32cb9d0807e6da468dc7bf96d427b2f38c1c4 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Fri, 15 Feb 2008 23:57:26 -0800 Subject: filter-branch: handle filenames that need quoting The command used a very old fashioned construct to extract filenames out of diff-index and ended up corrupting the output. We can simply use --name-only and pipe into --stdin mode of update-index. It's been like that for the past 2 years or so since a94d994 (update-index: work with c-quoted name). Signed-off-by: Junio C Hamano --- git-filter-branch.sh | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'git-filter-branch.sh') diff --git a/git-filter-branch.sh b/git-filter-branch.sh index ff716cabb0..49e13f0bb1 100755 --- a/git-filter-branch.sh +++ b/git-filter-branch.sh @@ -276,10 +276,11 @@ 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 '\012' '\000' | \ - xargs -0 git update-index --add --replace --remove - git ls-files -z --others | \ - xargs -0 git update-index --add --replace --remove + ( + git diff-index -r --name-only $commit + git ls-files --others + ) | + git update-index --add --replace --remove --stdin fi eval "$filter_index" < /dev/null || -- cgit v1.2.3