From 05f199ec7fc0842f206c5b157e3a4dc9fe511bc2 Mon Sep 17 00:00:00 2001 From: Michele Cereda Date: Wed, 7 Aug 2024 22:30:37 +0200 Subject: [PATCH] chore(git-all): improve python and shell versions --- knowledge base/fish.md | 29 ++++++++++++++++++++++++ knowledge base/gnu userland/parallel.md | 3 ++- scripts/git-all.py | 7 ++++-- snippets/git.fish | 19 ++++++++++++++++ snippets/git.sh | 30 +++++++++++++++++++++++-- 5 files changed, 83 insertions(+), 5 deletions(-) create mode 100644 snippets/git.fish diff --git a/knowledge base/fish.md b/knowledge base/fish.md index 49398d1..b0d7473 100644 --- a/knowledge base/fish.md +++ b/knowledge base/fish.md @@ -44,6 +44,35 @@ diff -y -W 200 \ # Math. math 2 '+' 6 time pulumi pre --parallel (math 2 '*' (nproc)) + +# Array manipulation. +echo (seq 10)[-1..1] # -> 10 9 8 7 6 5 4 3 2 1 +set array "$array appended_element" + +# Define CLI options. +# Use all lines. +set -l opts +set opts $opts (fish_opt -s 'c' -l 'command' --required-val) +set opts $opts (fish_opt -s 'p' -l 'path' --multiple-vals) +argparse $opts -- $argv +or return +echo $_flag_command +echo $_flag_path +echo $argv + +# Switch. +switch $animal + case cat + echo evil + case wolf dog human moose dolphin whale + echo mammal + case duck goose albatross + echo bird + case shark trout stingray + echo fish + case '*' + echo I have no idea what a $animal is +end ``` For functions defined in files in `~/.config/fish/functions/` to be automatically available, the files need to: diff --git a/knowledge base/gnu userland/parallel.md b/knowledge base/gnu userland/parallel.md index 99cf1e0..ef64052 100644 --- a/knowledge base/gnu userland/parallel.md +++ b/knowledge base/gnu userland/parallel.md @@ -23,8 +23,9 @@ parallel -qt -j 1 \ # Get the exit status of all subjobs ('--joblog $outfile'). # Use all the threads you can (--jobs 0), hammering the CPU. +# Highlight in red jobs that failed. find . -type d -name .git -exec dirname "{}" + \ -| parallel --jobs 0 --tagstring {/} --joblog - \ +| parallel --jobs 0 --color-failed --tagstring {/} --joblog - \ 'git -C {} pull --recurse-submodules' # Inject Istio's sidecar to all Deployments in a Namespace. diff --git a/scripts/git-all.py b/scripts/git-all.py index 435de52..6f9decf 100755 --- a/scripts/git-all.py +++ b/scripts/git-all.py @@ -93,7 +93,7 @@ def main(debug, directories, dry_run, git_subcommand, recursive, threads, verbos pre_flight(git_subcommand=git_subcommand_parts[0]) - repositories = list(directories) + repositories = [] if recursive: for directory in directories: logging.info(f"starting from '{directory}'") @@ -104,7 +104,10 @@ def main(debug, directories, dry_run, git_subcommand, recursive, threads, verbos logging.debug(f"{directory} has repositories {', '.join(repositories_in_dir)}") repositories.extend(repositories_in_dir) - repositories = set(repositories) + else: + # Just trust the user gave repositories in input + repositories.extend(directories) + repositories = set(repositories) logging.debug(f"repositories: {', '.join(repositories)}") logging.debug(f"creating threads") diff --git a/snippets/git.fish b/snippets/git.fish new file mode 100644 index 0000000..e0f8efd --- /dev/null +++ b/snippets/git.fish @@ -0,0 +1,19 @@ +#!/usr/bin/env fish + +function git-all + if ! which -s parallel + echo "GNU parallel not found" >&2 + return + end + + argparse -s 'c/command=' 'p/path=+' 'r/recursive' -- $argv + or return + + if test "$_flag_recursive" = '-r' || test "$_flag_recursive" = '--recursive' + set repositories (find $_flag_path -type 'd' -name '.git' -exec dirname {} +) + else + set repositories $_flag_path + end + + parallel --color-failed --tagstring "{/}" "git -C {} $_flag_command" ::: $repositories +end diff --git a/snippets/git.sh b/snippets/git.sh index 82758aa..5281675 100644 --- a/snippets/git.sh +++ b/snippets/git.sh @@ -12,6 +12,7 @@ git config --local 'commit.gpgsign' true git config --local 'pull.rebase' false git clone --recurse-submodules 'git@github.com:example/ansible-role-keychron-capable.git' +git clone 'https://gitlab-ci-token:glpat-01234567ABCDEFGHijkl@gitlab.example.org/testProj/myRepo.git' git branch --list --remote 'origin/*' | cut -d/ -f2 @@ -45,7 +46,6 @@ git reset --soft HEAD~1 # or `git reset --soft HEAD^` git restore --staged '.lefthook-local.yml' # or `git reset HEAD '.lefthook-local.yml'` git commit -c ORIG_HEAD - ## # Change the default branch from 'master' to 'main'. # -------------------------------------- @@ -67,7 +67,6 @@ git symbolic-ref 'refs/remotes/origin/HEAD' 'refs/remotes/origin/main' # delete the master branch on the remote git push origin --delete 'master' - # create patches from the last commit git format-patch -n HEAD^ git format-patch HEAD^ -o './patchfile.patch' @@ -75,3 +74,30 @@ git format-patch HEAD~1 --stdout # create patches from specific commits git format-patch -1 '3918a1d036e74d47a5c830e4bbabba6f507162b1' + +git-all () { + [[ -n $DEBUG ]] && set -o xtrace + + local COMMAND + local FOLDERS=() + for (( I = $# ; I >= 0 ; I-- )); do + if [[ -d ${@[$I]} ]]; then + FOLDERS+=${@[$I]} + else + COMMAND="${@[1,-$((${#FOLDERS}+1))]}" + break + fi + done + if [[ -z "$COMMAND" ]]; then + echo "error: no command given" >&2 + return + fi + local REPOSITORIES=( $(find ${FOLDERS[@]:-'.'} -type d -name .git -exec dirname '{}' \;) ) + + parallel --color-failed --tagstring "{/}" "git -C {} $COMMAND" ::: ${REPOSITORIES[@]} + # echo -n ${REPOSITORIES[@]} | xargs -d ' ' -tP 0 -I git -C "{}" $(echo ${COMMAND[@]}) # xargs, linux + # echo -n ${REPOSITORIES[@]} | xargs -n 1 -P 0 -I {} git -C "{}" $(echo ${COMMAND[@]}) # xargs, osx + # for REPOSITORY in ${REPOSITORIES[@]}; do echo -e "\n\n---\n${REPOSITORY}"; git -C "$REPOSITORY" "$COMMAND"; done + + [[ -n $DEBUG ]] && set +o xtrace +}