Add repository name and current tag to gstat (#692)

* Add repository name to gstat

* Fix getting repo name; Add tag as well
This commit is contained in:
Jakub Žádník 2022-01-07 13:44:05 +02:00 committed by GitHub
parent f016a5cb72
commit f964ce9bc0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,4 +1,4 @@
use git2::{Branch, BranchType, Repository};
use git2::{Branch, BranchType, DescribeOptions, Repository};
use nu_plugin::LabeledError;
use nu_protocol::{Span, Spanned, Value};
use std::fmt::Write;
@ -126,14 +126,26 @@ impl GStat {
}
};
let stats = Repository::discover(repo_path).map(|mut repo| (Stats::new(&mut repo)));
let stats = match stats {
Ok(s) => s,
Err(_) => {
// Since we really never want this to fail, lets return an empty record so
// that one can check it in a script and do something with it.
let (stats, repo) = if let Ok(mut repo) = Repository::discover(repo_path) {
(Stats::new(&mut repo), repo)
} else {
return Ok(self.create_empty_git_status(span));
}
};
let repo_name = repo
.path()
.parent()
.and_then(|p| p.file_name())
.map(|p| p.to_string_lossy().to_string())
.unwrap_or_else(|| "".to_string());
let mut desc_opts = DescribeOptions::new();
desc_opts.describe_tags();
let tag = if let Ok(Ok(s)) = repo.describe(&desc_opts).map(|d| d.format(None)) {
s
} else {
"no_tag".to_string()
};
let mut cols = vec![];
@ -214,6 +226,16 @@ impl GStat {
val: stats.stashes as i64,
span: *span,
});
cols.push("repo_name".into());
vals.push(Value::String {
val: repo_name,
span: *span,
});
cols.push("tag".into());
vals.push(Value::String {
val: tag,
span: *span,
});
cols.push("branch".into());
vals.push(Value::String {
val: stats.branch,
@ -321,6 +343,16 @@ impl GStat {
val: -1,
span: *span,
});
cols.push("repo_name".into());
vals.push(Value::String {
val: "no_repository".to_string(),
span: *span,
});
cols.push("tag".into());
vals.push(Value::String {
val: "no_tag".to_string(),
span: *span,
});
cols.push("branch".into());
vals.push(Value::String {
val: "no_branch".to_string(),