11use std:: process:: Stdio ;
22use std:: { path:: Path , process:: Command } ;
33
4+ pub struct GitConfig < ' a > {
5+ pub github_repository : & ' a str ,
6+ pub nightly_branch : & ' a str ,
7+ }
8+
49/// Runs a command and returns the output
510fn output_result ( cmd : & mut Command ) -> Result < String , String > {
611 let output = match cmd. stderr ( Stdio :: inherit ( ) ) . output ( ) {
@@ -27,7 +32,10 @@ fn output_result(cmd: &mut Command) -> Result<String, String> {
2732/// upstream https://github.com/rust-lang/rust (fetch)
2833/// upstream https://github.com/rust-lang/rust (push)
2934/// ```
30- pub fn get_rust_lang_rust_remote ( git_dir : Option < & Path > ) -> Result < String , String > {
35+ pub fn get_rust_lang_rust_remote (
36+ config : & GitConfig < ' _ > ,
37+ git_dir : Option < & Path > ,
38+ ) -> Result < String , String > {
3139 let mut git = Command :: new ( "git" ) ;
3240 if let Some ( git_dir) = git_dir {
3341 git. current_dir ( git_dir) ;
@@ -37,8 +45,8 @@ pub fn get_rust_lang_rust_remote(git_dir: Option<&Path>) -> Result<String, Strin
3745
3846 let rust_lang_remote = stdout
3947 . lines ( )
40- . find ( |remote| remote. contains ( "rust-lang" ) )
41- . ok_or_else ( || "rust-lang/rust remote not found". to_owned ( ) ) ?;
48+ . find ( |remote| remote. contains ( config . github_repository ) )
49+ . ok_or_else ( || format ! ( "{} remote not found", config . github_repository ) ) ?;
4250
4351 let remote_name =
4452 rust_lang_remote. split ( '.' ) . nth ( 1 ) . ok_or_else ( || "remote name not found" . to_owned ( ) ) ?;
@@ -76,9 +84,13 @@ pub fn rev_exists(rev: &str, git_dir: Option<&Path>) -> Result<bool, String> {
7684/// This could be because the user is updating their forked master branch using the GitHub UI
7785/// and therefore doesn't need an upstream master branch checked out.
7886/// We will then fall back to origin/master in the hope that at least this exists.
79- pub fn updated_master_branch ( git_dir : Option < & Path > ) -> Result < String , String > {
80- let upstream_remote = get_rust_lang_rust_remote ( git_dir) ?;
81- for upstream_master in [ format ! ( "{upstream_remote}/master" ) , format ! ( "origin/master" ) ] {
87+ pub fn updated_master_branch (
88+ config : & GitConfig < ' _ > ,
89+ git_dir : Option < & Path > ,
90+ ) -> Result < String , String > {
91+ let upstream_remote = get_rust_lang_rust_remote ( config, git_dir) ?;
92+ let branch = config. nightly_branch ;
93+ for upstream_master in [ format ! ( "{upstream_remote}/{branch}" ) , format ! ( "origin/{branch}" ) ] {
8294 if rev_exists ( & upstream_master, git_dir) ? {
8395 return Ok ( upstream_master) ;
8496 }
@@ -87,8 +99,11 @@ pub fn updated_master_branch(git_dir: Option<&Path>) -> Result<String, String> {
8799 Err ( format ! ( "Cannot find any suitable upstream master branch" ) )
88100}
89101
90- pub fn get_git_merge_base ( git_dir : Option < & Path > ) -> Result < String , String > {
91- let updated_master = updated_master_branch ( git_dir) ?;
102+ pub fn get_git_merge_base (
103+ config : & GitConfig < ' _ > ,
104+ git_dir : Option < & Path > ,
105+ ) -> Result < String , String > {
106+ let updated_master = updated_master_branch ( config, git_dir) ?;
92107 let mut git = Command :: new ( "git" ) ;
93108 if let Some ( git_dir) = git_dir {
94109 git. current_dir ( git_dir) ;
@@ -100,10 +115,11 @@ pub fn get_git_merge_base(git_dir: Option<&Path>) -> Result<String, String> {
100115/// The `extensions` parameter can be used to filter the files by their extension.
101116/// If `extensions` is empty, all files will be returned.
102117pub fn get_git_modified_files (
118+ config : & GitConfig < ' _ > ,
103119 git_dir : Option < & Path > ,
104120 extensions : & Vec < & str > ,
105121) -> Result < Option < Vec < String > > , String > {
106- let merge_base = get_git_merge_base ( git_dir) ?;
122+ let merge_base = get_git_merge_base ( config , git_dir) ?;
107123
108124 let mut git = Command :: new ( "git" ) ;
109125 if let Some ( git_dir) = git_dir {
@@ -122,8 +138,11 @@ pub fn get_git_modified_files(
122138}
123139
124140/// Returns the files that haven't been added to git yet.
125- pub fn get_git_untracked_files ( git_dir : Option < & Path > ) -> Result < Option < Vec < String > > , String > {
126- let Ok ( _updated_master) = updated_master_branch ( git_dir) else {
141+ pub fn get_git_untracked_files (
142+ config : & GitConfig < ' _ > ,
143+ git_dir : Option < & Path > ,
144+ ) -> Result < Option < Vec < String > > , String > {
145+ let Ok ( _updated_master) = updated_master_branch ( config, git_dir) else {
127146 return Ok ( None ) ;
128147 } ;
129148 let mut git = Command :: new ( "git" ) ;
0 commit comments