簡體   English   中英

如何獲取有關我的 git 本地存儲庫的每個分支的信息

[英]How do I get the information about each branch of my git local repository

這是git branch -v在我的一個本地目錄中產生的

* develop e229f7a Merge branch 'develop' of https://github.com/me/myremote-repo.git into develop Some diffs
  master  3343dea [behind 2] Added git-log, same as git_log without colorization

我正在嘗試通過該命令的分支集和 output 以及 output 包含 [ahead n] 或 [behind m],

[behind 2] --> bin [master] 3343dea
[ahead 1]  --> bin [develop] 99345b

對於每個分支,如果它包含在前面或后面。

我目前在我的代碼中有以下內容:

MOD=`git branch -v | perl -wlne '/^..(\S+)\s+([a-f0-9]+)\s+(\[ahead\s+\d+\]|\[behind\s+\d+\])/ or next; print  "$3 --> $ENV{reponame} [$1] $2"; '`;

    [ ! -z "$MOD" ] &&  MOD="$MOD" | ok=false
    if $ok; then
        echo " OK --> $reponame [$br]"
    else
       # a series of UGLY HACKs to get pretty-printing
       MOD=`echo "$MOD" | tr -d '\011\012\015' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'` | sed -e 's/]#/]\
 #/'
       if [ ! -z "$MOD" ]; then
          echo -e " $MOD" && continue
       fi
    fi


 bin [develop] --> Out of sync with origin/HEAD at /Users/me/bin
 bin [develop] --> Changes to be staged/committed at /Users/me/bin
 [behind 2] --> bin [master] 3343dea
 OK --> patti [develop]
 OK --> notes [master]
 OK --> indecks [develop]
 OK --> queue [develop]
 OK --> frameworks [develop]
 OK --> nodejs [master]
 OK --> perl-forth [develop]
 OK --> patti [master]
 OK --> blog [develop]

問題是我只得到第一個。 如果有兩個分支,無論是前面還是后面,我只會得到第一個。 我嘗試了幾種方法從 git 分支 -v 獲取 output 到一個數組中,然后遍歷數組,如果它與模式匹配,則進行打印。 問題是我找不到將 output 以有效的方式放入陣列的方法。 然后當我遍歷數組時,我得到了所有文件的列表。

我用:

branches={`git branch -v`)
for i in "${branches[@]}"
do
    echo "$i"
done

我得到的是目錄中所有文件的列表!

為什么,我應該怎么做?

while循環中使用readperl中的行一一讀取到MOD中。

git branch -v |
perl -wlne'
   print "$3 --> $ENV{reponame} [$1] $2"
      if /^..(\S+)\s+([a-f0-9]+)\s+(\[(?:ahead|behind)\s+\d+\])/
' |
while IFS= read -r MOD; do
   printf '{%s}\n' "$MOD"  # Replace with code that uses $MOD
done

我認為您可以通過指定git 分支 Z78E6221F633983CE4的格式來獲得大多數方式,而無需perlawk 請注意$REPONAME是由 shell 插值的,因為我使用的是雙引號字符串。 %(...)中的內容是git格式的一部分:

$ export REPONAME=bin
$ git branch --format "%(upstream:track) --> $REPONAME [%(refname:short)] %(objectname:short=6)" | grep '^\['
[behind 15] --> bin [master] 5b1e08

我沒有很努力地擺脫分支是最新的行,所以我選擇了grep

$ git branch --format "%(upstream:track) --> $REPONAME [%(refname:short)] %(objectname:short=6)"
  --> bin [a] 5b1e08
  --> bin [briandfoy/how-to-install-perl] 1d7456
[behind 15] --> bin [master] 5b1e08

您可以添加邏輯,但我沒有達到不輸出空行的地步:

$ git branch --format "%(if)%(upstream:track)%(then)%(upstream:track) --> $REPONAME [%(refname:short)] %(objectname:short=6)%(else)%(end)"


[behind 15] --> bin [master] 5b1e08

但是,我認為在您對 ikegami 的評論中,您希望擁有最新的分支“OK”。 修改if-then-else以僅覆蓋第一列並始終覆蓋 output 和 rest:

$ git branch --format "%(if)%(upstream:track)%(then)%(upstream:track)%(else)OK%(end) --> $REPONAME [%(refname:short)] %(objectname:short=6)"
OK --> bin [a] 5b1e08
OK --> bin [briandfoy/some_feature] 1d7456
[behind 15] --> bin [master] 5b1e08

如果您想要一些其他字符串用於不同步分支,只需更改if-then-else的該分支:

$ git branch --format "%(if)%(upstream:track)%(then)Out of sync%(else)OK%(end) --> $REPONAME [%(refname:short)] %(objectname:short=6)"
OK --> bin [a] 5b1e08
OK --> bin [briandfoy/some_feature] 1d7456
Out of sync --> bin [master] 5b1e08

您可以將整個格式放入它自己的變量中,但您需要小心,因為它會插入$REPONAME 您必須為每個 repo 設置$FORMAT 我認為這更容易消化:

$ export FORMAT="%(if)%(upstream:track)%(then)%(upstream:track)%(else)OK%(end) --> $REPONAME [%(refname:short)] %(objectname:short=6)"
$ git branch --format "$FORMAT"

暫無
暫無

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

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