feat: update git commands/aliases

This commit is contained in:
2024-06-17 14:26:26 +03:00
parent 5c11ec216d
commit 7cc7c06bab
4 changed files with 94 additions and 8 deletions

View File

@@ -251,9 +251,11 @@ customCommands:
options:
- name: 'Open Project'
value: 'project'
- name: 'Create Pull Request'
value: 'pr'
- name: 'Open Pull Requests'
value: 'prs'
- name: 'Open CI'
- name: 'Open CI/Actions'
value: 'ci'
command: 'git open {{.Form.action}} {{.SelectedLocalBranch.Name}}'
loadingText: 'Opening...'

View File

@@ -13,7 +13,7 @@ rsync_template="rsync $rflags {}"
# CLI Args
refresh_zplug=0
refresh_tmux=0
set_git_configs=$([[ -z $(git config --global user.signingkey) ]] && echo 1 || echo 0)
set_git_configs=$(git config --global user.signingkey &>/dev/null && echo 0 || echo 1)
while [[ $# -gt 0 ]]; do
case $1 in
@@ -72,6 +72,7 @@ if [[ -z $(git config --global user.email) ]]; then
fi
if [[ $set_git_configs -eq 1 ]]; then
echo_cyan "Setting git global config..."
git config --global user.signingkey "~/.ssh/id_casraf.pub"
git config --global filter.lfs.clean "git-lfs clean -- %f"
git config --global filter.lfs.smudge "git-lfs smudge -- %f"
@@ -100,11 +101,14 @@ if [[ $set_git_configs -eq 1 ]]; then
git config --global alias.unchanged "update-index --assume-unchanged"
git config --global alias.changed "update-index --no-assume-unchanged"
git config --global alias.show-unchanged "!git ls-files -v | sed -e 's/^[a-z] //p; d'"
git config --global alias.list-aliases "!git config --global --list | grep --color alias\. | grep -v list-aliases | sed \"s/alias\./\$(tput setaf 1)/\" | sed \"s/=/\$(tput sgr0)=/\""
# Open
git config --global alias.open "!source $DOTFILES/plugins/git_custom_commands.plugin.zsh open"
git config --global alias.pr "!source $DOTFILES/plugins/git_custom_commands.plugin.zsh prs"
git config --global alias.ci "!source $DOTFILES/plugins/git_custom_commands.plugin.zsh ci"
git config --global alias.open "!\$DOTFILES/plugins/git_custom_commands.plugin.zsh open"
git config --global alias.project "open project"
git config --global alias.pr "open pr"
git config --global alias.prs "open prs"
git config --global alias.ci "open ci"
fi
if [[ ! -f $(which delta) ]]; then

View File

@@ -461,6 +461,35 @@ strip-home() {
echo ${dir/$HOME/$repl}
}
uriencode() {
len="${#1}"
for ((n = 0; n < len; n++)); do
c="${1:$n:1}"
case $c in
[a-zA-Z0-9.~_-]) printf "$c" ;;
*) printf '%%%02X' "'$c"
esac
done
}
posix_compliant() {
strg="${*}"
printf '%s' "${strg%%[%+]*}"
j="${strg#"${strg%%[%+]*}"}"
strg="${j#?}"
case "${j}" in "%"* )
printf '%b' "\\0$(printf '%o' "0x${strg%"${strg#??}"}")"
strg="${strg#??}"
;; "+"* ) printf ' '
;; * ) return
esac
if [ -n "${strg}" ] ; then posix_compliant "${strg}"; fi
}
uridecode() {
posix_compliant "${*}"
}
# select random element from arguments
# always keep last, breaks syntax highlighting
randarg() {

57
plugins/git_custom_commands.plugin.zsh Normal file → Executable file
View File

@@ -1,3 +1,7 @@
#!/usr/bin/env zsh
type uriencode >/dev/null || source "$DOTFILES/plugins/functions.plugin.zsh"
git_get_remote() {
remote=$(git remote -v | grep "(push)" | awk '{print $2}')
echo $remote
@@ -125,6 +129,44 @@ git_open_pr_list() {
return 0
}
git_open_new_pr() {
remote=$(git_get_remote)
if [[ -z $remote ]]; then
echo "No remote found"
return 1
fi
remote_type=$(git_get_remote_type $remote)
if [[ -z $remote_type ]]; then
echo "Unknown remote type for $remote"
return 1
fi
repo_path=$(git_get_repo_path $remote)
branch=$(git branch --show-current)
default_branch=$(git remote show $remote | grep "HEAD branch" | awk '{print $3}')
if [[ -z $default_branch ]]; then
default_branch="master"
fi
branch=$(uriencode $branch)
default_branch=$(uriencode $branch)
case $remote_type in
github)
open_url "https://github.com/$repo_path/compare/$branch...$default_branch"
;;
gitlab)
open_url "https://gitlab.com/$repo_path/-/merge_requests/new?merge_request%5Bsource_branch%5D=$branch&merge_request%5Btarget_branch%5D=$default_branch"
;;
bitbucket)
open_url "https://bitbucket.org/$repo_path/pull-requests/new?source=$branch&t=1"
;;
esac
return 0
}
git_open_pipelines() {
branch=$1
if [[ -z $branch ]]; then
@@ -164,18 +206,27 @@ git_open_pipelines() {
git_open() {
if [[ -z $1 ]]; then
echo "Usage: git open_url <command>"
echo "Usage: git open <command>"
echo "Commands:"
echo " project|repo|open|. Open the project"
echo " prs Open the PR list"
echo " pr Open a new PR"
echo " actions|pipelines|ci Open the CI/CD pipelines"
return 1
fi
case $1 in
project|repo|open)
project|repo|\.)
git_open_project
;;
pr|prs)
prs)
shift
git_open_pr_list $@
;;
pr)
shift
git_open_new_pr $@
;;
actions|pipelines|ci)
shift
git_open_pipelines $@