-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathkeys.sh
More file actions
executable file
·207 lines (166 loc) · 5.15 KB
/
Copy pathkeys.sh
File metadata and controls
executable file
·207 lines (166 loc) · 5.15 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#! /bin/cat
KEYBOARD=~/$(dirname $BASH_SOURCE)/keyboard
keys_git () {
local __doc__="""Run git in the keyboard directory."""
git -C $(keyboard_path) "$@"
}
main_to_keyboard () {
keys_git log keyboard --merges --grep="Merge branch '__main__'" -1 --pretty=format:"%H"
}
commit_key_file () {
local __doc__="""Commit a single file to the keyboard branch with its filename as the commit message."""
local file_=$1
local letter_=$(basename "$file_" .sh)
# Check if a commit with the same message (letter) exists after the last merge from __main__
local existing_commit_=$(keys_git log keyboard --pretty=format:"%H" --grep="^$letter_$" --since="$(main_to_keyboard)")
if [ -n "$existing_commit_" ]; then
# Amend the existing commit if found
keys_git commit --amend --no-edit --only "$file_"
else
# Create a new commit with the filename letter as the message
keys_git commit -m "$letter_" --only "$file_"
fi
}
push_keys () {
local __doc__="""Stash changes, commit modified keyboard scripts, and push the keyboard branch to remote."""
(cd $(keyboard_path)
# Stash any existing changes on the current branch
git stash --include-untracked
# Ensure the keyboard branch exists, and if not, create it from __main__
if ! git rev-parse --verify keyboard >/dev/null 2>&1; then
git checkout __main__
git checkout -b keyboard
else
git checkout keyboard
fi
# Process each modified file individually
for file_ in $(git ls-files -m); do
commit_key_file "$file_"
done
# Push the keyboard branch to the remote repository
git push origin keyboard
# Return to the previous branch and apply the stashed changes
git checkout -
git stash pop 2>/dev/null
)
}
KEYS_HASH="fred"
push_changed_keys () {
local __doc__="""Check if ~/bash/keyboard/*.sh has changed, call keys_read, and commit changes if needed."""
local new_hash_=$(md5sum ~/bash/keyboard/*.sh | md5sum)
if [ "$new_hash_" != "$KEYS_HASH" ]; then
KEYS_HASH=$new_hash_
export KEYS_HASH
keys_read
push_keys
fi
}
KEYBOARD="$(dirname $(readlink -f $BASH_SOURCE))/keyboard"
KEYS_SOURCE="$(readlink -f "$BASH_SOURCE")"
BASH_DIR="$(dirname "$KEYS_SOURCE")"
KEYBOARD_DIR=$BASH_DIR/keyboard
.k () {
source "$KEYS_SOURCE"
keys_read
}
keyboard_path () {
echo ~/bash/keyboard/$1
}
key_exists () {
local script_=
for script_ in $(keyboard_path $1); do
quietly test -f "$script_" && return 0
done
return 5
}
path_to_key () {
local script_="${1%.sh}.sh"
echo $(keyboard_path $script_)
}
ls_path_to_key () {
quietly ls $(path_to_key $1)
}
echo_key () {
[[ $1 ]] || return 4
local script_=$(path_to_key $1)
echo $script_
}
echo_keys () {
[[ $@ ]] || return 0
(
command cd $(keyboard_path " ")
for arg_ in $(quietly ls "$@"); do
[[ $arg_ =~ __init__ ]] && continue
[[ $arg_ ]] || continue
ls $arg_
done
)
}
key_scripts () {
(
command cd $(keyboard_path)
readlink -f $(echo_keys [a-z12].sh)
)
}
key_init () {
path_to_key __init__
}
same_path () {
[[ $2 ]] || return 4
[[ $1 ]] || return 8
[[ $(readlink -f "$1") == $(readlink -f "$2") ]]
}
keys_merge () {
local __doc__="""Create a pull request from keyboard to __main__, capture the PR number, merge it, and delete the keyboard branch."""
(cd $(keyboard_path)
# Ensure we're up to date with the remote
git fetch origin
# Create a pull request from keyboard to __main__ and capture the PR number
local pr_number=$(gh pr create \
--base __main__ \
--head keyboard \
--title "Merge keyboard into __main__" \
--body "Automated merge of keyboard changes into __main__" \
--json number \
--jq '.number')
# Set the version number or use the PR number as needed
echo "PR number is: $pr_number"
# You can now use $pr_number to set your version number or perform other actions
# Merge the pull request using admin privileges (bypassing review if needed)
gh pr merge $pr_number --admin --delete-branch --squash
# Optionally delete the local keyboard branch after the merge
git branch -d keyboard
)
}
keys_write () {
local init_=$(key_init)
echo "#! /usr/bin/env bat -l bash" > $init_
for script_ in $(key_scripts); do
grep -v "^#! " $script_ >> $init_
done
}
keys_read () {
keys_write
. $(key_init)
}
keys_vim () {
local init_=$(key_init) files_=
if [[ ! "$@" ]]; then
files_="$(key_scripts)"
vv $files_
return
fi
local option_= path_= file_=
dir_=$(keyboard_path)
for option_ in "$@"; do
path_=$dir_/$option_
[[ -f ${path_} ]] && file_=${path_}
[[ -f ${path_}.sh ]] && file_=${path_}.sh
[[ -f $file_ ]] && files_="$files_ $file_"
file_=
done
[[ $files_ ]] || return 6
vim -p $files_
keys_read
}
keys_read