From b6df2a70f0ef9013bbbbfca342c29d165ea467fa Mon Sep 17 00:00:00 2001
From: Pietro Albini <pietro.albini@ferrous-systems.com>
Date: Thu, 30 Jun 2022 09:54:51 +0200
Subject: [PATCH 1/2] remove the need to update bump-stage0 with new stage0
 config fields

---
 src/tools/bump-stage0/Cargo.toml  | 2 +-
 src/tools/bump-stage0/src/main.rs | 9 ++++++---
 2 files changed, 7 insertions(+), 4 deletions(-)

diff --git a/src/tools/bump-stage0/Cargo.toml b/src/tools/bump-stage0/Cargo.toml
index cf8840ff6ee89..352a855561417 100644
--- a/src/tools/bump-stage0/Cargo.toml
+++ b/src/tools/bump-stage0/Cargo.toml
@@ -10,5 +10,5 @@ anyhow = "1.0.34"
 curl = "0.4.38"
 indexmap = { version = "1.7.0", features = ["serde"] }
 serde = { version = "1.0.125", features = ["derive"] }
-serde_json = "1.0.59"
+serde_json = { version = "1.0.59", features = ["preserve_order"] }
 toml = "0.5.7"
diff --git a/src/tools/bump-stage0/src/main.rs b/src/tools/bump-stage0/src/main.rs
index 1c839fdc00a08..aa346daf7e5f3 100644
--- a/src/tools/bump-stage0/src/main.rs
+++ b/src/tools/bump-stage0/src/main.rs
@@ -198,9 +198,12 @@ struct Stage0 {
 #[derive(Debug, serde::Serialize, serde::Deserialize)]
 struct Config {
     dist_server: String,
-    artifacts_server: String,
-    artifacts_with_llvm_assertions_server: String,
-    git_merge_commit_email: String,
+    // There are other fields in the configuration, which will be read by src/bootstrap or other
+    // tools consuming stage0.json. To avoid the need to update bump-stage0 every time a new field
+    // is added, we collect all the fields in an untyped Value and serialize them back with the
+    // same order and structure they were deserialized in.
+    #[serde(flatten)]
+    other: serde_json::Value,
 }
 
 #[derive(Debug, serde::Serialize, serde::Deserialize)]

From 6bc97d0adcc7e9bd828de1ccf9c9ffc9b30165ee Mon Sep 17 00:00:00 2001
From: Pietro Albini <pietro.albini@ferrous-systems.com>
Date: Mon, 11 Jul 2022 10:15:13 +0200
Subject: [PATCH 2/2]  configure nightly branch name in stage0.json

---
 src/bootstrap/config.rs |  1 +
 src/bootstrap/lib.rs    | 13 +++++--------
 src/stage0.json         |  3 ++-
 3 files changed, 8 insertions(+), 9 deletions(-)

diff --git a/src/bootstrap/config.rs b/src/bootstrap/config.rs
index 39d86ccbdbf34..62dd9a6b36502 100644
--- a/src/bootstrap/config.rs
+++ b/src/bootstrap/config.rs
@@ -226,6 +226,7 @@ pub struct Stage0Config {
     pub artifacts_server: String,
     pub artifacts_with_llvm_assertions_server: String,
     pub git_merge_commit_email: String,
+    pub nightly_branch: String,
 }
 #[derive(Default, Deserialize)]
 #[cfg_attr(test, derive(Clone))]
diff --git a/src/bootstrap/lib.rs b/src/bootstrap/lib.rs
index c1190c9192d1a..cd421c249d8da 100644
--- a/src/bootstrap/lib.rs
+++ b/src/bootstrap/lib.rs
@@ -1280,14 +1280,11 @@ impl Build {
         // Figure out how many merge commits happened since we branched off master.
         // That's our beta number!
         // (Note that we use a `..` range, not the `...` symmetric difference.)
-        let count = output(
-            self.config
-                .git()
-                .arg("rev-list")
-                .arg("--count")
-                .arg("--merges")
-                .arg("refs/remotes/origin/master..HEAD"),
-        );
+        let count =
+            output(self.config.git().arg("rev-list").arg("--count").arg("--merges").arg(format!(
+                "refs/remotes/origin/{}..HEAD",
+                self.config.stage0_metadata.config.nightly_branch
+            )));
         let n = count.trim().parse().unwrap();
         self.prerelease_version.set(Some(n));
         n
diff --git a/src/stage0.json b/src/stage0.json
index 64b8aca3960ce..ab8a79e2af595 100644
--- a/src/stage0.json
+++ b/src/stage0.json
@@ -3,7 +3,8 @@
     "dist_server": "https://static.rust-lang.org",
     "artifacts_server": "https://ci-artifacts.rust-lang.org/rustc-builds",
     "artifacts_with_llvm_assertions_server": "https://ci-artifacts.rust-lang.org/rustc-builds-alt",
-    "git_merge_commit_email": "bors@rust-lang.org"
+    "git_merge_commit_email": "bors@rust-lang.org",
+    "nightly_branch": "master"
   },
   "__comments": [
     "The configuration above this comment is editable, and can be changed",