-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit.rs
297 lines (265 loc) · 8.86 KB
/
git.rs
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
use std::fmt::Debug;
use std::path::{Path, PathBuf};
use duct::Expression;
use eyre::{eyre, Result, WrapErr};
use gix::{self};
use once_cell::sync::OnceCell;
use xx::file;
use crate::cmd;
use crate::cmd::CmdLineRunner;
use crate::config::SETTINGS;
use crate::file::touch_dir;
use crate::ui::progress_report::SingleReport;
pub struct Git {
pub dir: PathBuf,
pub repo: OnceCell<gix::Repository>,
}
macro_rules! git_cmd {
( $dir:expr $(, $arg:expr )* $(,)? ) => {
{
let safe = format!("safe.directory={}", $dir.display());
cmd!("git", "-C", $dir, "-c", safe $(, $arg)*)
}
}
}
macro_rules! git_cmd_read {
( $dir:expr $(, $arg:expr )* $(,)? ) => {
{
git_cmd!($dir $(, $arg)*).read().wrap_err_with(|| {
let args = [$($arg,)*].join(" ");
format!("git {args} failed")
})
}
}
}
impl Git {
pub fn new<P: AsRef<Path>>(dir: P) -> Self {
Self {
dir: dir.as_ref().to_path_buf(),
repo: OnceCell::new(),
}
}
pub fn repo(&self) -> Result<&gix::Repository> {
self.repo.get_or_try_init(|| {
trace!("opening git repository via gix at {:?}", self.dir);
gix::open(&self.dir)
.wrap_err_with(|| format!("failed to open git repository at {:?}", self.dir))
.inspect_err(|err| warn!("{err:#}"))
})
}
pub fn is_repo(&self) -> bool {
self.dir.join(".git").is_dir()
}
pub fn update(&self, gitref: Option<String>) -> Result<(String, String)> {
let gitref = gitref.map_or_else(|| self.current_branch(), Ok)?;
self.update_ref(gitref, false)
}
pub fn update_tag(&self, gitref: String) -> Result<(String, String)> {
self.update_ref(gitref, true)
}
fn update_ref(&self, gitref: String, is_tag_ref: bool) -> Result<(String, String)> {
debug!("updating {} to {}", self.dir.display(), gitref);
let exec = |cmd: Expression| match cmd.stderr_to_stdout().stdout_capture().unchecked().run()
{
Ok(res) => {
if res.status.success() {
Ok(())
} else {
Err(eyre!(
"git failed: {cmd:?} {}",
String::from_utf8(res.stdout).unwrap()
))
}
}
Err(err) => Err(eyre!("git failed: {cmd:?} {err:#}")),
};
debug!("updating {} to {} with git", self.dir.display(), gitref);
let refspec = if is_tag_ref {
format!("refs/tags/{}:refs/tags/{}", gitref, gitref)
} else {
format!("{}:{}", gitref, gitref)
};
exec(git_cmd!(
&self.dir,
"fetch",
"--prune",
"--update-head-ok",
"origin",
&refspec
))?;
let prev_rev = self.current_sha()?;
exec(git_cmd!(
&self.dir,
"-c",
"advice.detachedHead=false",
"-c",
"advice.objectNameWarning=false",
"checkout",
"--force",
&gitref
))?;
let post_rev = self.current_sha()?;
touch_dir(&self.dir)?;
Ok((prev_rev, post_rev))
}
pub fn clone(&self, url: &str, options: CloneOptions) -> Result<()> {
debug!("cloning {} to {}", url, self.dir.display());
if let Some(parent) = self.dir.parent() {
file::mkdirp(parent)?;
}
if SETTINGS.libgit2 || SETTINGS.gix {
let mut prepare_clone = gix::prepare_clone(url, &self.dir)?;
let (mut prepare_checkout, _) = prepare_clone
.fetch_then_checkout(gix::progress::Discard, &gix::interrupt::IS_INTERRUPTED)?;
prepare_checkout
.main_worktree(gix::progress::Discard, &gix::interrupt::IS_INTERRUPTED)?;
return Ok(());
}
match get_git_version() {
Ok(version) => trace!("git version: {}", version),
Err(err) => warn!(
"failed to get git version: {:#}\n Git is required to use mise.",
err
),
}
if let Some(pr) = &options.pr {
// in order to prevent hiding potential password prompt, just disable the progress bar
pr.abandon();
}
let mut cmd = CmdLineRunner::new("git")
.arg("clone")
.arg("-q")
.arg("--depth")
.arg("1")
.arg(url)
.arg(&self.dir);
if let Some(branch) = &options.branch {
cmd = cmd.args([
"-b",
branch,
"--single-branch",
"-c",
"advice.detachedHead=false",
]);
}
cmd.execute()?;
Ok(())
}
pub fn current_branch(&self) -> Result<String> {
let dir = &self.dir;
if let Ok(repo) = self.repo() {
let head = repo.head()?;
let branch = head
.referent_name()
.map(|name| name.shorten().to_string())
.unwrap_or_else(|| head.id().unwrap().to_string());
debug!("current branch for {dir:?}: {branch}");
return Ok(branch);
}
let branch = git_cmd_read!(&self.dir, "branch", "--show-current")?;
debug!("current branch for {}: {}", self.dir.display(), &branch);
Ok(branch)
}
pub fn current_sha(&self) -> Result<String> {
let dir = &self.dir;
if let Ok(repo) = self.repo() {
let head = repo.head()?;
let id = head.id();
let sha = id.unwrap().to_string();
debug!("current sha for {dir:?}: {sha}");
return Ok(sha);
}
let sha = git_cmd_read!(&self.dir, "rev-parse", "HEAD")?;
debug!("current sha for {}: {}", self.dir.display(), &sha);
Ok(sha)
}
pub fn current_sha_short(&self) -> Result<String> {
let dir = &self.dir;
if let Ok(repo) = self.repo() {
let head = repo.head()?;
let id = head.id();
let sha = id.unwrap().to_string()[..7].to_string();
debug!("current sha for {dir:?}: {sha}");
return Ok(sha);
}
let sha = git_cmd_read!(&self.dir, "rev-parse", "--short", "HEAD")?;
debug!("current sha for {dir:?}: {sha}");
Ok(sha)
}
pub fn current_abbrev_ref(&self) -> Result<String> {
let dir = &self.dir;
if let Ok(repo) = self.repo() {
let head = repo.head()?;
let head = head.name().shorten().to_string();
debug!("current abbrev ref for {dir:?}: {head}");
return Ok(head);
}
let aref = git_cmd_read!(&self.dir, "rev-parse", "--abbrev-ref", "HEAD")?;
debug!("current abbrev ref for {}: {}", self.dir.display(), &aref);
Ok(aref)
}
pub fn get_remote_url(&self) -> Option<String> {
let dir = &self.dir;
if !self.exists() {
return None;
}
if let Ok(repo) = self.repo() {
if let Ok(remote) = repo.find_remote("origin") {
if let Some(url) = remote.url(gix::remote::Direction::Fetch) {
trace!("remote url for {dir:?}: {url}");
return Some(url.to_string());
}
}
}
let res = git_cmd_read!(&self.dir, "config", "--get", "remote.origin.url");
match res {
Ok(url) => {
debug!("remote url for {dir:?}: {url}");
Some(url)
}
Err(err) => {
warn!("failed to get remote url for {dir:?}: {err:#}");
None
}
}
}
pub fn split_url_and_ref(url: &str) -> (String, Option<String>) {
match url.split_once('#') {
Some((url, _ref)) => (url.to_string(), Some(_ref.to_string())),
None => (url.to_string(), None),
}
}
pub fn exists(&self) -> bool {
self.dir.join(".git").is_dir()
}
pub fn get_root() -> eyre::Result<PathBuf> {
Ok(cmd!("git", "rev-parse", "--show-toplevel")
.read()?
.trim()
.into())
}
}
fn get_git_version() -> Result<String> {
let version = cmd!("git", "--version").read()?;
Ok(version.trim().into())
}
impl Debug for Git {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Git").field("dir", &self.dir).finish()
}
}
#[derive(Default)]
pub struct CloneOptions<'a> {
pr: Option<&'a Box<dyn SingleReport>>,
branch: Option<String>,
}
impl<'a> CloneOptions<'a> {
pub fn pr(mut self, pr: &'a Box<dyn SingleReport>) -> Self {
self.pr = Some(pr);
self
}
pub fn branch(mut self, branch: &str) -> Self {
self.branch = Some(branch.to_string());
self
}
}