我正在使用一个shell脚本,它是使用Whiptail(shell接口)的Git接口。
有我的问题,我想创建一个菜单,我们可以选择Git存储库的分支并将此字符串返回给脚本。
我知道如何使用以下命令获取Git分支的不同名称:
git branch
但我不知道如何解析它们以便将它们传达给Whiptail。
我想计算分支的数量,并逐个作为每个分支的选项进行通信。
例如:
$> git branch
master
asm
release
debug
我想在shell脚本中使用Whiptail:
Here the number of branch
v
whiptail --tittle "Branch select" --menu "Choose your branch" 20 60 4 \
"1" "master" \
"2" "asm" \
"3" "release" \
"4" "debug"
但是我不知道如何解析git branch
的输出以将其作为shell脚本,请问我可以得到帮助吗?
编辑:我终于做到了:) Source code
答案 0 :(得分:2)
我不知道whiptail
是什么,但不久前我写了一个bash函数来列出你所访问的所有最近的分支,并给用户一个菜单来选择一个。< / p>
我在此处复制的source is in my .bashrc
on Github:
# Colors for prompt
COLOR_RED=$(tput sgr0 && tput setaf 1)
COLOR_GREEN=$(tput sgr0 && tput setaf 2)
COLOR_YELLOW=$(tput sgr0 && tput setaf 3)
COLOR_DARK_BLUE=$(tput sgr0 && tput setaf 4)
COLOR_BLUE=$(tput sgr0 && tput setaf 6)
COLOR_PURPLE=$(tput sgr0 && tput setaf 5)
COLOR_PINK=$(tput sgr0 && tput bold && tput setaf 5)
COLOR_LIGHT_GREEN=$(tput sgr0 && tput bold && tput setaf 2)
COLOR_LIGHT_RED=$(tput sgr0 && tput bold && tput setaf 1)
COLOR_LIGHT_CYAN=$(tput sgr0 && tput bold && tput setaf 6)
COLOR_RESET=$(tput sgr0)
# git
function _c() {
cur=${COMP_WORDS[COMP_CWORD]}
branches=`git for-each-ref --sort=-committerdate refs/heads/ | head -n 10`
output=''
for branch in $branches
do
output+=`echo "$branch" | sed 's/.*refs\/heads\///'`
# creative way to get color here? echo messes everything up
# (http://unix.stackexchange.com/questions/107417/what-are-the-special-characters-to-print-from-a-script-to-move-the-cursor ?)
#output+=" '`git show --quiet $(echo $branch | cut -d' ' -f1) --pretty=format:"%C(Yellow)%h %Cred<%an>%Creset %s %C(cyan)(%cr)%Creset'"`"$'\n'
#echo " \'`git show --quiet $(echo $branch | cut -d' ' -f1) --pretty=format:"%C(Yellow)%h %Cred<%an>%Creset %s %C(cyan)(%cr)%Creset\'"`"$'\n'
output+=" \'`git show --quiet $(echo $branch | cut -d' ' -f1) --pretty=format:"%h <%an> %s (%cr)\'"`"$'\n'
done
response=''
for branch in $output
do
lowerBranch=`echo $branch | tr '[:upper:]' '[:lower:]'`
if [[ $branch =~ .*$cur.* ]]; then
response+=$branch$'\n'
fi
done
COMPREPLY=( $( compgen -W "$response" -- $cur ) )
}
function c() {
newBranch=""
inputted=""
if [[ -z "$1" ]]; then
branchOutput=`git for-each-ref --sort=-committerdate refs/heads/ | head -n 10`
declare -a branches
let xx=0
IFS=$'\n'
pad=$(printf '%0.1s' " "{1..32})
padlength=32
for branch in $branchOutput
do
# Show them in a list with a counter
xx=`expr $xx + 1`
branches=("${branches[@]}" "$branch")
branchName=`echo "$branch" | sed 's/.*refs\/heads\///'`
string1="$COLOR_PURPLE$xx. $COLOR_PINK $branchName"
uncolor="$xx. $branchName"
printf '%s' $string1
printf '%*.*s' 0 $((padlength - ${#uncolor} )) "$pad"
printf '%s\n' `git show --quiet $branchName --pretty=format:"%C(Yellow)%h %Cred<%an>%Creset %s %C(cyan)(%cr)%Creset"`
done
# Prompt user for file. -n means no line break after echo
echo -n "$COLOR_YELLOW?$COLOR_RESET "
read branchNumber
let "branchNumber+=-1"
if [[ "$branchNumber" =~ ^[0-9]+$ ]]; then
newBranch=`echo "${branches[@]:$branchNumber:1}" | sed 's/.*refs\/heads\///'`
if [[ -z "$newBranch" ]]; then
echo "Not real."
return 1
fi
else
echo "Wtf?"
return 1
fi
else
inputted=1
newBranch=`echo "$1" | cut -d' ' -f1`
fi
if [[ -n "$1" ]]; then
echo `git show --quiet "$newBranch" --pretty=format:"%C(Yellow)%h %Cred<%an>%Creset %s %C(cyan)(%cr)%Creset"`
fi
if [[ $newBranch =~ ^pr ]]; then
echo -e "\ngit fetch $newBranch && git checkout $newBranch"
git fetch $newBranch && git checkout $newBranch
else
echo -e "\ngit checkout $newBranch"
git checkout $newBranch
fi
}
# add autocompletion to the c command
complete -F _c c
如果你将它放在~/.bashrc
并在命令行运行source ~/.bashrc
(或打开一个新shell),那么你可以在你的git仓库中运行命令c
,你会得到这样的输出:
或者您只需键入c
tab tab 即可自动完成分支名称:
这需要很多天才能完成,我不希望这个过程对任何人。脚本bash来制作合理的用户界面很麻烦。