-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgit-bookmark
executable file
·145 lines (116 loc) · 3.54 KB
/
git-bookmark
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env bash
# shellcheck disable=SC2154
set -o errexit # abort on nonzero exitstatus
set -o nounset # abort on unbound variable
set -o pipefail # don't hide errors within pipes
# set -o xtrace
eval "$(
docopts -V - -h - : "$@" <<EOF
Usage:
git-bookmark init [--session=<name>] [--branch=<name>] [--gitv]
git-bookmark add [--session=<name>] [--branch=<name>] [--gitv] <url>...
git-bookmark list [--session=<name>] [--branch=<name>] [--gitv]
git-bookmark open [--session=<name>] [--branch=<name>] [--gitv] [--browser=<executable>]
git-bookmark edit [--session=<name>] [--branch=<name>] [--gitv] [--editor=<executable>] [--message=<message>]
git-bookmark -h | --help
git-bookmark --version
Options:
-h --help Displays this screen.
--version Displays version information.
--session=<name> Name of the bookmark file
[default: links].
--branch=<name> Name of the branch to look for the bookmark file
[default: bookmarks].
--editor=<executable> Editor to use when editing a bookmark file
[default: $EDITOR].
--message=<message> Commit message when editing.
When ommited it will prompt like git does.
--browser=<executable> Browser to use when opening from a bookmark file
[default: $BROWSER].
--gitv Show all of the underlyings git commands stderr.
----
git-bookmark 0.5.1
Copyright (C) 2020 Xavier Francisco
License MIT
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
EOF
)"
require_git_repo() {
# Abort when a git repo isn't found
if ! _git rev-parse --git-dir >/dev/null; then
echo "Not inside a git repo. Aborting"
exit 1
fi
}
_git() {
if [ -z "${gitv}" ]; then
{ git "$@" ; }
else
{ git "$@" ; } 2> /dev/null
fi
}
enter_branch() {
# Enter the branch to work with
origin="$(git rev-parse --show-toplevel)"
clone="$(mktemp --directory)"
if ! _git clone "$origin" --branch="$branch" "$clone"; then
echo 'Bookmark branch not initialized. Aborting.'
echo ''
echo "Tip: Run 'git bookmark init'"
exit 1
fi
trap 'rm --interactive=never --recursive "$clone"' EXIT
# Change directory to our cloned repo
cd "$clone" > /dev/null || exit 1
}
_git_acp() {
_git add "$session"
_git commit "$@"
_git push origin "$branch"
}
list_bookmarks() {
# Read our bookmarks file
# Strip: comments, empty lines, leading&trailing spaces
sed --expression 's/\s*#.*$//' \
--expression 's/^ *//; s/ *$//; /^$/d' \
"$session"
}
# Begin
require_git_repo
# When Initiating
if $init; then
original="$(git rev-parse --show-toplevel)"
clone="$(mktemp --directory)"
trap 'rm --interactive=never --recursive "$clone"' EXIT
cd "$clone" > /dev/null || exit 1
_git init > /dev/null
_git remote add origin "$original"
_git checkout -b "$branch"
touch "$session"
_git_acp --message "Initial Commit" --allow-empty
# When Adding
elif $add; then
enter_branch
for u in "${url[@]}"; do
echo "${u}" >> "$session"
done
_git_acp --message "Added ${#url[@]} new bookmarks"
# When Editing
elif $edit; then
enter_branch
"$editor" "$session"
if [ -z "$message" ]; then
_git_acp
else
_git_acp --message "$message"
fi
# When Opening
elif $open; then
enter_branch
list_bookmarks | xargs "$browser"
# When Listing
elif $list; then
enter_branch
list_bookmarks
fi