Skip to content

Commit f351d79

Browse files
authored
Merge pull request #9794 from ustaxcourt/devex-postmerge-againagain
devex: fix husky post-merge
2 parents ee677bd + 02b789e commit f351d79

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

.husky/post-merge

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ EFCMS_ROOT=$(realpath "$(dirname "$0")/..")
6464

6565
if [ -f "CHANGES.md" ]; then
6666
CHANGES_DIFF=$(git diff ORIG_HEAD HEAD -- CHANGES.md 2>/dev/null)
67-
ADDED_LINES=$(printf "%s\n" "$CHANGES_DIFF" | grep "^+" | grep -v "^+++" | grep -v "No newline")
67+
ADDED_LINES=$(printf "%s\n" "$CHANGES_DIFF" | grep "^+" | grep -v "^+++" | grep -v "No newline" || true)
6868

6969
if [ -n "$ADDED_LINES" ]; then
7070
{

.husky/post-merge.test.sh

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
# test-post-merge.sh: A script to simulate a git merge and test the post-merge hook.
44

55
EFCMS_ROOT=$(realpath "$(dirname "$0")/..")
6+
ANY_ERROR=0
67

78
set -e
89

910
GREEN='\033[0;32m'
11+
RED='\033[0;31m'
1012
NC='\033[0m' # No Color
1113

1214
echo -e "${GREEN}Setting up test environment...${NC}"
@@ -29,6 +31,32 @@ git config user.name "Test User"
2931
git add CHANGES.md
3032
git commit -m "Initial commit to the main branch" -q
3133

34+
git checkout -b no-changes -q
35+
echo "some text" > some-other-file.txt
36+
git add some-other-file.txt
37+
git commit -m "Add a file that is not CHANGES.md" -q
38+
39+
git checkout main -q
40+
mkdir -p .husky
41+
cp "${EFCMS_ROOT}/.husky/post-merge" .husky/post-merge
42+
chmod +x .husky/post-merge
43+
44+
echo -e "${GREEN}Simulating merge of 'no-changes' into 'main'...${NC}"
45+
PRE_MERGE_HEAD=$(git rev-parse HEAD)
46+
git merge no-changes --no-edit -q
47+
git update-ref ORIG_HEAD "$PRE_MERGE_HEAD"
48+
49+
echo -e "${GREEN}Executing post-merge hook...${NC}"
50+
./.husky/post-merge
51+
EXIT_CODE="$?"
52+
53+
if [[ "$EXIT_CODE" -eq 0 ]]; then
54+
echo -e "${GREEN}Exited with status ${EXIT_CODE}.${NC}"
55+
else
56+
ANY_ERROR=1
57+
echo -e "${RED}Exited with status ${EXIT_CODE}.${NC}"
58+
fi
59+
3260
git checkout -b throwaway -q
3361
{
3462
echo ""
@@ -43,12 +71,20 @@ mkdir -p .husky
4371
cp "${EFCMS_ROOT}/.husky/post-merge" .husky/post-merge
4472
chmod +x .husky/post-merge
4573

46-
4774
echo -e "${GREEN}Simulating merge of 'throwaway' into 'main'...${NC}"
4875
PRE_MERGE_HEAD=$(git rev-parse HEAD)
4976
git merge throwaway --no-edit -q
5077
git update-ref ORIG_HEAD "$PRE_MERGE_HEAD"
5178

5279
echo -e "${GREEN}Executing post-merge hook...${NC}"
5380
./.husky/post-merge
54-
echo -e "${GREEN}Test complete.${NC}"
81+
EXIT_CODE="$?"
82+
83+
if [[ "$EXIT_CODE" -eq 0 ]]; then
84+
echo -e "${GREEN}Exited with status ${EXIT_CODE}.${NC}"
85+
else
86+
ANY_ERROR=1
87+
echo -e "${RED}Exited with status ${EXIT_CODE}.${NC}"
88+
fi
89+
90+
[[ "$ANY_ERROR" -eq 0 ]] && echo -e "${GREEN}Test complete.${NC}" || echo -e "${RED}Test complete with error(s).${NC}"

0 commit comments

Comments
 (0)