Skip to content

Commit 7730861

Browse files
Allow users to incorporate Git tags into dynamic cache keys (#8259)
## Summary You can now use `cache-keys = [{ git = { commit = true, tags = true } }]` to include both the current commit and set of tags in the cache key. Closes #7866. Closes #7997.
1 parent 6a81d30 commit 7730861

File tree

8 files changed

+290
-116
lines changed

8 files changed

+290
-116
lines changed

crates/uv-cache-info/src/cache_info.rs

Lines changed: 69 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::commit_info::CacheCommit;
1+
use crate::git_info::{Commit, Tags};
22
use crate::timestamp::Timestamp;
33

44
use serde::Deserialize;
@@ -26,7 +26,9 @@ pub struct CacheInfo {
2626
/// files to timestamp via the `cache-keys` field.
2727
timestamp: Option<Timestamp>,
2828
/// The commit at which the distribution was built.
29-
commit: Option<CacheCommit>,
29+
commit: Option<Commit>,
30+
/// The Git tags present at the time of the build.
31+
tags: Option<Tags>,
3032
}
3133

3234
impl CacheInfo {
@@ -51,6 +53,7 @@ impl CacheInfo {
5153
/// Compute the cache info for a given directory.
5254
pub fn from_directory(directory: &Path) -> Result<Self, CacheInfoError> {
5355
let mut commit = None;
56+
let mut tags = None;
5457
let mut timestamp = None;
5558

5659
// Read the cache keys.
@@ -109,13 +112,37 @@ impl CacheInfo {
109112
}
110113
timestamp = max(timestamp, Some(Timestamp::from_metadata(&metadata)));
111114
}
112-
CacheKey::Git { git: true } => match CacheCommit::from_repository(directory) {
115+
CacheKey::Git {
116+
git: GitPattern::Bool(true),
117+
} => match Commit::from_repository(directory) {
113118
Ok(commit_info) => commit = Some(commit_info),
114119
Err(err) => {
115120
debug!("Failed to read the current commit: {err}");
116121
}
117122
},
118-
CacheKey::Git { git: false } => {}
123+
CacheKey::Git {
124+
git: GitPattern::Set(set),
125+
} => {
126+
if set.commit.unwrap_or(false) {
127+
match Commit::from_repository(directory) {
128+
Ok(commit_info) => commit = Some(commit_info),
129+
Err(err) => {
130+
debug!("Failed to read the current commit: {err}");
131+
}
132+
}
133+
}
134+
if set.tags.unwrap_or(false) {
135+
match Tags::from_repository(directory) {
136+
Ok(tags_info) => tags = Some(tags_info),
137+
Err(err) => {
138+
debug!("Failed to read the current tags: {err}");
139+
}
140+
}
141+
}
142+
}
143+
CacheKey::Git {
144+
git: GitPattern::Bool(false),
145+
} => {}
119146
}
120147
}
121148

@@ -150,7 +177,11 @@ impl CacheInfo {
150177
}
151178
}
152179

153-
Ok(Self { timestamp, commit })
180+
Ok(Self {
181+
timestamp,
182+
commit,
183+
tags,
184+
})
154185
}
155186

156187
/// Compute the cache info for a given file, assumed to be a binary or source distribution
@@ -165,14 +196,18 @@ impl CacheInfo {
165196
}
166197

167198
pub fn is_empty(&self) -> bool {
168-
self.timestamp.is_none() && self.commit.is_none()
199+
self.timestamp.is_none() && self.commit.is_none() && self.tags.is_none()
169200
}
170201
}
171202

172203
#[derive(Debug, serde::Deserialize)]
173204
struct TimestampCommit {
205+
#[serde(default)]
174206
timestamp: Option<Timestamp>,
175-
commit: Option<CacheCommit>,
207+
#[serde(default)]
208+
commit: Option<Commit>,
209+
#[serde(default)]
210+
tags: Option<Tags>,
176211
}
177212

178213
#[derive(Debug, serde::Deserialize)]
@@ -192,9 +227,15 @@ impl From<CacheInfoWire> for CacheInfo {
192227
timestamp: Some(timestamp),
193228
..Self::default()
194229
},
195-
CacheInfoWire::TimestampCommit(TimestampCommit { timestamp, commit }) => {
196-
Self { timestamp, commit }
197-
}
230+
CacheInfoWire::TimestampCommit(TimestampCommit {
231+
timestamp,
232+
commit,
233+
tags,
234+
}) => Self {
235+
timestamp,
236+
commit,
237+
tags,
238+
},
198239
}
199240
}
200241
}
@@ -226,8 +267,24 @@ pub enum CacheKey {
226267
Path(String),
227268
/// Ex) `{ file = "Cargo.lock" }` or `{ file = "**/*.toml" }`
228269
File { file: String },
229-
/// Ex) `{ git = true }`
230-
Git { git: bool },
270+
/// Ex) `{ git = true }` or `{ git = { commit = true, tags = false } }`
271+
Git { git: GitPattern },
272+
}
273+
274+
#[derive(Debug, Clone, serde::Deserialize)]
275+
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
276+
#[serde(untagged, rename_all = "kebab-case", deny_unknown_fields)]
277+
pub enum GitPattern {
278+
Bool(bool),
279+
Set(GitSet),
280+
}
281+
282+
#[derive(Debug, Clone, serde::Deserialize)]
283+
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
284+
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
285+
pub struct GitSet {
286+
commit: Option<bool>,
287+
tags: Option<bool>,
231288
}
232289

233290
pub enum FilePattern {

crates/uv-cache-info/src/commit_info.rs

Lines changed: 0 additions & 92 deletions
This file was deleted.

0 commit comments

Comments
 (0)