Skip to contentSkip to conversation

add setup step to filter-branch

PATCH Superseded Andreas Heiduk 3 participants 10 messages Jun 3, 2017 – Jun 10, 2017 Documentation

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 add setup step to filter-branch

A specific --setup step in git filter-branch makes it much easier to define the initial values of variables used in the real filters. Also sourcing/defining utility functions here instead of --env-filter improves performance and minimizes clogging the output in case of errors.

---
 Documentation/git-filter-branch.txt | 16 +++++++++++-----
 git-filter-branch.sh                | 18 +++++++++++++-----
 2 files changed, 24 insertions(+), 10 deletions(-)
Documentation/git-filter-branch.txt +11 -5
index 6e4bb0220..45c849d8c 100644
@@ -8,11 +8,11 @@ git-filter-branch - Rewrite branches
 SYNOPSIS
 --------
 [verse]
-'git filter-branch' [--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>]
-	[--prune-empty]
+'git filter-branch' [--setup <command>] [--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>] [--prune-empty]
 	[--original <namespace>] [-d <directory>] [-f | --force]
 	[--] [<rev-list options>...]
 
@@ -82,6 +82,12 @@ multiple commits.
 OPTIONS
 -------
 
+--setup <command>::
+	This is not a real filter executed for each commit but a one
+	time setup just before the loop. Therefore no commit-specific
+	variables are defined yet.  Functions or variables defined here
+	can be used or modified in the following filter steps.
+
 --env-filter <command>::
 	This filter may be used if you only need to modify the environment
 	in which the commit will be performed.  Specifically, you might
git-filter-branch.sh +13 -5
index aafaf708d..2758ae5eb 100755
@@ -81,11 +81,12 @@ set_ident () {
 	finish_ident COMMITTER
 }
 
-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]
+USAGE="[--setup <command>] [--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=
@@ -96,6 +97,7 @@ if [ "$(is_bare_repository)" = false ]; then
 fi
 
 tempdir=.git-rewrite
+filter_setup=
 filter_env=
 filter_tree=
 filter_index=
@@ -148,6 +150,9 @@ do
 	-d)
 		tempdir="$OPTARG"
 		;;
+	--setup)
+		filter_setup="$OPTARG"
+		;;
 	--env-filter)
 		filter_env="$OPTARG"
 		;;
@@ -317,6 +322,9 @@ else
 	need_index=
 fi
 
+eval "$filter_setup" < /dev/null ||
+	die "filter setup failed: $filter_setup"
+
 while read commit parents; do
 	git_filter_branch__commit_count=$(($git_filter_branch__commit_count+1))
 
Signature · 1 line
2.13.0
#1 lore raw

PATCH 2/2 add [--] to usage of filter-branch

---
 Documentation/git-filter-branch.txt | 3 ++-
 git-filter-branch.sh                | 2 +-
 2 files changed, 3 insertions(+), 2 deletions(-)
Documentation/git-filter-branch.txt +2 -1
index 45c849d8c..1efdda804 100644
@@ -86,7 +86,8 @@ OPTIONS
 	This is not a real filter executed for each commit but a one
 	time setup just before the loop. Therefore no commit-specific
 	variables are defined yet.  Functions or variables defined here
-	can be used or modified in the following filter steps.
+	can be used or modified in the following filter steps except
+	the commit filter, for technical reasons.
 
 --env-filter <command>::
 	This filter may be used if you only need to modify the environment
git-filter-branch.sh +1 -1
index 2758ae5eb..3a74602ef 100755
@@ -87,7 +87,7 @@ USAGE="[--setup <command>] [--env-filter <command>]
 	[--commit-filter <command>] [--tag-name-filter <command>]
 	[--subdirectory-filter <directory>] [--original <namespace>]
 	[-d <directory>] [-f | --force]
-	[<rev-list options>...]"
+	[--] [<rev-list options>...]"
 
 OPTIONS_SPEC=
 . git-sh-setup
Signature · 1 line
2.13.0
#2 lore raw

Re: [PATCH 2/2] add [--] to usage of filter-branch

Am 03.06.2017 um 12:17 schrieb Andreas Heiduk:

Quoted 11 lines
---
 Documentation/git-filter-branch.txt | 3 ++-
 git-filter-branch.sh                | 2 +-
 2 files changed, 3 insertions(+), 2 deletions(-)
Documentation/git-filter-branch.txt +0 -0
index 45c849d8c..1efdda804 100644

Documentation/git-filter-branch.txt OPTIONS · line 90

Quoted 6 lines
Documentation/git-filter-branch.txt +2 -1
@@ -86,7 +86,8 @@ OPTIONS
 	This is not a real filter executed for each commit but a one
 	time setup just before the loop. Therefore no commit-specific
 	variables are defined yet.  Functions or variables defined here
-	can be used or modified in the following filter steps.
+	can be used or modified in the following filter steps except
+	the commit filter, for technical reasons.

I'll move that into the previous commit.

#3 lore raw

Re: [PATCH 2/2] add [--] to usage of filter-branch

Quoted 7 lines · Andreas Heiduk

Am 03.06.2017 um 12:17 schrieb Andreas Heiduk:

Quoted 6 lines
---
 Documentation/git-filter-branch.txt | 3 ++-
 git-filter-branch.sh                | 2 +-
 2 files changed, 3 insertions(+), 2 deletions(-)
Quoted 13 lines

Documentation/git-filter-branch.txt OPTIONS · line 90

Quoted 11 lines
Documentation/git-filter-branch.txt +2 -1
index 45c849d8c..1efdda804 100644
@@ -86,7 +86,8 @@ OPTIONS
 	This is not a real filter executed for each commit but a one
 	time setup just before the loop. Therefore no commit-specific
 	variables are defined yet.  Functions or variables defined here
-	can be used or modified in the following filter steps.
+	can be used or modified in the following filter steps except
+	the commit filter, for technical reasons.

I'll move that into the previous commit.

Yeah, the description of "technical limitation" is different from clarifying the disambiguating "--" in the documentation.

I am curious what the "technical reason" really is, though ;-)

#4 lore raw

Re: [PATCH 2/2] add [--] to usage of filter-branch

Am 09.06.2017 um 15:14 schrieb Junio C Hamano:

Quoted 23 lines
Quoted 7 lines · Andreas Heiduk

Am 03.06.2017 um 12:17 schrieb Andreas Heiduk:

Quoted 6 lines
---
 Documentation/git-filter-branch.txt | 3 ++-
 git-filter-branch.sh                | 2 +-
 2 files changed, 3 insertions(+), 2 deletions(-)
Quoted 13 lines

Documentation/git-filter-branch.txt OPTIONS · line 90

Quoted 11 lines
Documentation/git-filter-branch.txt +2 -1
index 45c849d8c..1efdda804 100644
@@ -86,7 +86,8 @@ OPTIONS
 	This is not a real filter executed for each commit but a one
 	time setup just before the loop. Therefore no commit-specific
 	variables are defined yet.  Functions or variables defined here
-	can be used or modified in the following filter steps.
+	can be used or modified in the following filter steps except
+	the commit filter, for technical reasons.

I'll move that into the previous commit.

Quoted 5 lines

Yeah, the description of "technical limitation" is different from clarifying the disambiguating "--" in the documentation.

I am curious what the "technical reason" really is, though ;-)

Well, I just picked up the wording from the "Filter" section a couple paragraphs above:

Quoted 3 lines

The filters are applied in the order as listed below. The <command> argument is always evaluated in the shell context using the 'eval' command (with the notable exception of the commit filter, for technical reasons).

Because these reasons exist independently from my change I think I can get away with just that snappy reference :-]

#5 lore raw
In reply to Andreas Heiduk
Quoted 8 lines · Andreas Heiduk

A specific --setup step in git filter-branch makes it much easier to define the initial values of variables used in the real filters. Also sourcing/defining utility functions here instead of --env-filter improves performance and minimizes clogging the output in case of errors.

---

I was placed on To: line, but I do not have a strong opinion on this change, either for or against.

"filter-branch" program itself may probably already be hard to port to C, but I need to point out that this makes it even harder than it currently is [*1*], and it is likely that it has to stay implemented in shell forever, though. I do not mind that future myself, but those on platforms with weaker implementation of shells might.

[Footnote]

*1* The issue is *not* that these individual filter commands expect
<command> written as a shell scriptlet; it is that these scriptlets expect to be evaled inside a single shell process, making an update to a shell variable in one command visible to the next command that runs.

Quoted 87 lines
 Documentation/git-filter-branch.txt | 16 +++++++++++-----
 git-filter-branch.sh                | 18 +++++++++++++-----
 2 files changed, 24 insertions(+), 10 deletions(-)
Documentation/git-filter-branch.txt +11 -5
index 6e4bb0220..45c849d8c 100644
@@ -8,11 +8,11 @@ git-filter-branch - Rewrite branches
 SYNOPSIS
 --------
 [verse]
-'git filter-branch' [--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>]
-	[--prune-empty]
+'git filter-branch' [--setup <command>] [--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>] [--prune-empty]
 	[--original <namespace>] [-d <directory>] [-f | --force]
 	[--] [<rev-list options>...]
 
@@ -82,6 +82,12 @@ multiple commits.
 OPTIONS
 -------
 
+--setup <command>::
+	This is not a real filter executed for each commit but a one
+	time setup just before the loop. Therefore no commit-specific
+	variables are defined yet.  Functions or variables defined here
+	can be used or modified in the following filter steps.
+
 --env-filter <command>::
 	This filter may be used if you only need to modify the environment
 	in which the commit will be performed.  Specifically, you might
git-filter-branch.sh +13 -5
index aafaf708d..2758ae5eb 100755
@@ -81,11 +81,12 @@ set_ident () {
 	finish_ident COMMITTER
 }
 
-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]
+USAGE="[--setup <command>] [--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=
@@ -96,6 +97,7 @@ if [ "$(is_bare_repository)" = false ]; then
 fi
 
 tempdir=.git-rewrite
+filter_setup=
 filter_env=
 filter_tree=
 filter_index=
@@ -148,6 +150,9 @@ do
 	-d)
 		tempdir="$OPTARG"
 		;;
+	--setup)
+		filter_setup="$OPTARG"
+		;;
 	--env-filter)
 		filter_env="$OPTARG"
 		;;
@@ -317,6 +322,9 @@ else
 	need_index=
 fi
 
+eval "$filter_setup" < /dev/null ||
+	die "filter setup failed: $filter_setup"
+
 while read commit parents; do
 	git_filter_branch__commit_count=$(($git_filter_branch__commit_count+1))
#6 lore raw
Quoted 11 lines · Junio C Hamano
Quoted 8 lines · Andreas Heiduk

A specific --setup step in git filter-branch makes it much easier to define the initial values of variables used in the real filters. Also sourcing/defining utility functions here instead of --env-filter improves performance and minimizes clogging the output in case of errors.

---

Quoted 8 lines

I was placed on To: line, but I do not have a strong opinion on this change, either for or against.

"filter-branch" program itself may probably already be hard to port to C, but I need to point out that this makes it even harder than it currently is [*1*], and it is likely that it has to stay implemented in shell forever, though. I do not mind that future myself, but those on platforms with weaker implementation of shells might.

I have a feeling that if we were ever to rewrite filter-branch, it would probably be worth allowing people to write snippets in a better language (possibly even a domain-specific language). I'm sure that most of the program being written in shell doesn't help, but if we're spawning one or more shell instances per commit (plus the Git programs they spawn!), it's always going to be slow.

But I suspect that would be an uphill battle, as our only stable API involves starting external processes anyway. You'd probably do better to pick a language you like and rewrite it in using libgit2's bindings to that language. It's not feature complete, but basic stuff like "put this entry in the tree" is certainly mature.

Quoted 5 lines

*1* The issue is *not* that these individual filter commands expect
<command> written as a shell scriptlet; it is that these scriptlets expect to be evaled inside a single shell process, making an update to a shell variable in one command visible to the next command that runs.

I think you'd need a shell "helper" that's a single long-running process and just reads "eval the index snippet now" instructions from the C controller. At which point I don't think Andreas's "setup" feature is any harder to support. We just send an "eval the setup snippet" instruction first.

I guess one way to think of that is that the C program is itself generating a shell script.

-Peff

#7 lore raw
Quoted 19 lines · Jeff King

I have a feeling that if we were ever to rewrite filter-branch, it would probably be worth allowing people to write snippets in a better language (possibly even a domain-specific language). I'm sure that most of the program being written in shell doesn't help, but if we're spawning one or more shell instances per commit (plus the Git programs they spawn!), it's always going to be slow.

But I suspect that would be an uphill battle, as our only stable API involves starting external processes anyway. You'd probably do better to pick a language you like and rewrite it in using libgit2's bindings to that language. It's not feature complete, but basic stuff like "put this entry in the tree" is certainly mature.

Quoted 5 lines

*1* The issue is *not* that these individual filter commands expect
<command> written as a shell scriptlet; it is that these scriptlets expect to be evaled inside a single shell process, making an update to a shell variable in one command visible to the next command that runs.

Quoted 5 lines

I think you'd need a shell "helper" that's a single long-running process and just reads "eval the index snippet now" instructions from the C controller. At which point I don't think Andreas's "setup" feature is any harder to support. We just send an "eval the setup snippet" instruction first.

Yes. I do not think this particular one makes things any worse than it already is. As I said, I do not have a strong opinion against the topic; as long as people find the feature useful, I do not mind applying it.

Thanks.

#8 lore raw

PATCH v2 1/2 add setup step to filter-branch

In reply to Andreas Heiduk

A specific --setup step in git filter-branch makes it much easier to define the initial values of variables used in the real filters. Also sourcing/defining utility functions here instead of --env-filter improves performance and minimizes clogging the output in case of errors.

---
 Documentation/git-filter-branch.txt | 17 ++++++++++++-----
 git-filter-branch.sh                | 18 +++++++++++++-----
 2 files changed, 25 insertions(+), 10 deletions(-)
Documentation/git-filter-branch.txt +12 -5
index 7b695dbb7..9e5169aa6 100644
@@ -8,11 +8,11 @@ git-filter-branch - Rewrite branches
 SYNOPSIS
 --------
 [verse]
-'git filter-branch' [--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>]
-	[--prune-empty]
+'git filter-branch' [--setup <command>] [--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>] [--prune-empty]
 	[--original <namespace>] [-d <directory>] [-f | --force]
 	[--] [<rev-list options>...]
 
@@ -82,6 +82,13 @@ multiple commits.
 OPTIONS
 -------
 
+--setup <command>::
+	This is not a real filter executed for each commit but a one
+	time setup just before the loop. Therefore no commit-specific
+	variables are defined yet.  Functions or variables defined here
+	can be used or modified in the following filter steps except
+	the commit filter, for technical reasons.
+
 --env-filter <command>::
 	This filter may be used if you only need to modify the environment
 	in which the commit will be performed.  Specifically, you might
git-filter-branch.sh +13 -5
index aafaf708d..2758ae5eb 100755
@@ -81,11 +81,12 @@ set_ident () {
 	finish_ident COMMITTER
 }
 
-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]
+USAGE="[--setup <command>] [--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=
@@ -96,6 +97,7 @@ if [ "$(is_bare_repository)" = false ]; then
 fi
 
 tempdir=.git-rewrite
+filter_setup=
 filter_env=
 filter_tree=
 filter_index=
@@ -148,6 +150,9 @@ do
 	-d)
 		tempdir="$OPTARG"
 		;;
+	--setup)
+		filter_setup="$OPTARG"
+		;;
 	--env-filter)
 		filter_env="$OPTARG"
 		;;
@@ -317,6 +322,9 @@ else
 	need_index=
 fi
 
+eval "$filter_setup" < /dev/null ||
+	die "filter setup failed: $filter_setup"
+
 while read commit parents; do
 	git_filter_branch__commit_count=$(($git_filter_branch__commit_count+1))
 
Signature · 1 line
2.13.0
#9 lore raw

PATCH v2 2/2 add [--] to usage of filter-branch

---
 git-filter-branch.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
git-filter-branch.sh +1 -1
index 2758ae5eb..3a74602ef 100755
@@ -87,7 +87,7 @@ USAGE="[--setup <command>] [--env-filter <command>]
 	[--commit-filter <command>] [--tag-name-filter <command>]
 	[--subdirectory-filter <directory>] [--original <namespace>]
 	[-d <directory>] [-f | --force]
-	[<rev-list options>...]"
+	[--] [<rev-list options>...]"
 
 OPTIONS_SPEC=
 . git-sh-setup
Signature · 1 line
2.13.0