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
use crate::{BranchHeads, GitCommitMeta, GitRepo};
use std::collections::HashMap;
use std::path::Path;
use color_eyre::eyre::Result;
use git2::{Branch, BranchType, Commit, Repository};
use log::debug;
use mktemp::Temp;
impl GitRepo {
pub fn get_remote_name(&self, r: &git2::Repository) -> Result<String> {
let remote_name = r
.branch_upstream_remote(
r.head()
.and_then(|h| h.resolve())?
.name()
.expect("branch name is valid utf8"),
)
.map(|b| b.as_str().expect("valid utf8").to_string())
.unwrap_or_else(|_| "origin".into());
Ok(remote_name)
}
pub fn get_remote_branch_head_refs(
&self,
branch_filter: Option<Vec<String>>,
) -> Result<BranchHeads> {
let temp_dir = Temp::new_dir().unwrap();
let repo = if let Some(p) = self.path.clone() {
GitRepo::to_repository_from_path(p.clone())?
} else {
self.git_clone_shallow(temp_dir.as_path())?
.to_repository()?
};
let cb = self.build_git2_remotecallback();
let remote = self
.get_remote_name(&repo)
.expect("Could not read remote name from git2::Repository");
let mut remote = repo
.find_remote(&remote)
.or_else(|_| repo.remote_anonymous(&remote))
.unwrap();
let connection = remote
.connect_auth(git2::Direction::Fetch, Some(cb), None)
.expect("Unable to connect to git repo");
let git_branch_ref_prefix = "refs/heads/";
let mut ref_map: HashMap<String, GitCommitMeta> = HashMap::new();
for git_ref in connection
.list()?
.iter()
.filter(|head| head.name().starts_with(git_branch_ref_prefix))
{
let branch_name = git_ref
.name()
.to_string()
.rsplit(git_branch_ref_prefix)
.collect::<Vec<&str>>()[0]
.to_string();
if let Some(ref branches) = branch_filter {
if branches.contains(&branch_name.to_string()) {
continue;
}
}
let commit = repo.find_commit(git_ref.oid())?;
let head_commit = GitCommitMeta::new(commit.id().as_bytes())
.with_timestamp(commit.time().seconds())
.with_message(commit.message().map_or(None, |m| Some(m.to_string())));
ref_map.insert(branch_name, head_commit);
}
Ok(ref_map)
}
pub fn is_commit_in_branch<'repo>(
r: &'repo Repository,
commit: &Commit,
branch: &Branch,
) -> bool {
let branch_head = branch.get().peel_to_commit();
if branch_head.is_err() {
return false;
}
let branch_head = branch_head.expect("Unable to extract branch HEAD commit");
if branch_head.id() == commit.id() {
return true;
}
let check_commit_in_branch = r.graph_descendant_of(branch_head.id(), commit.id());
if check_commit_in_branch.is_err() {
return false;
}
check_commit_in_branch.expect("Unable to determine if commit exists within branch")
}
pub fn get_git2_branch<'repo>(
r: &'repo Repository,
local_branch: &Option<String>,
) -> Result<Branch<'repo>> {
match local_branch {
Some(branch) => {
let b = r.find_branch(&branch, BranchType::Local)?;
debug!("Returning given branch: {:?}", &b.name());
Ok(b)
}
None => {
let head = r.head();
let local_branch = Branch::wrap(head?);
debug!("Returning HEAD branch: {:?}", local_branch.name()?);
match r.find_branch(
local_branch
.name()?
.expect("Unable to return local branch name"),
BranchType::Local,
) {
Ok(b) => Ok(b),
Err(e) => Err(e.into()),
}
}
}
}
pub fn remote_url_from_repository<'repo>(r: &'repo Repository) -> Result<String> {
let remote_name = GitRepo::remote_name_from_repository(&r)?;
let remote_url: String = r
.find_remote(&remote_name)?
.url()
.expect("Unable to extract repo url from remote")
.chars()
.collect();
Ok(remote_url)
}
fn remote_name_from_repository<'repo>(r: &'repo Repository) -> Result<String> {
let remote_name = r
.branch_upstream_remote(
r.head()
.and_then(|h| h.resolve())?
.name()
.expect("branch name is valid utf8"),
)
.map(|b| b.as_str().expect("valid utf8").to_string())
.unwrap_or_else(|_| "origin".into());
debug!("Remote name: {:?}", &remote_name);
Ok(remote_name)
}
pub fn git_remote_from_path(path: &Path) -> Result<String> {
let r = GitRepo::to_repository_from_path(path)?;
GitRepo::remote_url_from_repository(&r)
}
pub fn git_remote_from_repo(local_repo: &Repository) -> Result<String> {
GitRepo::remote_url_from_repository(&local_repo)
}
}