2
0
mirror of https://github.com/kazhala/dotbare synced 2024-11-08 07:10:24 +00:00
dotbare/helper/git_query.sh

179 lines
5.9 KiB
Bash
Raw Normal View History

2020-05-11 01:56:27 +00:00
#!/usr/bin/env bash
#
# helper functions to obtain git related information
#######################################
# let user select a commit interactively
# Arguments:
# $1: the helper message to display in the fzf header
# $2: files to show diff against HEAD
# Outputs:
# the selected commit 6 char code
# e.g. b60b330
#######################################
function get_commit() {
local header="${1:-select a commit}"
local files="$2"
if [[ -z "${files}" ]]; then
/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" \
2020-05-10 22:52:06 +00:00
log --oneline --color=always --decorate=short \
| fzf --no-multi --header="${header}" \
2020-05-10 22:52:06 +00:00
--preview "echo {} \
2020-04-17 07:23:39 +00:00
| awk '{print \$1}' \
| xargs -I __ /usr/bin/git --git-dir=${DOTBARE_DIR} --work-tree=${DOTBARE_TREE} \
2020-05-10 22:52:06 +00:00
show --color=always __" \
| awk '{print $1}'
else
/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" \
2020-05-10 22:52:06 +00:00
log --oneline --color=always --decorate=short \
| fzf --no-multi --header="${header}" --preview "echo {} \
| awk '{print \$1}' \
| xargs -I __ /usr/bin/git --git-dir=${DOTBARE_DIR} --work-tree=${DOTBARE_TREE} \
2020-05-10 22:52:06 +00:00
diff --color=always __ $files" \
| awk '{print $1}'
fi
}
#######################################
# let user select a branch interactively
# Arguments:
# $1: the helper message to display in the fzf header
# Outputs:
# the selected branch name
# e.g. master
#######################################
function get_branch() {
local header="${1:-select a branch}"
/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" branch -a \
| awk '{
if ($0 ~ /\*.*\(HEAD.*/) {
gsub(/\* /, "", $0)
print "\033[32m" $0 "\033[0m"
} else if (match($1, "\\*") != 0) {
print "\033[32m" $2 "\033[0m"
} else if ($0 ~ /^[ \t]+remotes\/.*/) {
gsub(/[ \t]+/, "", $0)
print "\033[31m" $0 "\033[0m"
} else {
gsub(/^[ \t]+/, "", $0)
print $0
}
}' \
| fzf --no-multi --header="${header}" \
2020-05-10 22:46:38 +00:00
--preview="echo {} \
| awk '{
if (\$0 ~ /.*HEAD.*/) {
print \"HEAD\"
} else {
print \$0
}
}' \
| xargs -I __ /usr/bin/git --git-dir=${DOTBARE_DIR} --work-tree=${DOTBARE_TREE} \
log --oneline --graph --color=always --decorate=short __"
}
2020-04-16 05:30:48 +00:00
#######################################
# let user select a dotbare tracked file interactively
# Arguments:
# $1: the helper message to display in the fzf header
2020-05-07 01:40:33 +00:00
# $2: print option, values (full|raw)
# $3: if exist, don't do multi selection, do single
2020-04-16 05:30:48 +00:00
# Outputs:
# the selected file path
# e.g.$HOME/.config/nvim/init.vim
#######################################
function get_git_file() {
2020-04-24 06:21:41 +00:00
local header="${1:-select tracked file}"
2020-05-07 01:40:33 +00:00
local print_opt="${2:-full}"
set_fzf_multi "$3"
2020-04-17 04:39:33 +00:00
/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" \
2020-05-10 22:52:06 +00:00
ls-files --full-name --directory "${DOTBARE_TREE}" \
2020-04-17 04:39:33 +00:00
| fzf --header="${header}" \
2020-05-10 22:52:06 +00:00
--preview "head -50 ${DOTBARE_TREE}/{}" \
2020-05-07 01:40:33 +00:00
| awk -v home="${DOTBARE_TREE}" -v print_opt="${print_opt}" '{
if (print_opt == "full") {
print home "/" $0
} else {
print $0
}
}'
2020-04-16 05:30:48 +00:00
}
#######################################
# let user select a modified file interactively
# Arguments:
# $1: the helper message to display in the fzf header
# $2: display mode of modified files.
# default: true
# all: display all modified, include staged and unstaged
# staged: display only staged files
# unstaged: display only unstaged files
2020-04-21 06:42:00 +00:00
# $3: output_format
# default: name
# name: formatted name of the file
# raw: raw file name with status
2020-04-30 23:44:50 +00:00
# $4: if exists, don't do multi selection, do single
2020-04-16 05:30:48 +00:00
# Outputs:
# the selected file path
# e.g.$HOME/.config/nvim/init.vim
#######################################
function get_modified_file() {
local header="${1:-select a modified file}"
local display_mode="${2:-all}"
2020-04-21 06:42:00 +00:00
local output_format="${3:-name}"
2020-04-24 06:00:04 +00:00
set_fzf_multi "$4"
2020-04-17 04:39:33 +00:00
/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" \
2020-05-10 22:52:06 +00:00
status --porcelain \
| awk -v display_mode="${display_mode}" '{
2020-04-17 05:04:03 +00:00
if ($0 ~ /^[A-Za-z][A-Za-z].*$/) {
print "\033[32m" substr($0, 1, 1) "\033[31m" substr($0, 2) "\033[0m"
} else if ($0 ~ /^[A-Za-z][ \t].*$/) {
if (display_mode == "all" || display_mode == "staged") {
print "\033[32m" $0 "\033[0m"
}
2020-04-17 04:39:33 +00:00
} else {
if (display_mode == "all" || display_mode == "unstaged") {
print "\033[31m" $0 "\033[0m"
}
2020-04-17 04:39:33 +00:00
}
}' \
| fzf --header="${header}" --preview "echo {} \
| awk '{print \$2}' \
| xargs -I __ /usr/bin/git --git-dir=${DOTBARE_DIR} --work-tree=${DOTBARE_TREE} \
2020-05-10 22:52:06 +00:00
diff HEAD --color=always ${DOTBARE_TREE}/__" \
2020-04-21 06:42:00 +00:00
| awk -v home="${DOTBARE_TREE}" -v format="${output_format}" '{
if (format == "name") {
print home "/" $2
} else {
print $0
}
}'
}
2020-04-30 23:44:50 +00:00
#######################################
# let user select a stash interactively
# Arguments:
# $1: the helpe message to display in header
2020-05-01 02:50:15 +00:00
# $2: if exists, don't do multi select, only allow single selection
2020-04-30 23:44:50 +00:00
# Outputs:
# the selected stash identifier
# e.g. stash@{0}
#######################################
function get_stash() {
local header="${1:-select a stash}"
2020-05-01 02:50:15 +00:00
set_fzf_multi "$2"
2020-04-30 23:44:50 +00:00
/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" \
2020-05-10 22:52:06 +00:00
stash list \
2020-05-01 02:50:15 +00:00
| fzf --header="${header}" --preview "echo {} \
2020-04-30 23:44:50 +00:00
| awk '{
gsub(/:/, \"\", \$1)
print \$1
}' \
| xargs -I __ /usr/bin/git --git-dir=${DOTBARE_DIR} --work-tree=${DOTBARE_TREE} \
2020-05-10 22:52:06 +00:00
show -p __ --color=always" \
2020-04-30 23:44:50 +00:00
| awk '{
gsub(/:/, "", $1)
print $1
}'
}