2012-01-04 13 views
21

C'è un comando esistente o qualche trucco o script che mi permette di mostrare lo stato dei file mostrati in "ls"?"ls" migliorato con informazioni sullo stato git?

Qualcosa di simile a quanto segue:

$ git ls status #Command could be anything `lsg` is fine too, whatever. 

app   contents modified 
autotest  up-to-date 
config  up-to-date 
config.ru  staged 
db   contents modified 
doc   contents modified 
Gemfile  modified 
Gemfile.lock modified 
lib   up-to-date 
log   up-to-date 
public  up-to-date 
Rakefile  up-to-date 
README  up-to-date 
script  up-to-date 
spec   up-to-date 
tmp   up-to-date 
vendor  contents modidified 
test.tmp  removed 

In ogni modo: avere le informazioni di stato git disponibili in un elenco di directory.

+5

Cosa vorresti discutere è il vantaggio di questo formato su 'git status'? – Nate

+1

@Nate: offre un birdseye migliore, IMO. Particolarmente utile se molti file sono stati modificati. Ma anche vedere le modifiche nel contesto dell'intera directory-dir è utile. – berkes

+1

Il più vicino è probabilmente 'git status -s', ma non riporta altro che le modifiche – fge

risposta

10

Utilizzando lo stato Git short format, ecco uno script Bash che utilizza Awk e il comando column per fornire output di stato personalizzato.

#!/bin/bash 
git status --porcelain | \ 
    awk 'BEGIN {FS=" "} 
{ 
    xstat = substr($0, 1, 1); 
    ystat = substr($0, 2, 1); 
    f = substr($0, 4); 
    ri = index(f, " -> "); 
    if (ri > 0) f = substr(f, 1, ri); 
    if (xstat == " " && ystat ~ "M|D") stat = "not updated"; 
    else if (xstat == "M" && ystat ~ " |M|D") stat = "updated in index"; 
    else if (xstat == "A" && ystat ~ " |M|D") stat = "added to index"; 
    else if (xstat == "D" && ystat ~ " |M") stat = "deleted from index"; 
    else if (xstat == "R" && ystat ~ " |M|D") stat = "renamed in index"; 
    else if (xstat == "C" && ystat ~ " |M|D") stat = "copied in index"; 
    else if (xstat ~ "M|A|R|C" && ystat == " ") stat = "index and work tree matches"; 
    else if (xstat ~ " |M|A|R|C" && ystat == "M") stat = "work tree changed since index"; 
    else if (xstat ~ " |M|A|R|C" && ystat == "D") stat = "deleted in work tree"; 
    else if (xstat == "D" && ystat == "D") stat = "unmerged, both deleted"; 
    else if (xstat == "A" && ystat == "U") stat = "unmerged, added by us"; 
    else if (xstat == "U" && ystat == "D") stat = "unmerged, deleted by them"; 
    else if (xstat == "U" && ystat == "A") stat = "unmerged, added by them"; 
    else if (xstat == "D" && ystat == "U") stat = "unmerged, deleted by us"; 
    else if (xstat == "A" && ystat == "A") stat = "unmerged, both added"; 
    else if (xstat == "U" && ystat == "U") stat = "unmerged, both modified"; 
    else if (xstat == "?" && ystat == "?") stat = "untracked"; 
    else if (xstat == "!" && ystat == "!") stat = "ignored"; 
    else stat = "unknown status"; 
    print f " " stat; 
}' | \ 
    column -t -s " " 

Se si crea un eseguibile git-status-ls in una directory sul PATH ($HOME/bin dovrebbe essere un buon posto), è possibile digitare git status-ls in qualsiasi repo Git. Oppure potresti creare un one-liner Git alias per questo. Potresti anche implementarlo usando Perl, Python, C o qualsiasi altra lingua tu stia meglio.


Ecco un esempio di output:

B        renamed in index 
A        untracked 
dont_delete_git_pre-commit_hook untracked 

appena realizzato, le schede sono la visualizzazione come spazi. Nello script Awk print f " " stat; e nel comando column -t -s " ", c'è una scheda (non spazi) tra le virgolette. È possibile utilizzare un separatore diverso da tab.


Notato un problema con i flag di stato che gestiscono nello script precedente e corretto.

+0

Questo non mostrerà il "contesto" di file non modificati. Ma immagino di poter iniziare con questo per aggiungere anche tutti gli altri file. – berkes

+2

Così com'è, questo script ti dirà semplicemente lo stato di ciò che sarebbe/potrebbe essere eseguito. Sembrerebbe che elencare i file tracciati non modificati richiederà un po 'di lavoro; Non riesco a trovare una soluzione rapida. –

-1

Questo dovrebbe iniziare:

$ (git ls-files -o|sed -e 's/$/ untracked/'; \ 
    git ls-files -m|sed -e 's/$/contents modified/') | 
    sort 

Vedere git help ls-files per altre bandiere che è possibile utilizzare.

Si potrebbe desiderare di utilizzare la shell builtin printf per allineare l'uscita il modo in cui lo avete nel vostro esempio:

$ (git ls-files -o|sed -e 's/$/ untracked/'; \ 
git ls-files -m|sed -e 's/$/ contents modified/') | 
    sort | 
    while read file stat 
    do 
     printf "%-30s%-20s\n" $file $stat 
    done 
+0

Non riesco a vedere come git ls-files possono essere utilizzati per riportare solo sul contenuto delle directory; cioè non mostrare tutte le informazioni ricorsive – berkes

Problemi correlati