Skip to content

Commit

Permalink
feat: automatically add linked files to gitignore
Browse files Browse the repository at this point in the history
closes: #27
  • Loading branch information
blaggacao committed Jul 30, 2022
1 parent 1c8f4b3 commit ccd9d65
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
18 changes: 18 additions & 0 deletions lib/hooks/link.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{ configFile, hookConfig }:
let
inherit (hookConfig) output extra;

gitignore-sentinel = "ignore-linked-files";
in
''
# Check if the link is pointing to the existing derivation result
Expand All @@ -25,4 +27,20 @@ in
# this was an existing file
error "refusing to overwrite ${output}"
fi
# Add this output to gitignore if not already
if ! test -f .gitignore
then
touch .gitignore
fi
if ! grep -qF "${output}" .gitignore
then
if ! grep -qF "${gitignore-sentinel}" .gitignore
then
echo -e "\n# nixago: ${gitignore-sentinel}" >> .gitignore
fi
newgitignore="$(awk '1;/${gitignore-sentinel}/{ print "${output}"; }' .gitignore)"
echo -e -n "$newgitignore" > .gitignore
git add .gitignore
log "${output} added to .gitignore"
fi
''
6 changes: 5 additions & 1 deletion tests/hook/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ let
linkHook = pkgs.writeShellScript "link" (lib.make linkRequest).shellHook;
copyHook = pkgs.writeShellScript "copy" (lib.make copyRequest).shellHook;
in
pkgs.runCommand "test.hook" { }
pkgs.runCommand "test.hook" { buildInputs = [ pkgs.gitMinimal ]; }
''
git init
echo "Testing link hook"
source ${linkHook}
stat test.json
Expand All @@ -30,5 +32,7 @@ pkgs.runCommand "test.hook" { }
stat test.json
stat test.txt
rm -rf .git
touch $out
''

0 comments on commit ccd9d65

Please sign in to comment.