feat: supported branching for commit checking

This commit is contained in:
Leyla Becker 2025-10-19 18:50:26 -05:00
parent 0c88746da1
commit 260e37e016
3 changed files with 100 additions and 9 deletions

18
.hooks/post-merge Executable file
View file

@ -0,0 +1,18 @@
#!/usr/bin/env nix-shell
#! nix-shell -i bash ../shell.nix
# Get current branch name
current_branch=$(git branch --show-current)
# Only restore stash if we're on main branch and a merge just completed
if [ "$current_branch" = "main" ]; then
# Check if there are any stashes to restore
if git stash list | grep -q "stash@"; then
echo "Post-merge: restoring stashed changes on main branch"
git stash pop -q
else
echo "Post-merge: no stash to restore on main branch"
fi
else
echo "Post-merge: no action needed on branch '$current_branch'"
fi

37
.hooks/pre-merge-commit Executable file
View file

@ -0,0 +1,37 @@
#!/usr/bin/env nix-shell
#! nix-shell -i bash ../shell.nix
# Get the target branch (the branch being merged into)
target_branch=""
# Check if we're in the middle of a merge
if [ -f .git/MERGE_HEAD ]; then
# We're in a merge, check if the current branch is main
current_branch=$(git branch --show-current)
if [ "$current_branch" = "main" ]; then
target_branch="main"
fi
fi
# If we're merging into main, run nix flake check
if [ "$target_branch" = "main" ]; then
echo "Merging into main branch - running nix flake check..."
echo "stashing all uncommitted changes"
git stash -q --keep-index
echo "checking flakes all compile"
nix flake check
if [ ! $? -eq 0 ]; then
echo "Error: nix flake check failed. Merge aborted."
echo "Please fix the issues and try merging again."
exit 1
fi
echo "nix flake check passed. Merge can proceed."
else
echo "Not merging into main branch, skipping nix flake check."
fi
exit 0

View file

@ -1,5 +1,15 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# Get current branch and git status for branch-aware behavior
current_branch=$(git branch --show-current 2>/dev/null || echo "unknown")
git_status=$(git status --porcelain 2>/dev/null || echo "")
# Default values
default_target=$(hostname)
default_user="$USER"
default_host=$(hostname)
default_mode=$(if [[ "$current_branch" != "main" ]]; then echo "test"; else echo "switch"; fi)
if [ -d "result" ]; if [ -d "result" ];
then then
preserve_result=true preserve_result=true
@ -42,14 +52,29 @@ while [ $# -gt 0 ]; do
;; ;;
--help|-h) --help|-h)
echo "--help -h: print this message" echo "--help -h: print this message"
echo "--target -t: set the target system to rebuild on" echo "--target -t: defaults to the current system"
echo "--flake -f: set the flake to rebuild on the target system" echo " currently: $default_target"
echo "--mode -m: set the mode to rebuild flake as on the target system" echo "--flake -f: defaults to same as target"
echo "--user -u: set the user to rebuild flake as on the target system" echo " currently: ${target:-$default_target}"
echo "--host: set the host that the flake will be rebuilt on (unset for current machine)" echo "--mode -m: defaults to 'switch', but 'test' on non-main branches"
echo " currently would be: $default_mode"
echo "--user -u: defaults to the current user"
echo " currently: $default_user"
echo "--host: defaults to building on the current machine"
echo " currently: $default_host"
echo "--preserve-result: do not remove the generated result folder after building" echo "--preserve-result: do not remove the generated result folder after building"
echo "--no-preserve-result: remove any result folder after building" echo "--no-preserve-result: remove any result folder after building"
echo "--show-trace: show trace on builds" echo "--show-trace: show trace on builds"
echo ""
echo "Branch-aware behavior:"
echo " - On non-main branches: defaults to test mode with warning"
echo " - On main with uncommitted changes: shows warning about creating a branch"
echo " - Current branch: $current_branch"
if [[ -n "$git_status" ]]; then
echo " - Git status: uncommitted changes detected"
else
echo " - Git status: clean working tree"
fi
exit 0 exit 0
;; ;;
*) *)
@ -60,10 +85,21 @@ while [ $# -gt 0 ]; do
shift shift
done done
target=${target:-$(hostname)} target=${target:-$default_target}
flake=${flake:-$target} flake=${flake:-$target}
mode=${mode:-switch} mode=${mode:-$default_mode}
user=${user:-$USER} user=${user:-$default_user}
# Branch-aware warnings and behavior
if [[ "$current_branch" != "main" ]] && [[ "$mode" == "test" ]]; then
echo "⚠️ WARNING: You are on branch '$current_branch' (not main)"
echo " Defaulting to test mode to prevent accidental system changes"
echo " Specify --mode=switch explicitly if you want to apply changes"
elif [[ "$current_branch" == "main" ]] && [[ -n "$git_status" ]] && [[ "$mode" != "test" ]]; then
echo "⚠️ WARNING: You are on main branch with uncommitted changes"
echo " Consider creating a feature branch for development:"
echo " git checkout -b feature/your-feature-name"
fi
command="nixos-rebuild $mode --sudo --ask-sudo-password --flake .#$flake" command="nixos-rebuild $mode --sudo --ask-sudo-password --flake .#$flake"
@ -91,4 +127,4 @@ then
then then
rm -r result rm -r result
fi fi
fi fi