Skip to content

Commit

Permalink
Add git_shallow_fetch_since to checkout.py
Browse files Browse the repository at this point in the history
  • Loading branch information
elsaferrara committed Jul 2, 2024
1 parent 8f36db9 commit eba8023
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/e3/anod/checkout.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]
Expand Down
42 changes: 42 additions & 0 deletions tests/tests_e3/anod/checkout_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

0 comments on commit eba8023

Please sign in to comment.