From d248d090e3ed26cbb37cec9271f33e0c985ac8a7 Mon Sep 17 00:00:00 2001 From: Elsa Ferrara Date: Mon, 1 Jul 2024 14:53:40 +0200 Subject: [PATCH] Add git_shallow_fetch_since to checkout.py --- src/e3/anod/checkout.py | 17 +++++++++--- tests/tests_e3/anod/checkout_test.py | 39 ++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/src/e3/anod/checkout.py b/src/e3/anod/checkout.py index 98cebe93..bb7629ce 100644 --- a/src/e3/anod/checkout.py +++ b/src/e3/anod/checkout.py @@ -243,15 +243,26 @@ def update_git( try: old_commit = g.rev_parse() + shallow_cmd: str | None = None + # Using fetch + checkout ensure caching is effective - shallow = "git_shallow_fetch" in os.environ.get( + shallow_fetch = "git_shallow_fetch" in os.environ.get( "E3_ENABLE_FEATURE", "" - ).split(",") and (not self.compute_changelog or not old_commit) + ).split(",") + + if shallow_fetch and (not self.compute_changelog or not old_commit): + shallow_cmd = "--depth=1" + + for feature in os.environ.get("E3_ENABLE_FEATURE", "").split(","): + if "git_fetch_shallow_since" in feature: + date = feature.replace("git_fetch_shallow_since=", "") + shallow_cmd = f"--shallow-since={date}" + g.git_cmd( [ "fetch", "-f", - "--depth=1" if shallow else None, + shallow_cmd, remote_name, f"{revision}:refs/e3-checkout", ] diff --git a/tests/tests_e3/anod/checkout_test.py b/tests/tests_e3/anod/checkout_test.py index 63be6760..f62904fa 100644 --- a/tests/tests_e3/anod/checkout_test.py +++ b/tests/tests_e3/anod/checkout_test.py @@ -154,3 +154,42 @@ def test_svn_checkout(self, svn, compute_changelog, e3_feature): result = m.update(vcs="external", url=os.path.abspath("git2")) assert result == ReturnValue.unchanged + + def test_shallow_since_checkout(self): + os.environ["GIT_AUTHOR_EMAIL"] = "e3-core@example.net" + os.environ["GIT_AUTHOR_NAME"] = "e3 core" + os.environ["GIT_COMMITTER_NAME"] = "e3 core" + os.environ["GIT_COMMITTER_EMAIL"] = "e3-core@example.net" + os.environ["E3_ENABLE_FEATURE"] = "git_fetch_shallow_since=2020-08-04" + os.environ["GIT_COMMITTER_DATE"] = "2020-08-01T22:13:13" + + url = GitRepository.create("git3") + + with open(os.path.join("git3", "file3.txt"), "w") as fd: + fd.write("first file!") + with open(os.path.join("git3", "file4.txt"), "w") as fd: + fd.write("second file!") + + m = CheckoutManager(name="myrepo", working_dir=".") + + r = GitRepository(os.path.abspath("git3")) + + r.git_cmd(["add", "file3.txt"]) + r.git_cmd(["commit", "-m", "first commit", "--date", "2020-08-01T22:13:13"]) + + os.environ["GIT_COMMITTER_DATE"] = "2020-08-05T22:13:13" + r.git_cmd(["add", "file4.txt"]) + r.git_cmd(["commit", "-m", "second commit", "--date", "2020-08-05T22:13:13"]) + + result = m.update(vcs="git", url=url, revision="master") + + myrepo = GitRepository(os.path.abspath("myrepo")) + myrepo.git_cmd(["log", "--pretty=format:%s"], output="log.txt") + + with open("log.txt", "r") as fd: + log = fd.readlines() + + assert result == ReturnValue.success + assert os.path.isfile(os.path.join("myrepo", "file3.txt")) + assert os.path.isfile(os.path.join("myrepo", "file4.txt")) + assert log == ["second commit"]