From 6ff72045dee098e5a95f42c27b2f2cf167f0d52d Mon Sep 17 00:00:00 2001
From: Taylor Yu <tlyu@mit.edu>
Date: Thu, 7 Oct 2021 17:58:52 -0500
Subject: [PATCH 1/2] bootstrap: don't use `--merges` to find commits

Shallow clones can cause `git rev-list --merges` to miss merge
commits. Omit it, because the most recent bors commit is
almost always a merge commit.
---
 src/bootstrap/bootstrap.py | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/src/bootstrap/bootstrap.py b/src/bootstrap/bootstrap.py
index 05d7b0f611f72..49121869282d3 100644
--- a/src/bootstrap/bootstrap.py
+++ b/src/bootstrap/bootstrap.py
@@ -460,7 +460,7 @@ def download_toolchain(self, stage0=True, rustc_channel=None):
             # LLVM more often than necessary.
             #
             # This git command finds that commit SHA, looking for bors-authored
-            # merges that modified src/llvm-project or other relevant version
+            # commits that modified src/llvm-project or other relevant version
             # stamp files.
             #
             # This works even in a repository that has not yet initialized
@@ -470,7 +470,7 @@ def download_toolchain(self, stage0=True, rustc_channel=None):
             ]).decode(sys.getdefaultencoding()).strip()
             llvm_sha = subprocess.check_output([
                 "git", "rev-list", "--author=bors@rust-lang.org", "-n1",
-                "--merges", "--first-parent", "HEAD",
+                "--first-parent", "HEAD",
                 "--",
                 "{}/src/llvm-project".format(top_level),
                 "{}/src/bootstrap/download-ci-llvm-stamp".format(top_level),
@@ -685,7 +685,7 @@ def maybe_download_ci_toolchain(self):
         # Only commits merged by bors will have CI artifacts.
         merge_base = [
             "git", "rev-list", "--author=bors@rust-lang.org", "-n1",
-            "--merges", "--first-parent", "HEAD"
+            "--first-parent", "HEAD"
         ]
         commit = subprocess.check_output(merge_base, universal_newlines=True).strip()
 

From 8e467421decb43a1f1adc353c4984df7eecd0af7 Mon Sep 17 00:00:00 2001
From: Taylor Yu <tlyu@mit.edu>
Date: Thu, 7 Oct 2021 18:20:38 -0500
Subject: [PATCH 2/2] bootstrap: add error messages re shallow history

Exit with an error if we can't find a commit hash for downloading
LLVM or rustc snapshots.
---
 src/bootstrap/bootstrap.py | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/src/bootstrap/bootstrap.py b/src/bootstrap/bootstrap.py
index 49121869282d3..817850b1ec7eb 100644
--- a/src/bootstrap/bootstrap.py
+++ b/src/bootstrap/bootstrap.py
@@ -540,6 +540,12 @@ def _download_component_helper(
         unpack(tarball, tarball_suffix, self.bin_root(stage0), match=pattern, verbose=self.verbose)
 
     def _download_ci_llvm(self, llvm_sha, llvm_assertions):
+        if not llvm_sha:
+            print("error: could not find commit hash for downloading LLVM")
+            print("help: maybe your repository history is too shallow?")
+            print("help: consider disabling `download-ci-llvm`")
+            print("help: or fetch enough history to include one upstream commit")
+            exit(1)
         cache_prefix = "llvm-{}-{}".format(llvm_sha, llvm_assertions)
         cache_dst = os.path.join(self.build_dir, "cache")
         rustc_cache = os.path.join(cache_dst, cache_prefix)
@@ -688,6 +694,12 @@ def maybe_download_ci_toolchain(self):
             "--first-parent", "HEAD"
         ]
         commit = subprocess.check_output(merge_base, universal_newlines=True).strip()
+        if not commit:
+            print("error: could not find commit hash for downloading rustc")
+            print("help: maybe your repository history is too shallow?")
+            print("help: consider disabling `download-rustc`")
+            print("help: or fetch enough history to include one upstream commit")
+            exit(1)
 
         # Warn if there were changes to the compiler or standard library since the ancestor commit.
         status = subprocess.call(["git", "diff-index", "--quiet", commit, "--", compiler, library])