/
git-unstage
executable file
·66 lines (56 loc) · 2.29 KB
/
git-unstage
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env bash
#
# ╭╮
# ╭╯╰╮
# ╭━━┳╮╭┳━╮╭━━╋╮╭╋━━┳━━┳━━╮
# ┃╭╮┃┃┃┃╭╮┫ ━┫┃┃┃╭╮┃╭╮┃┃━┫
# ┃╰╯┃╰╯┃┃┃┣━━┃┃╰┫╭╮┃╰╯┃┃━┫
# ╰━╮┣━━┻╯╰┻━━╯╰━┻╯╰┻━╮┣━━╯
# ╭━╯┃ ╭━╯┃
# ╰━━╯ ╰━━╯
#
#
# gunstage
# 🔫 `git unstage` as a service
# author: Lucas Larson
# license: GPLP 1.0 or later (GPL-compatible)
#
gunstage () {( # 🔫 `git unstage` as a service
IFS="$(printf '\n\t')"
# check whether we’re in a Git repository
# https://stackoverflow.com/a/53809163
if git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
# if program was called with any arguments,
# then convert them into an array called files,
# otherwise use `.` for everything in the current directory and below
files=(
"${@:-.}"
)
# run the same command against each element in the array
# https://stackoverflow.com/a/12711853
for file in "${files[@]}"; do
# https://github.com/gggritso/gggritso.com/commit/a07b620
git reset --quiet HEAD -- "${file}"
# perform a `git status` only if the loop was successful
done && git status
else
# we’re not in a Git repository
# store the failed if as a return status
# https://github.com/ohmyzsh/ohmyzsh/pull/9238#discussion_r484806772
gunstage=$?
# if called from outside a Git repository, then
# provide instructions on how to use it
printf 'gunstage must be called from within a Git repository\n'
printf 'create a file and a repository, then stage and gunstage:\n
touch file && # create a file called '
printf '\xe2\x80\x9cfile\xe2\x80\x9d \x5c
git init && # make a Git repository if necessary \x5c
git add file && git status # stage the file you just created
# now use \x60gunstage\x60 to unstage the file
gunstage file # huzzah.\n\n
gunstage # \xf0\x9f\x94\xab \x60git unstage\x60 as a service
https://github.com/LucasLarson/gunstage\n\n'
return "${gunstage}"
fi
)}
gunstage "$@"