Skip to content

Commit c262745

Browse files
committed
Add git-sha to rule variables, add test for the variable.
1 parent 9460d22 commit c262745

6 files changed

Lines changed: 73 additions & 1 deletion

File tree

src/dune_lang/pform.ml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ module Var = struct
138138
| Pkg of Pkg.t
139139
| Oxcaml_supported
140140
| Dune_warnings
141+
| Git_sha
141142

142143
let compare : t -> t -> Ordering.t = Poly.compare
143144

@@ -193,7 +194,8 @@ module Var = struct
193194
| Os os -> Os.to_dyn os
194195
| Pkg pkg -> Pkg.to_dyn pkg
195196
| Oxcaml_supported -> variant "Oxcaml_supported" []
196-
| Dune_warnings -> variant "Dune_warnings" [])
197+
| Dune_warnings -> variant "Dune_warnings" []
198+
| Git_sha -> variant "Git_sha" [])
197199
;;
198200

199201
let of_opam_global_variable_name name =
@@ -541,6 +543,7 @@ let encode_to_latest_dune_lang_version t =
541543
| Pkg pkg -> Some (Var.Pkg.encode_to_latest_dune_lang_version pkg)
542544
| Oxcaml_supported -> Some "oxcaml_supported"
543545
| Dune_warnings -> Some "dune-warnings"
546+
| Git_sha -> Some "git-sha"
544547
with
545548
| None -> Pform_was_deleted
546549
| Some name -> Success { name; payload = None })
@@ -762,6 +765,7 @@ module Env = struct
762765
; ( "oxcaml_supported"
763766
, since ~what:Oxcaml.syntax ~version:(0, 1) Var.Oxcaml_supported )
764767
; "dune-warnings", since ~version:(3, 21) Var.Dune_warnings
768+
; "git-sha", since ~version:(3, 24) Var.Git_sha
765769
]
766770
in
767771
String.Map.of_list_exn

src/dune_lang/pform.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ module Var : sig
9292
| Pkg of Pkg.t
9393
| Oxcaml_supported
9494
| Dune_warnings
95+
| Git_sha
9596

9697
val compare : t -> t -> Ordering.t
9798
val to_dyn : t -> Dyn.t

src/dune_rules/expander.ml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,16 @@ let expand_pform_var (context : Context.t) ~dir ~source (var : Pform.Var.t) =
599599
let+ scope = scope in
600600
let dune_version = Dune_project.dune_version (Scope.project scope) in
601601
Value.L.strings (Ocaml_flags.dune_warnings ~dune_version ~profile:Dev)))
602+
| Git_sha ->
603+
(let open Memo.O in
604+
let+ sha =
605+
Source_tree.nearest_vcs Path.Source.root
606+
>>= function
607+
| None -> Memo.return None
608+
| Some vcs -> Vcs.git_sha_short vcs
609+
in
610+
string (Option.value sha ~default:""))
611+
|> static
602612
;;
603613

604614
let ocaml_config_macro source macro_invocation context =

src/dune_vcs/vcs.ml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,17 @@ let commit_id =
169169
Some res)
170170
;;
171171

172+
let git_sha_short =
173+
Staged.unstage
174+
@@ make_fun
175+
"vcs-git-sha-short"
176+
~git:(fun t ->
177+
match run_git t [ "rev-parse"; "--short"; "HEAD" ] with
178+
| exception _ -> Fiber.return None
179+
| fiber -> fiber)
180+
~hg:(fun _ -> Fiber.return None)
181+
;;
182+
172183
let files =
173184
let run_zero_separated_hg t args =
174185
Process.run_capture_zero_separated

src/dune_vcs/vcs.mli

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ val describe : t -> string option Memo.t
2929
(** String uniquely identifying the current head commit *)
3030
val commit_id : t -> string option Memo.t
3131

32+
(** Short git SHA of the current head commit, or [None] if unavailable *)
33+
val git_sha_short : t -> string option Memo.t
34+
3235
(** List of files committed in the repo *)
3336
val files : t -> Path.Source.t list Memo.t
3437

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
Test the %{git-sha} variable.
2+
3+
Without a git repository, %{git-sha} expands to the empty string.
4+
5+
$ make_dune_project 3.24
6+
$ cat > dune << EOF
7+
> (rule
8+
> (target sha.txt)
9+
> (action (with-stdout-to sha.txt (echo "%{git-sha}"))))
10+
> EOF
11+
$ dune build sha.txt
12+
$ cat _build/default/sha.txt
13+
14+
In a git repository with a commit, %{git-sha} expands to the short git SHA.
15+
16+
$ git init --quiet
17+
$ git config user.email "test@test.com"
18+
$ git config user.name "Test"
19+
$ git add .
20+
$ git commit -q -m "initial"
21+
$ rm -f _build/default/sha.txt
22+
$ dune build sha.txt
23+
$ [ "$(cat _build/default/sha.txt)" = "$(git rev-parse --short HEAD)" ] && echo "sha matches"
24+
sha matches
25+
26+
In a git repository with no commits, %{git-sha} expands to the empty string.
27+
28+
$ rm -rf .git
29+
$ git init --quiet
30+
$ rm -f _build/default/sha.txt
31+
$ dune build sha.txt
32+
$ cat _build/default/sha.txt
33+
34+
Version check: %{git-sha} requires dune >= 3.24.
35+
36+
$ make_dune_project 3.23
37+
$ dune build sha.txt
38+
File "dune", line 3, characters 40-50:
39+
3 | (action (with-stdout-to sha.txt (echo "%{git-sha}"))))
40+
^^^^^^^^^^
41+
Error: %{git-sha} is only available since version 3.24 of the dune language.
42+
Please update your dune-project file to have (lang dune 3.24).
43+
[1]

0 commit comments

Comments
 (0)