Struct git_status_vars::Reference
source · Expand description
A reference in a git repository.
Fields§
§name: String
§kind: String
§error: String
Implementations§
source§impl Reference
impl Reference
sourcepub fn new<N, K>(name: N, kind: K) -> Selfwhere
N: AsRef<str>,
K: AsRef<str>,
pub fn new<N, K>(name: N, kind: K) -> Selfwhere
N: AsRef<str>,
K: AsRef<str>,
Examples found in repository?
src/lib.rs (line 47)
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
pub fn symbolic(name: &str) -> Self {
Reference::new(name, "symbolic")
}
pub fn direct(name: &str) -> Self {
Reference::new(name, "direct")
}
pub fn short(&self) -> &str {
self.name
.strip_prefix("refs/heads/")
.or_else(|| self.name.strip_prefix("refs/tags/"))
.unwrap_or(&self.name)
}
}
impl ShellVars for Reference {
// Output the reference information with a prefix (e.g. "ref_").
fn write_to_shell<W: io::Write>(&self, out: &ShellWriter<W>) {
out.write_var("name", &self.name);
out.write_var("short", self.short());
out.write_var("kind", &self.kind);
out.write_var("error", &self.error);
}
}
/// The trail of a `HEAD` reference.
#[derive(Debug, Default)]
pub struct Head {
pub trail: Vec<Reference>,
pub hash: String,
pub ahead_of_upstream: Option<usize>,
pub behind_upstream: Option<usize>,
pub upstream_error: String,
}
impl ShellVars for Head {
fn write_to_shell<W: io::Write>(&self, out: &ShellWriter<W>) {
out.write_var("ref_length", self.trail.len() - 1);
for (i, reference) in self.trail[1..].iter().enumerate() {
out.group_n("ref", i + 1).write_vars(reference);
}
out.write_var("hash", &self.hash);
out.write_var("ahead", display_option(self.ahead_of_upstream));
out.write_var("behind", display_option(self.behind_upstream));
out.write_var("upstream_error", &self.upstream_error);
}
}
/// Summarize information about a repository.
///
/// This takes the `Result` from one of the `Repository::open()` functions.
///
/// ### Example
///
/// ```no_run
/// use git_status_vars::{summarize_repository, ShellWriter};
/// use git2::Repository;
///
/// summarize_repository(&ShellWriter::default(), Repository::open_from_env());
/// ```
pub fn summarize_repository<W: std::io::Write>(
out: &ShellWriter<W>,
opened: Result<Repository, git2::Error>,
) {
let result = match opened {
Ok(repository) => summarize_opened_repository(out, repository),
Err(error)
if error.code() == ErrorCode::NotFound
&& error.class() == ErrorClass::Repository =>
{
out.write_var("repo_state", "NotFound");
Ok(())
}
Err(error) => Err(error),
};
if let Err(error) = result {
out.write_var("repo_state", "Error");
out.write_var_debug("repo_error", error);
}
}
/// Summarize information about a successfully opened repository.
///
/// ### Example
///
/// ```no_run
/// use git_status_vars::{summarize_opened_repository, ShellWriter};
/// use git2::Repository;
///
/// summarize_opened_repository(
/// &ShellWriter::default(),
/// Repository::open_from_env().unwrap(),
/// ).unwrap();
/// ```
pub fn summarize_opened_repository<W: std::io::Write>(
out: &ShellWriter<W>,
repository: Repository,
) -> Result<(), git2::Error> {
out.write_var_debug("repo_state", repository.state());
out.write_var(
"repo_workdir",
display_option(repository.workdir().map(|path| path.display())),
);
out.write_var("repo_empty", repository.is_empty()?);
out.write_var("repo_bare", repository.is_bare());
out.group("head").write_vars(&head_info(&repository)?);
out.write_vars(&count_changes(&repository)?);
Ok(())
}
/// Trace the `HEAD` reference for a repository.
pub fn head_info(repository: &Repository) -> Result<Head, git2::Error> {
let mut current = "HEAD".to_string();
let mut head = Head::default();
loop {
match repository.find_reference(¤t) {
Ok(reference) => match reference.kind() {
Some(ReferenceType::Direct) => {
head.trail.push(Reference::direct(&display_option(
reference.name(),
)));
head.hash = display_option(reference.target());
break;
}
Some(ReferenceType::Symbolic) => {
head.trail.push(Reference::symbolic(&display_option(
reference.name(),
)));
let target = reference
.symbolic_target()
.expect("Symbolic ref should have symbolic target");
current = target.to_string();
}
None => {
head.trail.push(Reference::new(
display_option(reference.name()),
"unknown",
));
break;
}
},
Err(error) => {
head.trail
.push(Reference::new_with_error(current, "", error));
break;
}
};
}
match get_upstream_difference(repository) {
Ok(Some((ahead, behind))) => {
head.ahead_of_upstream = Some(ahead);
head.behind_upstream = Some(behind);
}
Ok(None) => {}
Err(error) => {
head.upstream_error = format!("{:?}", error);
}
}
Ok(head)
}
sourcepub fn new_with_error<N, K, E>(name: N, kind: K, error: E) -> Selfwhere
N: AsRef<str>,
K: AsRef<str>,
E: Debug,
pub fn new_with_error<N, K, E>(name: N, kind: K, error: E) -> Selfwhere
N: AsRef<str>,
K: AsRef<str>,
E: Debug,
Examples found in repository?
src/lib.rs (line 192)
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
pub fn head_info(repository: &Repository) -> Result<Head, git2::Error> {
let mut current = "HEAD".to_string();
let mut head = Head::default();
loop {
match repository.find_reference(¤t) {
Ok(reference) => match reference.kind() {
Some(ReferenceType::Direct) => {
head.trail.push(Reference::direct(&display_option(
reference.name(),
)));
head.hash = display_option(reference.target());
break;
}
Some(ReferenceType::Symbolic) => {
head.trail.push(Reference::symbolic(&display_option(
reference.name(),
)));
let target = reference
.symbolic_target()
.expect("Symbolic ref should have symbolic target");
current = target.to_string();
}
None => {
head.trail.push(Reference::new(
display_option(reference.name()),
"unknown",
));
break;
}
},
Err(error) => {
head.trail
.push(Reference::new_with_error(current, "", error));
break;
}
};
}
match get_upstream_difference(repository) {
Ok(Some((ahead, behind))) => {
head.ahead_of_upstream = Some(ahead);
head.behind_upstream = Some(behind);
}
Ok(None) => {}
Err(error) => {
head.upstream_error = format!("{:?}", error);
}
}
Ok(head)
}
sourcepub fn symbolic(name: &str) -> Self
pub fn symbolic(name: &str) -> Self
Examples found in repository?
src/lib.rs (lines 174-176)
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
pub fn head_info(repository: &Repository) -> Result<Head, git2::Error> {
let mut current = "HEAD".to_string();
let mut head = Head::default();
loop {
match repository.find_reference(¤t) {
Ok(reference) => match reference.kind() {
Some(ReferenceType::Direct) => {
head.trail.push(Reference::direct(&display_option(
reference.name(),
)));
head.hash = display_option(reference.target());
break;
}
Some(ReferenceType::Symbolic) => {
head.trail.push(Reference::symbolic(&display_option(
reference.name(),
)));
let target = reference
.symbolic_target()
.expect("Symbolic ref should have symbolic target");
current = target.to_string();
}
None => {
head.trail.push(Reference::new(
display_option(reference.name()),
"unknown",
));
break;
}
},
Err(error) => {
head.trail
.push(Reference::new_with_error(current, "", error));
break;
}
};
}
match get_upstream_difference(repository) {
Ok(Some((ahead, behind))) => {
head.ahead_of_upstream = Some(ahead);
head.behind_upstream = Some(behind);
}
Ok(None) => {}
Err(error) => {
head.upstream_error = format!("{:?}", error);
}
}
Ok(head)
}
sourcepub fn direct(name: &str) -> Self
pub fn direct(name: &str) -> Self
Examples found in repository?
src/lib.rs (lines 167-169)
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
pub fn head_info(repository: &Repository) -> Result<Head, git2::Error> {
let mut current = "HEAD".to_string();
let mut head = Head::default();
loop {
match repository.find_reference(¤t) {
Ok(reference) => match reference.kind() {
Some(ReferenceType::Direct) => {
head.trail.push(Reference::direct(&display_option(
reference.name(),
)));
head.hash = display_option(reference.target());
break;
}
Some(ReferenceType::Symbolic) => {
head.trail.push(Reference::symbolic(&display_option(
reference.name(),
)));
let target = reference
.symbolic_target()
.expect("Symbolic ref should have symbolic target");
current = target.to_string();
}
None => {
head.trail.push(Reference::new(
display_option(reference.name()),
"unknown",
));
break;
}
},
Err(error) => {
head.trail
.push(Reference::new_with_error(current, "", error));
break;
}
};
}
match get_upstream_difference(repository) {
Ok(Some((ahead, behind))) => {
head.ahead_of_upstream = Some(ahead);
head.behind_upstream = Some(behind);
}
Ok(None) => {}
Err(error) => {
head.upstream_error = format!("{:?}", error);
}
}
Ok(head)
}