From a4fbb8edd09a30af2d5c6001b4e84e3d499f72e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20K=C4=99pie=C5=84?= Date: Thu, 7 Dec 2023 13:23:22 +0100 Subject: [PATCH] Improve determining the lines added by a branch Since the list of lines added to Git-tracked text files in a given branch is not part of the Danger DSL [1], it is determined using custom code in dangerfile.py. The current implementation of that logic is less than perfect as it examines the diff between the current tip of the target branch and the source branch rather than the diff between the merge base of the two branches and the source branch. Consider a Git history like this: * F (target) ... * E * D * C | * B (source) |/ * A (merge base) If danger-python or Hazard are run for commit B, the current logic for determining the list of added lines in dangerfile.py examines the diff between commits F and B rather than between commits A and B. Therefore, the added_lines() function returns not just the lines added by commit B on top of commit A, but also the list of lines that were removed between commits A and F, which leads to confusing results. Fix by using the triple-dot diff operator in the Git invocation whose output is used as the source of information for determining the list of lines added by a given branch. Since Hazard fetches the target branch itself when it is run, remove the explicit "git fetch" invocation that fetches the target branch from GitLab (shortening its local history to a single commit in the process) before "git diff" is invoked. [1] https://danger.systems/js/reference.html#GitDSL (cherry picked from commit 43126e81e620deb63889d0812b2186af22da82bd) --- dangerfile.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/dangerfile.py b/dangerfile.py index ee77f13cd1..531e05bb18 100644 --- a/dangerfile.py +++ b/dangerfile.py @@ -23,11 +23,13 @@ import gitlab def added_lines(target_branch, paths): import subprocess - subprocess.check_output( - ["/usr/bin/git", "fetch", "--depth", "1", "origin", target_branch] - ) + # Hazard fetches the target branch itself, so there is no need to fetch it + # explicitly using `git fetch --depth 1000 origin `. The + # refs/remotes/origin/ ref is also expected to be readily + # usable by the time this file is executed. + diff = subprocess.check_output( - ["/usr/bin/git", "diff", "FETCH_HEAD..", "--"] + paths + ["/usr/bin/git", "diff", f"origin/{target_branch}...", "--"] + paths ) added_lines = [] for line in diff.splitlines():