Auto merge of #117122 - ferrocene:pa-configure-git-diff, r=albertlars… · rust-lang/rust@4c8862b (original) (raw)

1

1

`use std::process::Stdio;

`

2

2

`use std::{path::Path, process::Command};

`

3

3

``

``

4

`+

pub struct GitConfig<'a> {

`

``

5

`+

pub git_repository: &'a str,

`

``

6

`+

pub nightly_branch: &'a str,

`

``

7

`+

}

`

``

8

+

4

9

`/// Runs a command and returns the output

`

5

10

`fn output_result(cmd: &mut Command) -> Result<String, String> {

`

6

11

`let output = match cmd.stderr(Stdio::inherit()).output() {

`

`@@ -27,7 +32,10 @@ fn output_result(cmd: &mut Command) -> Result<String, String> {

`

27

32

`/// upstream https://github.com/rust-lang/rust (fetch)

`

28

33

`/// upstream https://github.com/rust-lang/rust (push)

`

29

34

```` /// ```

````

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> {

`

31

39

`let mut git = Command::new("git");

`

32

40

`if let Some(git_dir) = git_dir {

`

33

41

` git.current_dir(git_dir);

`

`@@ -37,8 +45,8 @@ pub fn get_rust_lang_rust_remote(git_dir: Option<&Path>) -> Result<String, Strin

`

37

45

``

38

46

`let rust_lang_remote = stdout

`

39

47

`.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.git_repository))

`

``

49

`+

.ok_or_else(|| format!("{} remote not found", config.git_repository))?;

`

42

50

``

43

51

`let remote_name =

`

44

52

` 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> {

`

76

84

`/// This could be because the user is updating their forked master branch using the GitHub UI

`

77

85

`/// and therefore doesn't need an upstream master branch checked out.

`

78

86

`/// 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}")] {

`

82

94

`if rev_exists(&upstream_master, git_dir)? {

`

83

95

`return Ok(upstream_master);

`

84

96

`}

`

`@@ -87,8 +99,11 @@ pub fn updated_master_branch(git_dir: Option<&Path>) -> Result<String, String> {

`

87

99

`Err(format!("Cannot find any suitable upstream master branch"))

`

88

100

`}

`

89

101

``

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)?;

`

92

107

`let mut git = Command::new("git");

`

93

108

`if let Some(git_dir) = git_dir {

`

94

109

` git.current_dir(git_dir);

`

`@@ -100,10 +115,11 @@ pub fn get_git_merge_base(git_dir: Option<&Path>) -> Result<String, String> {

`

100

115

`` /// The extensions parameter can be used to filter the files by their extension.

``

101

116

`` /// If extensions is empty, all files will be returned.

``

102

117

`pub fn get_git_modified_files(

`

``

118

`+

config: &GitConfig<'_>,

`

103

119

`git_dir: Option<&Path>,

`

104

120

`extensions: &Vec<&str>,

`

105

121

`) -> Result<Option<Vec>, String> {

`

106

``

`-

let merge_base = get_git_merge_base(git_dir)?;

`

``

122

`+

let merge_base = get_git_merge_base(config, git_dir)?;

`

107

123

``

108

124

`let mut git = Command::new("git");

`

109

125

`if let Some(git_dir) = git_dir {

`

`@@ -122,8 +138,11 @@ pub fn get_git_modified_files(

`

122

138

`}

`

123

139

``

124

140

`/// 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> {

`

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> {

`

``

145

`+

let Ok(_updated_master) = updated_master_branch(config, git_dir) else {

`

127

146

`return Ok(None);

`

128

147

`};

`

129

148

`let mut git = Command::new("git");

`