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
use crate::build::{ConstType, ConstVal, ShadowConst};
use crate::ci::CIType;
use crate::err::*;
use chrono::{DateTime, Local, NaiveDateTime, Utc};
use git2::Error as git2Error;
use git2::{Reference, Repository};
use std::collections::HashMap;
use std::path::Path;
use std::process::Command;
pub const BRANCH: ShadowConst = "BRANCH";
pub(crate) const TAG: ShadowConst = "TAG";
const SHORT_COMMIT: ShadowConst = "SHORT_COMMIT";
const COMMIT_HASH: ShadowConst = "COMMIT_HASH";
const COMMIT_DATE: ShadowConst = "COMMIT_DATE";
const COMMIT_AUTHOR: ShadowConst = "COMMIT_AUTHOR";
const COMMIT_EMAIL: ShadowConst = "COMMIT_EMAIL";
#[derive(Default, Debug)]
pub struct Git {
map: HashMap<ShadowConst, ConstVal>,
ci_type: CIType,
}
impl Git {
fn update_val(&mut self, c: ShadowConst, v: String) {
if let Some(val) = self.map.get_mut(c) {
*val = ConstVal {
desc: val.desc.clone(),
v,
t: ConstType::Str,
}
}
}
fn init(&mut self, path: &Path, std_env: &HashMap<String, String>) -> SdResult<()> {
let repo = git_repo(path)?;
let reference = repo.head()?;
let (branch, tag) = self.get_branch_tag(&reference, &std_env)?;
self.update_val(BRANCH, branch);
self.update_val(TAG, tag);
if let Some(v) = reference.target() {
let commit = v.to_string();
self.update_val(COMMIT_HASH, commit.clone());
let mut short_commit = commit.as_str();
if commit.len() > 8 {
short_commit = &short_commit[0..8];
}
self.update_val(SHORT_COMMIT, short_commit.to_string());
}
let commit = reference.peel_to_commit()?;
let time_stamp = commit.time().seconds().to_string().parse::<i64>()?;
let dt = NaiveDateTime::from_timestamp(time_stamp, 0);
let date_time = DateTime::<Utc>::from_utc(dt, Utc);
let date_time: DateTime<Local> = DateTime::from(date_time);
self.update_val(
COMMIT_DATE,
date_time.format("%Y-%m-%d %H:%M:%S").to_string(),
);
let author = commit.author();
if let Some(v) = author.email() {
self.update_val(COMMIT_EMAIL, v.to_string());
}
if let Some(v) = author.name() {
self.update_val(COMMIT_AUTHOR, v.to_string());
}
Ok(())
}
#[allow(clippy::manual_strip)]
fn get_branch_tag(
&self,
reference: &Reference<'_>,
std_env: &HashMap<String, String>,
) -> SdResult<(String, String)> {
let mut branch = String::new();
let mut tag = String::new();
if let Some(v) = reference
.shorthand()
.map(|x| x.trim().to_string())
.or_else(command_current_branch)
{
branch = v
}
if let Ok(out) = Command::new("git")
.args(&["tag", "-l", "--contains", "HEAD"])
.output()
{
tag = String::from_utf8(out.stdout)?.trim().to_string();
}
match self.ci_type {
CIType::Gitlab => {
let gitlab_branch = if let Some(v) = std_env.get("CI_COMMIT_REF_NAME") {
v.to_string()
} else {
branch.clone()
};
if let Some(v) = std_env.get("CI_COMMIT_TAG") {
tag = v.to_string();
} else {
branch = gitlab_branch;
}
}
CIType::Github => {
if let Some(v) = std_env.get("GITHUB_REF") {
let ref_branch_prefix: &str = "refs/heads/";
let ref_tag_prefix: &str = "refs/tags/";
if v.starts_with(ref_branch_prefix) {
branch = v[ref_branch_prefix.len()..].to_string()
} else if v.starts_with(ref_tag_prefix) {
tag = v[ref_tag_prefix.len()..].to_string()
}
}
}
_ => {}
}
Ok((branch, tag))
}
}
pub fn new_git(
path: &std::path::Path,
ci: CIType,
std_env: &HashMap<String, String>,
) -> HashMap<ShadowConst, ConstVal> {
let mut git = Git {
map: Default::default(),
ci_type: ci,
};
git.map
.insert(BRANCH, ConstVal::new("display current branch"));
git.map.insert(TAG, ConstVal::new("display current tag"));
git.map
.insert(COMMIT_HASH, ConstVal::new("display current commit_id"));
git.map.insert(
SHORT_COMMIT,
ConstVal::new("display current short commit_id"),
);
git.map.insert(
COMMIT_AUTHOR,
ConstVal::new("display current commit author"),
);
git.map
.insert(COMMIT_EMAIL, ConstVal::new("display current commit email"));
git.map
.insert(COMMIT_DATE, ConstVal::new("display current commit date"));
if let Err(e) = git.init(path, std_env) {
println!("{}", e.to_string());
}
git.map
}
pub fn branch() -> String {
git_repo(".")
.map(|x| git2_current_branch(&x))
.unwrap_or_else(|_| command_current_branch())
.unwrap_or_default()
}
fn git_repo<P: AsRef<Path>>(path: P) -> Result<Repository, git2Error> {
git2::Repository::discover(path)
}
fn git2_current_branch(repo: &Repository) -> Option<String> {
repo.head()
.map(|x| x.shorthand().map(|x| x.to_string()))
.unwrap_or(None)
}
fn command_current_branch() -> Option<String> {
Command::new("git")
.args(&["symbolic-ref", "--short", "HEAD"])
.output()
.map(|x| String::from_utf8(x.stdout).ok())
.map(|x| x.map(|x| x.trim().to_string()))
.unwrap_or(None)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::Shadow;
use std::path::Path;
#[test]
fn test_git() {
let map = Shadow::get_env();
let map = new_git(Path::new("./"), CIType::Github, &map);
println!("map:{:?}", map);
}
#[test]
fn test_current_branch() {
let git2_branch = git_repo(".")
.map(|x| git2_current_branch(&x))
.unwrap_or(None);
let command_branch = command_current_branch();
assert!(git2_branch.is_some());
assert!(command_branch.is_some());
assert_eq!(command_branch, git2_branch);
assert_eq!(Some(branch()), git2_branch);
}
}