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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
use crate::api::search::PullRequestStatus;
use crate::graph::FlatDep;
use dialoguer::Input;
use git2::build::CheckoutBuilder;
use git2::{
CherrypickOptions,
Repository, Sort,
};
use std::error::Error;
use tokio::process::Command;
fn remote_ref(remote: &str, git_ref: &str) -> String {
format!("{}/{}", remote, git_ref)
}
fn loop_until_confirm(prompt: &str) {
let prompt = format!("{} Type 'yes' to continue", prompt);
loop {
let result = Input::<String>::new()
.with_prompt(&prompt)
.interact()
.unwrap();
match &result[..] {
"yes" => return,
_ => continue,
}
}
}
pub fn generate_rebase_script(deps: FlatDep) -> String {
let deps = deps
.iter()
.filter(|(dep, _)| *dep.state() == PullRequestStatus::Open)
.collect::<Vec<_>>();
let mut out = String::new();
out.push_str("#!/usr/bin/env bash\n\n");
out.push_str("set -euo pipefail\n");
out.push_str("set -o xtrace\n\n");
out.push_str("# ------ THIS SCRIPT ASSUMES YOUR PR STACK IS A SINGLE CHAIN WITHOUT BRANCHING ----- #\n\n");
out.push_str("# It starts at the base of the stack, cherry-picking onto the new base and force-pushing as it goes.\n");
out.push_str("# We can't tell where the initial cherry-pick should stop (mainly because of our squash merge workflow),\n");
out.push_str(
"# so that initial stopping point for the first PR needs to be specified manually.\n\n",
);
out.push_str("export PREBASE=\"<enter a marker to stop the initial cherry-pick at>\"\n");
for (from, to) in deps {
let to = if let Some(pr) = to {
pr.head().to_string()
} else {
String::from("<enter a ref to rebase the stack on; usually `develop`>")
};
out.push_str("\n# -------------- #\n\n");
out.push_str(&format!("export TO=\"{}\"\n", remote_ref("heap", &to)));
out.push_str(&format!(
"export FROM=\"{}\"\n\n",
remote_ref("heap", from.head())
));
out.push_str("git checkout \"$TO\"\n");
out.push_str("git cherry-pick \"$PREBASE\"..\"$FROM\"\n");
out.push_str("export PREBASE=\"$(git rev-parse --verify $FROM)\"\n");
out.push_str("git push -f heap HEAD:refs/heads/\"$FROM\"\n");
}
out
}
pub async fn perform_rebase(
deps: FlatDep,
repo: &Repository,
remote: &str,
) -> Result<(), Box<dyn Error>> {
let deps = deps
.iter()
.filter(|(dep, _)| *dep.state() == PullRequestStatus::Open)
.collect::<Vec<_>>();
let (pr, _) = deps[0];
let base = remote_ref(remote, pr.base());
let base = repo.revparse_single(&base).unwrap();
let base = base.as_commit().unwrap();
let head = pr.head();
let head = repo.revparse_single(&head).unwrap();
let head = head.as_commit().unwrap();
let mut stop_cherry_pick_at = repo.merge_base(base.id(), head.id()).unwrap();
println!("Checking out {:?}", base);
repo.checkout_tree(&base.as_object(), None).unwrap();
repo.set_head_detached(base.id()).unwrap();
let mut push_refspecs = vec![];
for (pr, _) in deps {
println!("Working on PR: {:?}", pr.head());
let from = repo.revparse_single(&pr.head()).unwrap();
let from = from.as_commit().unwrap();
let mut walk = repo.revwalk().unwrap();
walk.set_sorting(Sort::TOPOLOGICAL).unwrap();
walk.set_sorting(Sort::REVERSE).unwrap();
walk.push(from.id()).unwrap();
walk.hide(stop_cherry_pick_at).unwrap();
for from in walk {
let from = repo.find_commit(from.unwrap()).unwrap();
let to = repo
.find_commit(repo.head().unwrap().target().unwrap())
.unwrap();
if from.parent_count() > 1 {
panic!("Exiting: I don't know how to deal with merge commits correctly.");
}
let mut cb = CheckoutBuilder::new();
cb.allow_conflicts(true);
let mut opts = CherrypickOptions::new();
opts.checkout_builder(cb);
println!("Cherry-picking: {:?}", from);
repo.cherrypick(&from, Some(&mut opts)).unwrap();
let mut index = repo.index().unwrap();
if index.has_conflicts() {
let prompt = "Conflicts! Resolve manually and `git add` each one (don't run any `git cherry-pick` commands, though).";
loop_until_confirm(prompt);
index = repo.index().unwrap();
index.read(true).unwrap();
}
let tree = index.write_tree_to(&repo).unwrap();
let tree = repo.find_tree(tree).unwrap();
let signature = repo.signature().unwrap();
let commit = repo
.commit(
None,
&signature,
&signature,
&from.message().unwrap(),
&tree,
&[&to],
)
.unwrap();
let commit = repo.find_commit(commit).unwrap();
let mut cb = CheckoutBuilder::new();
cb.force();
repo.checkout_tree(&commit.as_object(), Some(&mut cb))
.unwrap();
repo.set_head_detached(commit.id()).unwrap();
repo.cleanup_state().unwrap();
}
let head = repo.head().unwrap().target().unwrap();
let head = repo.find_commit(head).unwrap();
repo.branch(pr.head(), &head, true).unwrap();
let from = repo
.revparse_single(&remote_ref(remote, pr.head()))
.unwrap();
let from = from.as_commit().unwrap();
stop_cherry_pick_at = from.id();
push_refspecs.push(format!("refs/heads/{}:refs/heads/{}", pr.head(), pr.head()));
}
let repo_dir = repo.workdir().unwrap().to_str().unwrap();
let mut command = Command::new("git");
command.arg("push").arg("-f").arg(remote);
command.args(push_refspecs.as_slice());
command.current_dir(repo_dir);
println!("\n{:?}", push_refspecs);
loop_until_confirm("Going to push these refspecs ☝️ ");
command.spawn()?.await?;
Ok(())
}