Skip to content

Commit

Permalink
filter-repo: handle locally modified refs vs. automatic fetch of --sdr
Browse files Browse the repository at this point in the history
Signed-off-by: Elijah Newren <[email protected]>
  • Loading branch information
newren committed Nov 21, 2024
1 parent 9674d53 commit 20cca91
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
21 changes: 20 additions & 1 deletion git-filter-repo
Original file line number Diff line number Diff line change
Expand Up @@ -4368,6 +4368,7 @@ class RepoFilter(object):
target_working_dir = self._args.target or b'.'
refs_to_migrate = set(x for x in self._orig_refs
if x.startswith(b'refs/remotes/origin/'))
refs_to_warn_about = set()
if refs_to_migrate:
if self._args.debug:
print("[DEBUG] Migrating refs/remotes/origin/* -> refs/heads/*")
Expand All @@ -4381,8 +4382,10 @@ class RepoFilter(object):
newref = ref.replace(b'refs/remotes/origin/', b'refs/heads/')
if newref not in self._orig_refs:
p.stdin.write(b'create %s %s\n' % (newref, self._orig_refs[ref]))
self._orig_refs[newref] = self._orig_refs[ref]
elif self._orig_refs[ref] != self._orig_refs[newref]:
refs_to_warn_about.add(newref)
p.stdin.write(b'delete %s %s\n' % (ref, self._orig_refs[ref]))
self._orig_refs[newref] = self._orig_refs[ref]
del self._orig_refs[ref]
p.stdin.close()
if p.wait(): # pragma: no cover
Expand All @@ -4399,6 +4402,22 @@ class RepoFilter(object):
not self._args.no_fetch and \
not self._already_ran and \
self._config_settings.get(b'remote.origin.mirror', b'false') != b'true':

if refs_to_warn_about:
msg = ("Warning: You have refs modified from upstream:\n " +
"\n ".join([decode(x) for x in refs_to_warn_about]) +
"\n" +
" We want to forcibly fetch from upstream to ensure\n" +
" that all relevent refs are rewritten, but this will\n" +
" discard your local changes before starting the\n" +
" rewrite. Proceed with fetch (Y/N)?")
response = input(msg)

if response.lower() != 'y':
self._args.no_fetch = True
# Don't do the fetch, and don't remove the origin remote
return

cmd = 'git fetch -q --prune --update-head-ok --refmap "" origin +refs/*:refs/*'
m = _("NOTICE: Fetching all refs from origin to make sure we rewrite\n"
" all history that may reference the sensitive data, via\n"
Expand Down
22 changes: 22 additions & 0 deletions t/t9393-filter-repo-rerun.sh
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,28 @@ test_expect_success 'sdr: interaction with fetch and notes and stashes' '
)
'

test_expect_success 'sdr: handling local-only changes' '
test_create_repo sdr_with_local_only_changes &&
(
cd sdr_with_local_only_changes &&
git fast-import --quiet <$DATA/simple &&
git clone "file://$(pwd)" fresh_clone &&
cd fresh_clone &&
echo stuff >>fileB &&
git commit -m "random changes" fileB &&
echo n | git filter-repo --sdr --path fileB --force >../output &&
grep "You have refs modified from upstream" ../output &&
git log -1 --format=%s fileB >actual &&
echo "random changes" >expect &&
test_cmp expect actual
)
'

test_expect_success 'lfs: not in use, no files to process' '
test_create_repo no_lfs_files_to_process &&
(
Expand Down

0 comments on commit 20cca91

Please sign in to comment.