Skip to content

Commit 49d6f02

Browse files
committed
filter-repo: clarify interactions between path filtering and path renaming
Signed-off-by: Elijah Newren <[email protected]>
1 parent 3e1bff2 commit 49d6f02

File tree

3 files changed

+101
-1
lines changed

3 files changed

+101
-1
lines changed

Documentation/git-filter-repo.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ Filtering based on paths (see also --filename-callback)
112112
Renaming based on paths (see also --filename-callback)
113113
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
114114

115+
Note: if you combine path filtering with path renaming, be aware that
116+
a rename directive does not select paths, it only says how to
117+
rename paths that are selected with the filters.
118+
115119
--path-rename <old_name:new_name>::
116120
--path-rename-match <old_name:new_name>::
117121
Path to rename; if filename or directory matches <old_name>
@@ -616,6 +620,21 @@ created by the rename so reversing the two would give you an empty repo).
616620
Also, note that the rename of cmds/run_release.sh a couple examples ago was
617621
done before the other renames.
618622

623+
Note that path renaming does not do path filtering, thus the following
624+
command
625+
626+
--------------------------------------------------
627+
git filter-repo --path src/main/ --path-rename tools/:scripts/
628+
--------------------------------------------------
629+
630+
would not result in the tools or scripts directories being present, because
631+
the single filter selected only src/main/. It's likely that you would
632+
instead want to run:
633+
634+
--------------------------------------------------
635+
git filter-repo --path src/main/ --path tools/ --path-rename tools/:scripts/
636+
--------------------------------------------------
637+
619638
If you prefer to filter based solely on basename, use the `--use-base-name`
620639
flag (though this is incompatible with `--path-rename`). For example, to
621640
only include README.md and Makefile files from any directory:

git-filter-repo

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1792,7 +1792,9 @@ EXAMPLES
17921792
action=FilteringOptions.AppendFilter,
17931793
help=_("Path to rename; if filename or directory matches OLD_NAME "
17941794
"rename to NEW_NAME. Multiple --path-rename options can be "
1795-
"specified."))
1795+
"specified. NOTE: If you combine filtering options with "
1796+
"renaming ones, do not rely on a rename argument to select "
1797+
"paths; you also need a filter to select them."))
17961798

17971799
helpers = parser.add_argument_group(title=_("Path shortcuts"))
17981800
helpers.add_argument('--paths-from-file', metavar='FILENAME',

t/t9390-filter-repo.sh

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,85 @@ test_expect_success '--paths-from-file' '
178178
)
179179
'
180180

181+
create_path_filtering_and_renaming() {
182+
test -d path_filtering_and_renaming && return
183+
184+
test_create_repo path_filtering_and_renaming &&
185+
(
186+
cd path_filtering_and_renaming &&
187+
188+
>.gitignore &&
189+
mkdir -p src/main/java/com/org/{foo,bar} &&
190+
mkdir -p src/main/resources &&
191+
test_seq 1 10 >src/main/java/com/org/foo/uptoten &&
192+
test_seq 11 20 >src/main/java/com/org/bar/uptotwenty &&
193+
test_seq 1 7 >src/main/java/com/org/uptoseven &&
194+
test_seq 1 5 >src/main/resources/uptofive &&
195+
git add . &&
196+
git commit -m Initial
197+
)
198+
}
199+
200+
test_expect_success 'Mixing filtering and renaming paths, not enough filters' '
201+
create_path_filtering_and_renaming &&
202+
git clone --no-local path_filtering_and_renaming \
203+
path_filtering_and_renaming_1 &&
204+
(
205+
cd path_filtering_and_renaming_1 &&
206+
207+
git filter-repo --path .gitignore \
208+
--path src/main/resources \
209+
--path-rename src/main/java/com/org/foo/:src/main/java/com/org/ &&
210+
211+
cat <<-EOF >expect &&
212+
.gitignore
213+
src/main/resources/uptofive
214+
EOF
215+
git ls-files >actual &&
216+
test_cmp expect actual
217+
)
218+
'
219+
220+
test_expect_success 'Mixing filtering and renaming paths, enough filters' '
221+
create_path_filtering_and_renaming &&
222+
git clone --no-local path_filtering_and_renaming \
223+
path_filtering_and_renaming_2 &&
224+
(
225+
cd path_filtering_and_renaming_2 &&
226+
227+
git filter-repo --path .gitignore \
228+
--path src/main/resources \
229+
--path src/main/java/com/org/foo/ \
230+
--path-rename src/main/java/com/org/foo/:src/main/java/com/org/ &&
231+
232+
cat <<-EOF >expect &&
233+
.gitignore
234+
src/main/java/com/org/uptoten
235+
src/main/resources/uptofive
236+
EOF
237+
git ls-files >actual &&
238+
test_cmp expect actual
239+
)
240+
'
241+
242+
test_expect_success 'Mixing filtering and to-subdirectory-filter' '
243+
create_path_filtering_and_renaming &&
244+
git clone --no-local path_filtering_and_renaming \
245+
path_filtering_and_renaming_3 &&
246+
(
247+
cd path_filtering_and_renaming_3 &&
248+
249+
git filter-repo --path src/main/resources \
250+
--to-subdirectory-filter my-module &&
251+
252+
cat <<-EOF >expect &&
253+
my-module/src/main/resources/uptofive
254+
EOF
255+
git ls-files >actual &&
256+
test_cmp expect actual
257+
)
258+
'
259+
181260
test_expect_success 'setup metasyntactic repo' '
182261
test_create_repo metasyntactic &&
183262
(

0 commit comments

Comments
 (0)