Skip to content

Commit 47c5a29

Browse files
committed
Merge branch 'sb/callback-from-file'
Signed-off-by: Elijah Newren <[email protected]>
2 parents a10fa46 + 5256c99 commit 47c5a29

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

git-filter-repo

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1708,6 +1708,9 @@ class FilteringOptions(object):
17081708
To remove all .DS_Store files:
17091709
git filter-repo --filename-callback 'return None if os.path.basename(filename) == b".DS_Store" else filename'
17101710
1711+
Note that if BODY resolves to a filename, then the contents of that file
1712+
will be used as the BODY in the callback function.
1713+
17111714
For more detailed examples and explanations AND caveats, see
17121715
https://htmlpreview.github.io/?https://github.com/newren/git-filter-repo/blob/docs/html/git-filter-repo.html#CALLBACKS
17131716
@@ -1904,32 +1907,32 @@ EXAMPLES
19041907
"always use merge --no-ff."))
19051908

19061909
callback = parser.add_argument_group(title=_("Generic callback code snippets"))
1907-
callback.add_argument('--filename-callback', metavar="FUNCTION_BODY",
1910+
callback.add_argument('--filename-callback', metavar="FUNCTION_BODY_OR_FILE",
19081911
help=_("Python code body for processing filenames; see CALLBACKS "
19091912
"sections below."))
1910-
callback.add_argument('--message-callback', metavar="FUNCTION_BODY",
1913+
callback.add_argument('--message-callback', metavar="FUNCTION_BODY_OR_FILE",
19111914
help=_("Python code body for processing messages (both commit "
19121915
"messages and tag messages); see CALLBACKS section below."))
1913-
callback.add_argument('--name-callback', metavar="FUNCTION_BODY",
1916+
callback.add_argument('--name-callback', metavar="FUNCTION_BODY_OR_FILE",
19141917
help=_("Python code body for processing names of people; see "
19151918
"CALLBACKS section below."))
1916-
callback.add_argument('--email-callback', metavar="FUNCTION_BODY",
1919+
callback.add_argument('--email-callback', metavar="FUNCTION_BODY_OR_FILE",
19171920
help=_("Python code body for processing emails addresses; see "
19181921
"CALLBACKS section below."))
1919-
callback.add_argument('--refname-callback', metavar="FUNCTION_BODY",
1922+
callback.add_argument('--refname-callback', metavar="FUNCTION_BODY_OR_FILE",
19201923
help=_("Python code body for processing refnames; see CALLBACKS "
19211924
"section below."))
19221925

1923-
callback.add_argument('--blob-callback', metavar="FUNCTION_BODY",
1926+
callback.add_argument('--blob-callback', metavar="FUNCTION_BODY_OR_FILE",
19241927
help=_("Python code body for processing blob objects; see "
19251928
"CALLBACKS section below."))
1926-
callback.add_argument('--commit-callback', metavar="FUNCTION_BODY",
1929+
callback.add_argument('--commit-callback', metavar="FUNCTION_BODY_OR_FILE",
19271930
help=_("Python code body for processing commit objects; see "
19281931
"CALLBACKS section below."))
1929-
callback.add_argument('--tag-callback', metavar="FUNCTION_BODY",
1932+
callback.add_argument('--tag-callback', metavar="FUNCTION_BODY_OR_FILE",
19301933
help=_("Python code body for processing tag objects; see CALLBACKS "
19311934
"section below."))
1932-
callback.add_argument('--reset-callback', metavar="FUNCTION_BODY",
1935+
callback.add_argument('--reset-callback', metavar="FUNCTION_BODY_OR_FILE",
19331936
help=_("Python code body for processing reset objects; see "
19341937
"CALLBACKS section below."))
19351938

@@ -2809,6 +2812,9 @@ class RepoFilter(object):
28092812
callback_field = '_{}_callback'.format(type)
28102813
code_string = getattr(self._args, type+'_callback')
28112814
if code_string:
2815+
if os.path.exists(code_string):
2816+
with open(code_string, 'r', encoding='utf-8') as f:
2817+
code_string = f.read()
28122818
if getattr(self, callback_field):
28132819
raise SystemExit(_("Error: Cannot pass a %s_callback to RepoFilter "
28142820
"AND pass --%s-callback"

t/t9392-python-callback.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,4 +181,16 @@ test_expect_success 'callback has return statement sanity check' '
181181
)
182182
'
183183

184+
test_expect_success 'Callback read from a file' '
185+
setup name-callback-from-file &&
186+
(
187+
cd name-callback-from-file &&
188+
echo "return name.replace(b\"N.\", b\"And\")" >../name-func &&
189+
git filter-repo --name-callback ../name-func &&
190+
git log --format=%an >log-person-names &&
191+
grep Copy.And.Paste log-person-names
192+
)
193+
'
194+
195+
184196
test_done

0 commit comments

Comments
 (0)