Skip to content

Commit 26949a6

Browse files
authored
feat: manage global gitignore in git plugin (#10)
1 parent c54f191 commit 26949a6

2 files changed

Lines changed: 68 additions & 2 deletions

File tree

plugins/git/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Git
2+
3+
## Aliases
4+
5+
| Alias | Command | Description |
6+
|-------|---------|-------------|
7+
| `g` | `git` | Shorter alias for `git`. |
8+
| `gd` | `git d` | Runs `git d`. |
9+
| `gs` | `git s` | Runs `git s`. |
10+
| `gr` | `git r` | Runs `git r`. |
11+
12+
## Global Git Ignore
13+
14+
The plugin maintains a user-level global gitignore file at
15+
`$HOME/.config/git/ignore`. The file can be changed by setting the
16+
`GIT_GLOBAL_IGNORE_FILE` environment variable before loading the plugin.
17+
18+
On activation the plugin configures `core.excludesFile` to point to this
19+
file if it is not already set.
20+
21+
### Updating
22+
23+
Use the `git_global_ignore_update` command to replace the current contents
24+
with templates from [github/gitignore](https://github.com/github/gitignore):
25+
26+
```sh
27+
git_global_ignore_update macOS Emacs
28+
```
29+
30+
Multiple template names may be supplied. An alias `ggiu` is also
31+
available.
32+

plugins/git/git.plugin.sh

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# vim: set ft=sh:
2+
# shellcheck shell=sh
13
####
24
# Plugin: git
35
####
@@ -6,5 +8,37 @@ alias gd='git d'
68
alias gs='git s'
79
alias gr='git r'
810

9-
# @todo manage global excludes file and keep it updated
10-
# @see https://github.com/github/gitignore
11+
git_global_ignore_file="${GIT_GLOBAL_IGNORE_FILE:-$HOME/.config/git/ignore}"
12+
13+
git_setup_global_ignore() {
14+
mkdir -p "$(dirname "$git_global_ignore_file")"
15+
touch "$git_global_ignore_file"
16+
17+
current_excludes_file="$(git config --global core.excludesFile 2>/dev/null || true)"
18+
if [ "$current_excludes_file" != "$git_global_ignore_file" ]; then
19+
git config --global core.excludesFile "$git_global_ignore_file"
20+
fi
21+
}
22+
23+
git_global_ignore_update() {
24+
if [ "$#" -eq 0 ]; then
25+
printf '%s\n' "Usage: git_global_ignore_update <Template> [Template...]" >&2
26+
return 1
27+
fi
28+
29+
git_setup_global_ignore || return 1
30+
: >"$git_global_ignore_file"
31+
32+
for template in "$@"; do
33+
url="https://raw.githubusercontent.com/github/gitignore/main/Global/${template}.gitignore"
34+
if ! curl -fsSL "$url" >>"$git_global_ignore_file"; then
35+
printf '%s\n' "Failed to download template: $template" >&2
36+
return 1
37+
fi
38+
printf '\n' >>"$git_global_ignore_file"
39+
done
40+
}
41+
42+
alias ggiu='git_global_ignore_update'
43+
44+
git_setup_global_ignore

0 commit comments

Comments
 (0)