簡體   English   中英

我如何通過bash腳本選擇下一個分支

[英]How I can choose next branch via bash script

我編寫了一個腳本來列出存儲庫中的所有本地分支。

#!/bin/bash
clear

branches=()
menuposition=0
eval "$(git for-each-ref --shell --format='branches+=(%(refname))' refs/heads/)"
for branch in "${branches[@]}"; do
    menuposition=$((menuposition+1))
    echo "$menuposition) $branch"
done

輸出是...

1) master
2) foo
3) bar

我可以使用讀取命令來獲取用戶輸入。 但是,...然后,我該如何檢出用戶選擇的分支?

#!/bin/bash
clear
options=()

#for bash version 4 or higher use mapfile.
#Fallback to while loop if mapfile not found
mapfile -t options < <(git for-each-ref \
--format='%(refname:short)' refs/heads/) &>/dev/null \
|| while read line; do options+=( "$line" ); done \
< <(git for-each-ref --format='%(refname:short)' refs/heads/)

options+=('Exit')

select opt in "${options[@]}"
do
    if [[ "$opt" ]] && [[ "$opt" == 'Exit' ]]; then
        echo "Bye Bye"
        exit 0
    elif [[ "$opt" ]]; then
        git checkout "$opt"
    else
        echo "Wrong Input. Please enter the correct input"
    fi
done

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM