Skip to content

Commit

Permalink
[CI] Replace filter-branch with a faster filter on top of fast-export…
Browse files Browse the repository at this point in the history
…/fast-import
  • Loading branch information
glandium committed Apr 22, 2023
1 parent 20c42b7 commit 3ee4872
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 4 deletions.
60 changes: 60 additions & 0 deletions CI/filter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import sys


class DataCommand:
def __init__(self, data):
self.data = data

def write_to(self, out):
out.write(b"data %d\n" % len(self.data))
out.write(self.data)


def iter_commands(input=sys.stdin.buffer):
for line in input:
if line.startswith(b"data "):
_, length = line.split()
length = int(length)
data = input.read(length)
assert len(data) == length
yield DataCommand(data)
else:
yield line


def write_command(command, out=sys.stdout.buffer):
if isinstance(command, DataCommand):
command.write_to(out)
else:
out.write(command)


if __name__ == "__main__":
args = sys.argv[1:]
for arg in args:
if arg not in ["--commits", "--roots"]:
print(f"Unsupported options: {args}")
sys.exit(1)

commands = iter_commands()
for command in commands:
if isinstance(command, DataCommand):
if "--commits" in args:
command.data += b"\n"
if "--roots" in args:
write_command(command)
while True:
command = next(commands)
# No "from" command, so this is a root, remove all the
# files from it.
if not command.startswith((b"deleteall", b"M ")):
break

elif command.startswith((b"author <", b"committer <")):
cmd, email = command.split(b"<", 1)
command = cmd[:-1] + email.split(b"@", 1)[0] + b" <" + email
write_command(command)
7 changes: 3 additions & 4 deletions CI/tests.mk
Original file line number Diff line number Diff line change
Expand Up @@ -236,12 +236,11 @@ hg.graft.base.git hg.graft2.base.git: hg.upgraded.git hg.pure.hg
$(GIT) init $@
$(GIT) -C $@ remote add origin hg::$(PATH_URL)/$(word 2,$^)
$(GIT) -C $< push $(CURDIR)/$@ refs/remotes/*:refs/remotes/*
$(GIT) -C $@ checkout $$($(GIT) -C $< rev-parse HEAD)

hg.graft.git: hg.graft.base.git hg.upgraded.git
cp -r $< $@
$(GIT) -C $@ cinnabar rollback 0000000000000000000000000000000000000000
$(GIT) -c user.email=foo@bar -C $@ filter-branch --msg-filter 'cat ; echo' --original original -- --all
$(GIT) -C $@ fast-export --no-data --all | python3 $(TOPDIR)/CI/filter.py --commits | git -c core.ignorecase=false -C $@ fast-import --force
$(GIT) -C $@ -c cinnabar.graft=true remote update
$(call COMPARE_REFS, $(word 2,$^), $@, XARGS_GIT2HG)
$(GIT) -C $@ cinnabar fsck --full
Expand All @@ -254,10 +253,10 @@ hg.graft2.git: hg.graft.git hg.pure.hg hg.graft2.base.git
$(call COMPARE_REFS, $<, $@)
$(GIT) -C $@ cinnabar fsck --full

hg.graft.replace.git: hg.graft.git hg.upgraded.git
hg.graft.replace.git: hg.graft.base.git hg.upgraded.git
cp -r $< $@
$(GIT) -C $@ cinnabar rollback 0000000000000000000000000000000000000000
$(GIT) -c user.email=foo@bar -C $@ filter-branch --index-filter 'test $$GIT_COMMIT = '$$($(call GET_ROOTS,$@,--remotes))' && git rm -r --cached -- \* || true' --original original -- --all
$(GIT) -C $@ fast-export --no-data --full-tree --all | python3 $(TOPDIR)/CI/filter.py --commits --roots | git -c core.ignorecase=false -C $@ fast-import --force
$(GIT) -C $@ -c cinnabar.graft=true remote update
$(call COMPARE_REFS, $(word 2,$^), $@, XARGS_GIT2HG)
$(call COMPARE_COMMANDS,$(call GET_ROOTS,$(word 2,$^),--remotes),$(call GET_ROOTS,$@,--glob=refs/cinnabar/replace))
Expand Down

0 comments on commit 3ee4872

Please sign in to comment.