Skip to content

Commit be864c0

Browse files
authored
Rollup merge of rust-lang#131358 - onur-ozkan:129528, r=Mark-Simulacrum
force "HEAD" for non-CI and `git_upstream_merge_base` for CI environment When rust-lang/rust is configured as remote, some of the git logic (for tracking changed files) that uses get_closest_merge_commit starts to produce annoying results as the upstream branch becomes outdated quickly (since it isn't updated with git pull). We can rely on HEAD for non-CI environments as we specifically treat bors commits as merge commits, which also exist on upstream. As for CI environments, we should use `git_upstream_merge_base` to correctly track modified files as bors commits may be in `HEAD` but not yet on the upstream remote. This is also an alternative fix for rust-lang#129528 since rust-lang#131331 reverts the previous fix attempts.
2 parents 77c52ae + 4454fa9 commit be864c0

File tree

4 files changed

+52
-3
lines changed

4 files changed

+52
-3
lines changed

.github/workflows/ci.yml

+3
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ jobs:
122122
# which then uses log commands to actually set them.
123123
EXTRA_VARIABLES: ${{ toJson(matrix.env) }}
124124

125+
- name: setup upstream remote
126+
run: src/ci/scripts/setup-upstream-remote.sh
127+
125128
- name: ensure the channel matches the target branch
126129
run: src/ci/scripts/verify-channel.sh
127130

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/bin/bash
2+
# In CI environments, bootstrap is forced to use the remote upstream based
3+
# on "git_repository" and "nightly_branch" values from src/stage0 file.
4+
# This script configures the remote as it may not exist by default.
5+
6+
set -euo pipefail
7+
IFS=$'\n\t'
8+
9+
ci_dir=$(cd $(dirname $0) && pwd)/..
10+
source "$ci_dir/shared.sh"
11+
12+
git_repository=$(parse_stage0_file_by_key "git_repository")
13+
nightly_branch=$(parse_stage0_file_by_key "nightly_branch")
14+
15+
# Configure "rust-lang/rust" upstream remote only when it's not origin.
16+
if [ -z "$(git config remote.origin.url | grep $git_repository)" ]; then
17+
echo "Configuring https://github.com/$git_repository remote as upstream."
18+
git remote add upstream "https://github.com/$git_repository"
19+
REMOTE_NAME="upstream"
20+
else
21+
REMOTE_NAME="origin"
22+
fi
23+
24+
git fetch $REMOTE_NAME $nightly_branch

src/ci/shared.sh

+12
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,15 @@ function releaseChannel {
136136
echo $RUST_CI_OVERRIDE_RELEASE_CHANNEL
137137
fi
138138
}
139+
140+
# Parse values from src/stage0 file by key
141+
function parse_stage0_file_by_key {
142+
local key="$1"
143+
local file="$ci_dir/../stage0"
144+
local value=$(awk -F= '{a[$1]=$2} END {print(a["'$key'"])}' $file)
145+
if [ -z "$value" ]; then
146+
echo "ERROR: Key '$key' not found in '$file'."
147+
exit 1
148+
fi
149+
echo "$value"
150+
}

src/tools/build_helper/src/git.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use std::path::{Path, PathBuf};
22
use std::process::{Command, Stdio};
33

4+
use crate::ci::CiEnv;
5+
46
pub struct GitConfig<'a> {
57
pub git_repository: &'a str,
68
pub nightly_branch: &'a str,
@@ -114,8 +116,8 @@ fn git_upstream_merge_base(
114116

115117
/// Searches for the nearest merge commit in the repository that also exists upstream.
116118
///
117-
/// If it fails to find the upstream remote, it then looks for the most recent commit made
118-
/// by the merge bot by matching the author's email address with the merge bot's email.
119+
/// It looks for the most recent commit made by the merge bot by matching the author's email
120+
/// address with the merge bot's email.
119121
pub fn get_closest_merge_commit(
120122
git_dir: Option<&Path>,
121123
config: &GitConfig<'_>,
@@ -127,7 +129,15 @@ pub fn get_closest_merge_commit(
127129
git.current_dir(git_dir);
128130
}
129131

130-
let merge_base = git_upstream_merge_base(config, git_dir).unwrap_or_else(|_| "HEAD".into());
132+
let merge_base = {
133+
if CiEnv::is_ci() {
134+
git_upstream_merge_base(config, git_dir).unwrap()
135+
} else {
136+
// For non-CI environments, ignore rust-lang/rust upstream as it usually gets
137+
// outdated very quickly.
138+
"HEAD".to_string()
139+
}
140+
};
131141

132142
git.args([
133143
"rev-list",

0 commit comments

Comments
 (0)