2020-04-14 08:36:33 +00:00
|
|
|
#!/bin/bash
|
|
|
|
#
|
|
|
|
# helper functions to obtain git related information
|
|
|
|
|
|
|
|
#######################################
|
|
|
|
# let user select a commit interactively
|
2020-04-16 05:04:34 +00:00
|
|
|
# Arguments:
|
|
|
|
# $1: the helper message to display in the fzf header
|
2020-04-17 07:21:16 +00:00
|
|
|
# $2: files to show diff against HEAD
|
2020-04-14 08:36:33 +00:00
|
|
|
# Outputs:
|
|
|
|
# the selected commit 6 char code
|
|
|
|
# e.g. b60b330
|
|
|
|
#######################################
|
|
|
|
function get_commit() {
|
2020-04-16 05:04:34 +00:00
|
|
|
local header="${1:-select a commit}"
|
2020-04-17 07:21:16 +00:00
|
|
|
local files="$2"
|
|
|
|
if [[ -z "${files}" ]]; then
|
|
|
|
/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" \
|
|
|
|
log --oneline --color=always --decorate=short \
|
|
|
|
| fzf --no-multi --header="${header}" \
|
2020-04-17 07:23:39 +00:00
|
|
|
--preview "echo {} \
|
|
|
|
| awk '{print \$1}' \
|
|
|
|
| xargs -I __ /usr/bin/git --git-dir=${DOTBARE_DIR} --work-tree=${DOTBARE_TREE} \
|
|
|
|
show --color=always __" \
|
2020-04-17 07:21:16 +00:00
|
|
|
| awk '{print $1}'
|
|
|
|
else
|
|
|
|
/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" \
|
|
|
|
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} \
|
|
|
|
diff --color=always __ $files" \
|
|
|
|
| awk '{print $1}'
|
|
|
|
fi
|
2020-04-16 05:04:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#######################################
|
|
|
|
# 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}" \
|
|
|
|
--preview="/usr/bin/git --git-dir=${DOTBARE_DIR} --work-tree=${DOTBARE_TREE} \
|
|
|
|
log --oneline --graph --color=always --decorate=short {}"
|
2020-04-14 08:36:33 +00:00
|
|
|
}
|
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
|
|
|
|
# $2: if exist, don't do multi selection, do single
|
|
|
|
# Outputs:
|
|
|
|
# the selected file path
|
|
|
|
# e.g.$HOME/.config/nvim/init.vim
|
|
|
|
#######################################
|
|
|
|
function get_git_file() {
|
|
|
|
local header="${1:-select a file}"
|
2020-04-17 05:03:49 +00:00
|
|
|
set_fzf_multi "$2"
|
2020-04-17 04:39:33 +00:00
|
|
|
/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" \
|
|
|
|
ls-files --full-name --directory "${DOTBARE_TREE}" \
|
|
|
|
| fzf --header="${header}" \
|
|
|
|
--preview "head -50 ${DOTBARE_TREE}/{}" \
|
|
|
|
| awk -v home="${DOTBARE_TREE}" '{print home "/" $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
|
2020-04-21 04:50:51 +00:00
|
|
|
# $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
|
|
|
|
# $3: 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}"
|
2020-04-21 04:50:51 +00:00
|
|
|
local display_mode="${2:-all}"
|
|
|
|
set_fzf_multi "$3"
|
2020-04-17 04:39:33 +00:00
|
|
|
/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" \
|
2020-04-21 04:50:51 +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].*$/) {
|
2020-04-21 04:50:51 +00:00
|
|
|
if (display_mode == "all" || display_mode == "staged") {
|
|
|
|
print "\033[32m" $0 "\033[0m"
|
|
|
|
}
|
2020-04-17 04:39:33 +00:00
|
|
|
} else {
|
2020-04-21 04:50:51 +00:00
|
|
|
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-04-21 06:07:12 +00:00
|
|
|
diff HEAD --color=always ${DOTBARE_TREE}/__" \
|
2020-04-17 05:03:49 +00:00
|
|
|
| awk -v home="${DOTBARE_TREE}" '{print home "/" $2}'
|
|
|
|
}
|