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..b26ae088 100644 --- a/tests/tests_e3/anod/checkout_test.py +++ b/tests/tests_e3/anod/checkout_test.py @@ -154,3 +154,45 @@ 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") + print("test") + r.git_cmd(["log"]) + print("test") + + 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"]