diff --git a/zsh/custom-functions/custom-functions.plugin.zsh b/zsh/custom-functions/custom-functions.plugin.zsh index 7fd2dcc..a3f0c22 100755 --- a/zsh/custom-functions/custom-functions.plugin.zsh +++ b/zsh/custom-functions/custom-functions.plugin.zsh @@ -93,3 +93,31 @@ function filesize() { stat -c "%s bytes" "$@" fi } + +# ignore files from git +# usage: ignore_from_git file1.md file2.txt folder1/ folder2/** +ignore_from_git() { + local exclude_file=".git/info/exclude" + + if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then + echo "Error: Not a git repository." >&2 + return 1 + fi + + if [ $# -eq 0 ]; then + echo "Error: No file or folder was specified." >&2 + echo "Usage: ignore_from_git file1.md file2.txt folder1/ folder2/**" >&2 + return 1 + fi + + for pattern in "$@"; do + local escaped_pattern=$(printf '%s\n' "$pattern" | sed 's/[[\.*^$()+?{|]/\\&/g') + + if ! grep -Fxq -- "$pattern" "$exclude_file" 2>/dev/null; then + echo "$pattern" >> "$exclude_file" + echo "Added: $pattern to $exclude_file" + else + echo "$pattern already exists in $exclude_file" + fi + done +}