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