bootstrap/utils/
channel.rs
1use std::fs;
9use std::path::Path;
10
11use super::helpers;
12use crate::Build;
13use crate::utils::helpers::{start_process, t};
14
15#[derive(Clone, Default)]
16pub enum GitInfo {
17 #[default]
19 Absent,
20 Present(Option<Info>),
24 RecordedForTarball(Info),
27}
28
29#[derive(Clone)]
30pub struct Info {
31 pub commit_date: String,
32 pub sha: String,
33 pub short_sha: String,
34}
35
36impl GitInfo {
37 pub fn new(omit_git_hash: bool, dir: &Path) -> GitInfo {
38 if !dir.join(".git").exists() {
40 match read_commit_info_file(dir) {
41 Some(info) => return GitInfo::RecordedForTarball(info),
42 None => return GitInfo::Absent,
43 }
44 }
45
46 match helpers::git(Some(dir)).arg("rev-parse").as_command_mut().output() {
48 Ok(ref out) if out.status.success() => {}
49 _ => return GitInfo::Absent,
50 }
51
52 if omit_git_hash {
55 return GitInfo::Present(None);
56 }
57
58 let ver_date = start_process(
60 helpers::git(Some(dir))
61 .arg("log")
62 .arg("-1")
63 .arg("--date=short")
64 .arg("--pretty=format:%cd")
65 .as_command_mut(),
66 );
67 let ver_hash =
68 start_process(helpers::git(Some(dir)).arg("rev-parse").arg("HEAD").as_command_mut());
69 let short_ver_hash = start_process(
70 helpers::git(Some(dir)).arg("rev-parse").arg("--short=9").arg("HEAD").as_command_mut(),
71 );
72 GitInfo::Present(Some(Info {
73 commit_date: ver_date().trim().to_string(),
74 sha: ver_hash().trim().to_string(),
75 short_sha: short_ver_hash().trim().to_string(),
76 }))
77 }
78
79 pub fn info(&self) -> Option<&Info> {
80 match self {
81 GitInfo::Absent => None,
82 GitInfo::Present(info) => info.as_ref(),
83 GitInfo::RecordedForTarball(info) => Some(info),
84 }
85 }
86
87 pub fn sha(&self) -> Option<&str> {
88 self.info().map(|s| &s.sha[..])
89 }
90
91 pub fn sha_short(&self) -> Option<&str> {
92 self.info().map(|s| &s.short_sha[..])
93 }
94
95 pub fn commit_date(&self) -> Option<&str> {
96 self.info().map(|s| &s.commit_date[..])
97 }
98
99 pub fn version(&self, build: &Build, num: &str) -> String {
100 let mut version = build.release(num);
101 if let Some(inner) = self.info() {
102 version.push_str(" (");
103 version.push_str(&inner.short_sha);
104 version.push(' ');
105 version.push_str(&inner.commit_date);
106 version.push(')');
107 }
108 version
109 }
110
111 pub fn is_managed_git_subrepository(&self) -> bool {
113 match self {
114 GitInfo::Absent | GitInfo::RecordedForTarball(_) => false,
115 GitInfo::Present(_) => true,
116 }
117 }
118
119 pub fn is_from_tarball(&self) -> bool {
121 match self {
122 GitInfo::Absent | GitInfo::Present(_) => false,
123 GitInfo::RecordedForTarball(_) => true,
124 }
125 }
126}
127
128pub fn read_commit_info_file(root: &Path) -> Option<Info> {
131 if let Ok(contents) = fs::read_to_string(root.join("git-commit-info")) {
132 let mut lines = contents.lines();
133 let sha = lines.next();
134 let short_sha = lines.next();
135 let commit_date = lines.next();
136 let info = match (commit_date, sha, short_sha) {
137 (Some(commit_date), Some(sha), Some(short_sha)) => Info {
138 commit_date: commit_date.to_owned(),
139 sha: sha.to_owned(),
140 short_sha: short_sha.to_owned(),
141 },
142 _ => panic!("the `git-commit-info` file is malformed"),
143 };
144 Some(info)
145 } else {
146 None
147 }
148}
149
150pub fn write_commit_info_file(root: &Path, info: &Info) {
153 let commit_info = format!("{}\n{}\n{}\n", info.sha, info.short_sha, info.commit_date);
154 t!(fs::write(root.join("git-commit-info"), commit_info));
155}
156
157pub fn write_commit_hash_file(root: &Path, sha: &str) {
159 t!(fs::write(root.join("git-commit-hash"), sha));
160}