-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.saci-bashrc
executable file
·347 lines (292 loc) · 10.3 KB
/
.saci-bashrc
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
################################################################################
#
# !!! DO NOT MODIFY !!!
# !!! THIS FILE IS AUTOMATICALLY GENERATED BASED ON !!!
# !!! identify-repo-ctx/scripts/.saci-bashrc.template !!!
#
# This file contains a series of shorthands that are useful in SACI's day-to-day work.
#
# The most useful functions have been given very short aliases to reduce typing
# (for example, `gsc` for `git_signed_off_commit`), but feel free to use whichever form
# you prefer (or even the raw git commands!).
#
################################################################################
################################################################################
#
# Overwrite push/popd with versions that don't polute the terminal
#
# Suggested usage is as follows (won't apply in all cases):
#
# pushd "path/to/dir"; (
# # whatever should run in that directory
# if [ -d ./something ]; then
# ./do_your_thing
# ./or_whatever
# fi
# ); popd
#
# The syntax isn't required, but wrapping the commands in parentheses means crashes
# won't leave you in the wrong directory.
# The `pushd ...; (...); popd` format simply tries to "associate" the parentheses to
# push/popd.
#
################################################################################
function pushd () {
# running pushd like this means it can't expand `~`, we must replace it with $HOME
command pushd "${1/#\~/$HOME}" "${@:2}" > /dev/null
}
function popd () {
command popd > /dev/null
}
################################################################################
#
# Shortcut to run SACI's utility scripts, with autocompletion
#
# usage: `saci add_dependabot_gha.sh` to run ./scripts/add_dependabot_gha.sh
#
################################################################################
# the directory that holds all your checked-out repositories
BASE_DIR=~
IDENTIFY_REPO_CTX="$BASE_DIR/identify-repo-ctx"
function saci {
path="$1"
shift
$IDENTIFY_REPO_CTX/scripts/$path $@
}
_saci ()
{
# This function defines how to autocomplete saci.
# See https://web.archive.org/web/20190207081539/https://debian-administration.org/article/316/An_introduction_to_bash_completion_part_1
# (and part 2, linked at the end) for an explanation.
local cur prev
COMPREPLY=()
cur=`_get_cword`
# This is the only part that matters, especially the argument after -W, which
# defines the possible completion values. These are compared with `cur`: what's
# already been typed
#
# In this case, it's the list of files in identify-repo-ctx/scripts
COMPREPLY=( $(compgen -W "$(ls $IDENTIFY_REPO_CTX/scripts)" -- ${cur}) )
return 0
}
# register _saci as the autocompletion function for saci
complete -F _saci -o filenames saci
################################################################################
#
# Shortcut to activate a virtual environment
# (assumes the venv folder is called ".venv")
#
################################################################################
function activate {
source ./.venv/bin/activate
}
# Blocks pip from installing dependencies if it is not in a virtual environment.
export PIP_REQUIRE_VIRTUALENV=true
################################################################################
#sh
# Functions to handle the ssh-agent
#
################################################################################
function ssh-start {
# activate the ssh-agent and load the ssh keys
eval `ssh-agent -s`
ssh-add ~/.ssh/id_ed25519
}
function ssh-enabled {
# returns whether the ssh-agent is active
ps -p $SSH_AGENT_PID &> /dev/null
}
function ssh-confirm {
# confirms ssh-agent is enabled, enables if not
if ! ssh-enabled; then
ssh-start
fi
}
################################################################################
#
# Functions to handle gcert
#
################################################################################
function gcert-confirm () {
if ! gcertstatus > /dev/null ; then
gcert
fi
}
################################################################################
#
# Git utilities
#
################################################################################
function git_main_branch {
# returns name of main branch
git remote show origin | grep 'HEAD branch:' | sed 's/.*: //'
}
function git_pull_upstream {
# pulls the upstream's main branch
main_branch="$(git_main_branch)"
git pull upstream "$main_branch"
}
function git_remote {
# Returns name of current remote
# Tries to get the current branch's remote repo
remote="$(git branch -vv | \
grep -E "^\* *[^ ]+ *[^ ]* *\[[^\]+/" | \
sed -E "s~.*\[(.*)/.*~\1~"
)"
if [ -n "$remote" ]; then
echo "$remote"
return 0
fi
# If the current branch doesn't have a remote (i.e. never pushed), get the default
# remote. If one hasn't been explicitly set, assume "origin".
remote="$(git config remote.pushDefault)"
echo "${remote:-origin}"
}
################################################################################
#
# Git shorthands
#
################################################################################
function git_commit_amend_no_edit {
# Meant for those times where you just want to quickly amend a commit's diff
#
# Usage: `gca`, without arguments
if git diff --cached --quiet ; then
# Warn if I forgot to stage something first.
# I do this all the time, and `--amend` pretends this is fine
RED='\033[0;31m'
NO_COLOR='\033[0m'
echo -e "${RED}YOU FORGOT TO STAGE SOMETHING!${NO_COLOR}"
return 1
fi
ssh-confirm
git commit --amend --no-edit
}
alias gca="git_commit_amend_no_edit"
function git_new_check {
# Updates the main branch and creates a new branch for a new check
#
# Usage: `gnc $BRANCH_NAME`
git_pull_upstream
git checkout -b "$1" "$(git_main_branch)"
}
alias gnc="git_new_check"
function git_push {
# Pushes code to remote repository.
#
# If dealing with a remote:
# - Gerrit repository*: then the push uses the standard Gerrit command:
# `git push origin HEAD:refs/for/main` (or whatever the main branch is).
# Currently assumes the remote repository is set as "origin", and does not accept
# any arguments.
# - standard Git repository (i.e. GitHub): it first unsets GITHUB_TOKEN, which is
# often defined after running Scorecard and can block the push, after which it
# performs a standard `git push` (followed by whichever arguments you pass, such
# as --force).
#
# Args:
# $1: tested to see if it is a valid remote. If so, it is used to identify whether
# the remote repository is a Gerrit repo. If not, the remote is assumed to be
# origin.
# $@: If pushing to a GitHub repository, all passed arguments (including $1) are
# appended directly to the `git push` command, allowing you to call
# `gp --force`, for example.
#
# \* Gerrit repos are distinguished by their use of `sso://` in the repo URL. This
# isn't strictly necessary, but is the most common way to clone GoB repos.
if [ -n "$1" ]; then
remote="$1"
else
remote="$(git_remote)"
fi
is_gerrit="$(git remote get-url $remote 2> /dev/null | grep sso://)"
if [ -n "$is_gerrit" ]; then
git push "$remote" "HEAD:refs/for/$(git_main_branch)"
else
unset GITHUB_TOKEN
git push $@
fi
}
alias gp="git_push"
function git_signed_off_commit {
# Creates a commit that's signed-off.
# Assumes git is configured to always crypto-sign commits.
#
# Usage: identical to `git commit`
ssh-confirm
if [ $# -eq 0 ]; then
git commit -s
else
git commit -s "$@"
fi
}
alias gsc="git_signed_off_commit"
function git_rebase {
# Identical to `git rebase`, just ensures the ssh-agent is active first
ssh-confirm
git rebase $@
}
alias gr="git_rebase"
function git_rebase_on_upstream() {
# Pulls the upstream's main branch, then rebases the current branch onto it
# Usage: `gru`, without arguments
git_pull_upstream
main_branch="$(git_main_branch)"
gr "$main_branch"
}
alias gru="git_rebase_on_upstream"
GITHUB_USERNAME=diogoteles08
function git_shallow_clone {
# Performs a shallow clone of the given repository in the $BASE_DIR and moves the
# current workfing directory to the cloned repository
#
# Takes one argument, which may be:
# - a full URL: as used with `git clone`
# - org/repo: becomes github.com/org/repo
# - repo: bcomes github.com/your_username/repo
cd "$BASE_DIR"
GH="https://github.com"
url="$1"
if [[ ! $url =~ / ]]; then
# Doesn't have any /, so it's just a repo name, assuming user pnacht
url="$GH/$GITHUB_USERNAME/$url"
elif [[ ! $url =~ /.*/ ]]; then
# We got past the first `if`, so we know it has one `/`, but doesn't have two.
# Must be user/org
url="$GH/$url"
fi
git clone --filter=blob:none $url
# directory name is the last part of the URL, after all the `/`
dir_name="${url/*\//}"
cd $dir_name
}
alias gshc="git_shallow_clone"
################################################################################
#
# Shortcut to run Scorecard in json format, with details, pretty-printed by jq
#
################################################################################
function scorecard_pretty {
# Usage: identical to Scorecard
scorecard --show-details --format json $@ | jq .
}
alias sc="scorecard_pretty"
function scorecard_pretty_head {
# "Scorecard at HEAD" (or whatever is currently checked out)
# Usage: identical to Scorecard
pushd "$BASE_DIR/scorecard"; (
go run main.go --show-details --format json $@ | jq .
); popd
}
alias sch="scorecard_pretty_head"
################################################################################
#
# Shortcut to run osv-scanner from a checked-out repository
#
################################################################################
function osv {
# Usage: identical to osv-scanner
pushd "$BASE_DIR/osv-scanner"; (
go run ./cmd/osv-scanner/main.go $@
); popd
}