Skip to content

Commit 38dd2e2

Browse files
authored
Avoid detached HEAD in Update workflow on push events (#2517)
* Fail in Update workflow if push event has detached HEAD * Exit with error when event is unsupported * Check out github.ref instead of github.sha on push events * Factor out nix/hpack.nix from shell.nix * Factor out nix/fourmolu.nix from shell.nix * Use unqualified reference for checkout, when applicable
1 parent e4457b2 commit 38dd2e2

File tree

9 files changed

+101
-28
lines changed

9 files changed

+101
-28
lines changed

.github/workflows/update.yml

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ jobs:
1111
steps:
1212
- id: config
1313
run: |
14-
if [ "${{ github.event_name }}" == 'pull_request' ]
15-
then
16-
echo "::set-output name=ref::${{ github.event.pull_request.head.sha }}"
17-
else
18-
echo "::set-output name=ref::$GITHUB_SHA"
14+
ref=${{ github.ref }}
15+
if [ "${{ github.event_name }}" == 'pull_request' ]; then
16+
ref="${{ github.event.pull_request.head.sha }}"
1917
fi
18+
ref="${ref#refs/heads/}"
19+
echo "::set-output name=ref::$ref"
2020
2121
git config --global user.name github-actions
2222
git config --global user.email [email protected]
@@ -47,6 +47,13 @@ jobs:
4747
env:
4848
GITHUB_EVENT_NAME: ${{ github.event_name }}
4949
run: |
50+
if [[ $GITHUB_EVENT_NAME == 'push' ]]; then
51+
if git status -s -b | grep -q '^##.*(no branch)$'; then
52+
echo 2>&1 "Error: Git is in detached HEAD state"
53+
exit 1
54+
fi
55+
fi
56+
5057
if [ -n "$(git status --porcelain 'nix/')" ]; then
5158
if [[ $GITHUB_EVENT_NAME == 'pull_request' ]]; then
5259
echo 2>&1 "Error: found modified files"
@@ -58,6 +65,7 @@ jobs:
5865
git push
5966
else
6067
echo 2>&1 "Error: event not supported: $GITHUB_EVENT_NAME"
68+
exit 1
6169
fi
6270
fi
6371
@@ -67,12 +75,12 @@ jobs:
6775
steps:
6876
- id: config
6977
run: |
70-
if [ "${{ github.event_name }}" == 'pull_request' ]
71-
then
72-
echo "::set-output name=ref::${{ github.event.pull_request.head.sha }}"
73-
else
74-
echo "::set-output name=ref::$GITHUB_SHA"
78+
ref=${{ github.ref }}
79+
if [ "${{ github.event_name }}" == 'pull_request' ]; then
80+
ref="${{ github.event.pull_request.head.sha }}"
7581
fi
82+
ref="${ref#refs/heads/}"
83+
echo "::set-output name=ref::$ref"
7684
7785
git config --global user.name github-actions
7886
git config --global user.email [email protected]
@@ -103,6 +111,13 @@ jobs:
103111
env:
104112
GITHUB_EVENT_NAME: ${{ github.event_name }}
105113
run: |
114+
if [[ $GITHUB_EVENT_NAME == 'push' ]]; then
115+
if git status -s -b | grep -q '^##.*(no branch)$'; then
116+
echo 2>&1 "Error: Git is in detached HEAD state"
117+
exit 1
118+
fi
119+
fi
120+
106121
if [ -n "$(git status --porcelain '*.hs')" ]; then
107122
if [[ $GITHUB_EVENT_NAME == 'pull_request' ]]; then
108123
echo 2>&1 "Error: found modified files"
@@ -114,5 +129,6 @@ jobs:
114129
git push
115130
else
116131
echo 2>&1 "Error: event not supported: $GITHUB_EVENT_NAME"
132+
exit 1
117133
fi
118134
fi

default.nix

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ let
9090
name = "kore-${version}";
9191
paths = pkgs.lib.attrValues project.kore.components.exes;
9292
};
93+
94+
# Change the compiler when updating our own resolver.
95+
compiler-nix-name = "ghc8104";
96+
index-state = "2021-02-09T00:00:00Z";
9397
};
9498

9599
in default

nix/fourmolu.nix

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{ default ? import ../default.nix {}
2+
, checkMaterialization ? false
3+
}:
4+
5+
let
6+
sources = import ./sources.nix;
7+
8+
inherit (default) compiler-nix-name index-state;
9+
10+
fourmolu-project = default.pkgs.haskell-nix.cabalProject {
11+
src = sources."fourmolu";
12+
inherit checkMaterialization compiler-nix-name index-state;
13+
materialized = ./fourmolu.nix.d;
14+
};
15+
16+
inherit (fourmolu-project.fourmolu.components.exes) fourmolu;
17+
in
18+
19+
fourmolu // {
20+
passthru =
21+
(fourmolu.passthru or {})
22+
// { inherit (fourmolu-project.plan-nix.passthru) updateMaterialized; };
23+
}

nix/fourmolu.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env nix-shell
2-
#!nix-shell ../shell.nix -i bash
2+
#!nix-shell shell.fourmolu.nix -i bash
33

44
./scripts/remove-import-groups.sh
55
./scripts/fourmolu.sh

nix/hpack.nix

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{ default ? import ../default.nix {}
2+
, checkMaterialization ? false
3+
}:
4+
5+
let
6+
sources = import ./sources.nix;
7+
8+
inherit (default) compiler-nix-name index-state;
9+
10+
hpack-project = default.pkgs.haskell-nix.cabalProject {
11+
src = sources."hpack";
12+
inherit checkMaterialization compiler-nix-name index-state;
13+
materialized = ./hpack.nix.d;
14+
};
15+
16+
inherit (hpack-project.hpack.components.exes) hpack;
17+
in
18+
19+
hpack // {
20+
passthru =
21+
(hpack.passthru or {})
22+
// { inherit (hpack-project.plan-nix.passthru) updateMaterialized; };
23+
}

nix/rematerialize.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/bin/sh
22

3-
nix-shell --run 'hpack -f kore'
3+
nix-shell nix/shell.hpack.nix --run 'hpack -f kore'
44
$(nix-build --no-out-link -A rematerialize)
55
$(nix-build --no-out-link shell.nix -A passthru.rematerialize)

nix/shell.fourmolu.nix

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
let
2+
default = import ../default.nix {};
3+
fourmolu = import ./fourmolu.nix {};
4+
inherit (default) pkgs;
5+
inherit (pkgs) fd;
6+
in
7+
8+
pkgs.mkShell {
9+
buildInputs = [ fourmolu fd ];
10+
}

nix/shell.hpack.nix

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
let
2+
default = import ../default.nix {};
3+
hpack = import ./hpack.nix {};
4+
in
5+
6+
default.pkgs.mkShell {
7+
buildInputs = [ hpack ];
8+
}

shell.nix

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ let
1212
inherit (pkgs) cabal-install ghcid stack;
1313
inherit (pkgs) fd gnumake yq z3;
1414

15-
# Change the compiler when updating our own resolver.
16-
compiler-nix-name = "ghc8104";
17-
index-state = "2021-02-09T00:00:00Z";
15+
inherit (default) compiler-nix-name index-state;
1816

1917
hls-project = import sources."nix-haskell-hls" {
2018
ghcVersion = compiler-nix-name;
@@ -35,19 +33,9 @@ let
3533
};
3634
inherit (stylish-haskell-project.stylish-haskell.components.exes) stylish-haskell;
3735

38-
fourmolu-project = default.pkgs.haskell-nix.cabalProject {
39-
src = sources."fourmolu";
40-
inherit checkMaterialization compiler-nix-name index-state;
41-
materialized = ./nix/fourmolu.nix.d;
42-
};
43-
inherit (fourmolu-project.fourmolu.components.exes) fourmolu;
36+
fourmolu = import ./nix/fourmolu.nix { inherit default checkMaterialization; };
4437

45-
hpack-project = default.pkgs.haskell-nix.cabalProject {
46-
src = sources."hpack";
47-
inherit checkMaterialization compiler-nix-name index-state;
48-
materialized = ./nix/hpack.nix.d;
49-
};
50-
inherit (hpack-project.hpack.components.exes) hpack;
38+
hpack = import ./nix/hpack.nix { inherit default checkMaterialization; };
5139

5240
in
5341

@@ -63,6 +51,7 @@ shellFor {
6351
#!/bin/sh
6452
${hlint-project.plan-nix.passthru.updateMaterialized}
6553
${stylish-haskell-project.plan-nix.passthru.updateMaterialized}
66-
${hpack-project.plan-nix.passthru.updateMaterialized}
54+
${fourmolu.passthru.updateMaterialized}
55+
${hpack.passthru.updateMaterialized}
6756
'';
6857
}

0 commit comments

Comments
 (0)