Skip to content

Commit 9db9cbb

Browse files
committed
feat: add git origin info to cache .ltar files (#7143)
This should have no user-visible effects, but from here on `.ltar` files in the cache will contain the commit from which they were generated. Because of the way `cache` works, when you download the cache for a commit you might be using files built from several different commits, because later commits only invalidated some files and the old files are still good. If an `.ltar` file turns out to be bad (e.g. has a surprising `.trace` and is causing `lake build` to ignore it), this is helpful information for debugging, and it's only a few extra bytes in the file. The new version of leantar has a new function: `leantar -k 1234.ltar` will print out the "comments" in the file. If it is a file generated by an old version of leantar it will be empty, but files generated subsequent to this commit will show something along the lines `git=mathlib4@12de34` containing the commit sha for the run that generated this ltar file. Co-authored-by: Mario Carneiro <di.gama@gmail.com>
1 parent 50988b8 commit 9db9cbb

File tree

3 files changed

+15
-16
lines changed

3 files changed

+15
-16
lines changed

Cache/IO.lean

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def CURLBIN :=
6969

7070
/-- leantar version at https://github.com/digama0/leangz -/
7171
def LEANTARVERSION :=
72-
"0.1.5"
72+
"0.1.7"
7373

7474
def EXE := if System.Platform.isWindows then ".exe" else ""
7575

@@ -242,23 +242,24 @@ def mkDir (path : FilePath) : IO Unit := do
242242
Given a path to a Lean file, concatenates the paths to its build files.
243243
Each build file also has a `Bool` indicating whether that file is required for caching to proceed.
244244
-/
245-
def mkBuildPaths (path : FilePath) : IO $ Array (FilePath × Bool) := do
245+
def mkBuildPaths (path : FilePath) : IO $ List (FilePath × Bool) := do
246246
let packageDir ← getPackageDir path
247-
return #[
247+
return [
248248
(packageDir / LIBDIR / path.withExtension "trace", true),
249249
(packageDir / LIBDIR / path.withExtension "olean", true),
250250
(packageDir / LIBDIR / path.withExtension "ilean", true),
251251
(packageDir / IRDIR / path.withExtension "c", true),
252252
(packageDir / LIBDIR / path.withExtension "extra", false)]
253253

254254
/-- Check that all required build files exist. -/
255-
def allExist (paths : Array (FilePath × Bool)) : IO Bool := do
255+
def allExist (paths : List (FilePath × Bool)) : IO Bool := do
256256
for (path, required) in paths do
257257
if required then if !(← path.pathExists) then return false
258258
pure true
259259

260260
/-- Compresses build files into the local cache and returns an array with the compressed files -/
261-
def packCache (hashMap : HashMap) (overwrite : Bool) : IO $ Array String := do
261+
def packCache (hashMap : HashMap) (overwrite : Bool) (comment : Option String := none) :
262+
IO $ Array String := do
262263
mkDir CACHEDIR
263264
IO.println "Compressing cache"
264265
let mut acc := #[]
@@ -270,8 +271,10 @@ def packCache (hashMap : HashMap) (overwrite : Bool) : IO $ Array String := do
270271
if ← allExist buildPaths then
271272
if overwrite || !(← zipPath.pathExists) then
272273
tasks := tasks.push <| ← IO.asTask do
273-
runCmd (← getLeanTar) $ #[zipPath.toString] ++
274-
((← buildPaths.filterM (·.1.pathExists)) |>.map (·.1.toString))
274+
let trace :: args := (← buildPaths.filterM (·.1.pathExists)) |>.map (·.1.toString)
275+
| unreachable!
276+
runCmd (← getLeanTar) $ #[zipPath.toString, trace] ++
277+
(if let some c := comment then #["-c", s!"git=mathlib4@{c}"] else #[]) ++ args
275278
acc := acc.push zip
276279
for task in tasks do
277280
_ ← IO.ofExcept task.get

Cache/Main.lean

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,16 @@ def main (args : List String) : IO Unit := do
8282
getFiles (← hashMemo.filterByFilePaths (toPaths args)) true true goodCurl true
8383
| "get-" :: args =>
8484
getFiles (← hashMemo.filterByFilePaths (toPaths args)) false false goodCurl false
85-
| ["pack"] => discard $ packCache hashMap false
86-
| ["pack!"] => discard $ packCache hashMap true
85+
| ["pack"] => discard $ packCache hashMap false (← getGitCommitHash)
86+
| ["pack!"] => discard $ packCache hashMap true (← getGitCommitHash)
8787
| ["unpack"] => unpackCache hashMap false
8888
| ["unpack!"] => unpackCache hashMap true
8989
| ["clean"] =>
9090
cleanCache $ hashMap.fold (fun acc _ hash => acc.insert $ CACHEDIR / hash.asLTar) .empty
9191
| ["clean!"] => cleanCache
9292
-- We allow arguments for `put` and `put!` so they can be added to the `roots`.
93-
| "put" :: _ => putFiles (← packCache hashMap false) false (← getToken)
94-
| "put!" :: _ => putFiles (← packCache hashMap false) true (← getToken)
93+
| "put" :: _ => putFiles (← packCache hashMap false (← getGitCommitHash)) false (← getToken)
94+
| "put!" :: _ => putFiles (← packCache hashMap false (← getGitCommitHash)) true (← getToken)
9595
| ["commit"] =>
9696
if !(← isGitStatusClean) then IO.println "Please commit your changes first" return else
9797
commit hashMap false (← getToken)

Cache/Requests.lean

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -168,11 +168,7 @@ section Commit
168168
def isGitStatusClean : IO Bool :=
169169
return (← IO.runCmd "git" #["status", "--porcelain"]).isEmpty
170170

171-
def getGitCommitHash : IO String := do
172-
let ret := (← IO.runCmd "git" #["log", "-1"]).replace "\n" " "
173-
match ret.splitOn " " with
174-
| "commit" :: hash :: _ => return hash
175-
| _ => throw $ IO.userError "Invalid format for the return of `git log -1`"
171+
def getGitCommitHash : IO String := return (← IO.runCmd "git" #["rev-parse", "HEAD"]).trimRight
176172

177173
/--
178174
Sends a commit file to the server, containing the hashes of the respective committed files.

0 commit comments

Comments
 (0)