-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathno_merges.rs
More file actions
163 lines (144 loc) · 4.38 KB
/
Copy pathno_merges.rs
File metadata and controls
163 lines (144 loc) · 4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
//! Purpose: When opening a PR, or pushing new changes, check for merge commits
//! and notify the user of our no-merge policy.
use crate::{
config::NoMergesConfig,
github::{GithubCommit, Repository},
};
use std::collections::BTreeSet;
use std::fmt::Write;
pub(super) fn merges_in_commits(
issue_title: &str,
repository: &Repository,
config: &NoMergesConfig,
commits: &[GithubCommit],
) -> Option<(String, Vec<String>)> {
// Don't trigger if the PR has any of the excluded title segments.
if config
.exclude_titles
.iter()
.any(|s| issue_title.contains(s))
{
return None;
}
let mut merge_commits = BTreeSet::new();
for commit in commits {
if commit.parents.len() > 1 {
merge_commits.insert(&*commit.sha);
}
}
// No merge commits.
if merge_commits.is_empty() {
return None;
}
let message = config
.message
.as_deref()
.unwrap_or(&get_default_message(
&repository.full_name,
&repository.default_branch,
merge_commits,
))
.to_string();
Some((message, config.labels.clone()))
}
fn get_default_message<'a>(
repository_name: &str,
default_branch: &str,
commits: impl IntoIterator<Item = &'a str>,
) -> String {
let mut message = String::from(
"The following commits have merge commits (commits with multiple parents) in your changes. \
We have a [no merge policy](https://rustc-dev-guide.rust-lang.org/git.html#no-merge-policy) \
so these commits will need to be removed for this pull request to be merged.
"
);
for commit in commits {
writeln!(message, "- {commit}").unwrap();
}
writeln!(
message,
"
You can start a rebase with the following commands:
```shell-session
$ # rebase
$ git pull --rebase https://github.com/{repository_name}.git {default_branch}
$ git push --force-with-lease
```"
)
.unwrap();
message
}
#[test]
fn end_to_end() {
use super::dummy_commit_from_body;
use crate::github::Parent;
let config = NoMergesConfig {
exclude_titles: vec!["Subtree update".to_string()],
labels: vec!["merge-commits".to_string()],
message: None,
};
let title = "This a PR!";
let repository = Repository {
full_name: "rust-lang/rust".to_string(),
default_branch: "master".to_string(),
fork: false,
parent: None,
};
let commit_with_merge = || {
let mut commit_with_merge =
dummy_commit_from_body("9cc6dce67c917fe5937e984f58f5003ccbb5c37e", "+ main.rs");
commit_with_merge.parents = vec![
Parent {
sha: "4fb1228e72cf76549e275c38c226c1c2326ca991".to_string(),
},
Parent {
sha: "febd545030008f13541064895ae36e19d929a043".to_string(),
},
];
commit_with_merge
};
// contains merges
{
let Some((warning, labels)) = merges_in_commits(
&title,
&repository,
&config,
&vec![
commit_with_merge(),
dummy_commit_from_body("499bdd2d766f98420c66a80a02b7d3ceba4d06ba", "+ nothing.rs"),
],
) else {
unreachable!()
};
assert_eq!(warning, "The following commits have merge commits (commits with multiple parents) in your changes. We have a [no merge policy](https://rustc-dev-guide.rust-lang.org/git.html#no-merge-policy) so these commits will need to be removed for this pull request to be merged.
- 9cc6dce67c917fe5937e984f58f5003ccbb5c37e
You can start a rebase with the following commands:
```shell-session
$ # rebase
$ git pull --rebase https://github.com/rust-lang/rust.git master
$ git push --force-with-lease
```
");
assert_eq!(labels, vec!["merge-commits"]);
}
// doesn't contains merges
{
let commit = dummy_commit_from_body("67c917fe5937e984f58f5003ccbb5c37e", "+ main.rs");
assert_eq!(
merges_in_commits(&title, &repository, &config, &[commit]),
None
);
}
// contains merges, but excluded by title
{
assert_eq!(
merges_in_commits(
"Subtree update of rustc_custom_codegen",
&repository,
&config,
&[commit_with_merge()]
),
None
);
}
}