Git mi avvisa se un ID commit stenografia può riferirsi a 2 commit diversi?


130

Se cee157può fare riferimento a 2 diversi ID commit, come ad esempio

cee157eb799af829a9a0c42c0915f55cd29818d4 e cee1577fecf6fc5369a80bd6e926ac5f864a754b

Git mi avvertirà se scrivo git log cee157? (o Git 1.8.5.2 (Apple Git-48) mi permette di digitare git log cee1).

Penso che dovrebbe, anche se non riesco a trovare alcuna fonte autorevole che lo dica.


4
Vedi man gitrevisions, che almeno implica che verrà dato un avvertimento poiché afferma che puoi nominare una revisione con il suo nome SHA1-1 completo o "una sottostringa iniziale unica nel repository".
Chepner,

5
hai 17 commit diversi? basta provare git log c... e vedere.
Djechlin,

1
In ELL, probabilmente lo contrassegnerei come [riferimento generale]
solo

3
@djechlin Ho bisogno di almeno 4 cifre. git log abcdice fatal: ambiguous argument 'abc': unknown revision or path not in the working tree.anche se ho un SHA1 unico a partire da abc. Non funziona con 1-2-3 cifre, 4 sembra essere il minimo. Testato su Windows (1.8.1) e Mac (1.9.1).
Janos,

4
@janos Questo perché environment.h definisce minimum_abbrevun valore di 4.
Devnull

Risposte:


168

Dovrebbe darti qualcosa del genere:

$ git log cee157
error: short SHA1 cee157 is ambiguous.
error: short SHA1 cee157 is ambiguous.
fatal: ambiguous argument 'cee157': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

Ho appena provato questo su un vero repository Git, trovando commit con prefissi duplicati come questo:

git rev-list master | cut -c-4 | sort | uniq -c | sort -nr | head

Questo prende l'elenco delle revisioni master, ritaglia i primi 4 caratteri e butta via il resto, conta i duplicati e ordina numericamente. In un mio repository relativamente piccolo di ~ 1500 commit ho trovato alcune revisioni con un prefisso comune a 4 cifre. Ho scelto un prefisso di 4 cifre perché sembra essere la lunghezza legale più breve supportata da Git. (Non funziona con 3 cifre o meno, anche se non ambiguo.)

A proposito, questo non era un errore di battitura, non so perché il messaggio di errore su SHA1 ambiguo appare due volte, indipendentemente dal numero di SHA1 duplicato (provato con 2 e 3):

error: short SHA1 cee157 is ambiguous.
error: short SHA1 cee157 is ambiguous.

(Entrambi stderrattivi. In realtà l'intero output è attivo stderr, niente attivo stdout.)

Testato su Windows:

$ git --version
git version 1.8.1.msysgit.1

Penso che sia sicuro dire che se la tua versione è> = 1.8.1, Git ti avvertirà dei duplicati. (Rifiuterà di operare con i duplicati.) Immagino che anche versioni molto più vecchie funzionassero in questo modo.

AGGIORNARE

Quando si verifica questo, è necessario un minimo di 4 cifre SHA1, a causa di int minimum_abbrev = 4in environment.c . (Grazie @devnull per averlo sottolineato!)


5
L'errore appare due volte anche quando ci sono più di due commit con prefissi corrispondenti?
Nit

4
@Non sì, anche quando ci sono 3 duplicati, il messaggio appare due volte. Aggiornato la mia risposta per chiarirlo.
Janos,

1
Data la struttura del codice sorgente git, sembra che uno dei due output sia un avvertimento e l'altro un errore. Non certo, però.
Izkata,

1
@MarkHurd sia su stderr. In realtà l'intero output è su stderr, niente su stdout. (che ha un senso)
Janos

63

Il poster originale afferma:

Penso che dovrebbe, anche se non riesco a trovare alcuna fonte autorevole che lo dica.

La fonte autorevole può essere trovato nel codice sorgente, get_short_sha1() .

Citando questo :

if (!quietly && (status == SHORT_NAME_AMBIGUOUS))
    return error("short SHA1 %.*s is ambiguous.", len, hex_pfx);

e questo :

if (!ds->candidate_checked)
    /*
     * If this is the only candidate, there is no point
     * calling the disambiguation hint callback.
     *
     * On the other hand, if the current candidate
     * replaced an earlier candidate that did _not_ pass
     * the disambiguation hint callback, then we do have
     * more than one objects that match the short name
     * given, so we should make sure this one matches;
     * otherwise, if we discovered this one and the one
     * that we previously discarded in the reverse order,
     * we would end up showing different results in the
     * same repository!
     */
    ds->candidate_ok = (!ds->disambiguate_fn_used ||
                        ds->fn(ds->candidate, ds->cb_data));

if (!ds->candidate_ok)
    return SHORT_NAME_AMBIGUOUS;

Inoltre, esistono anche test per garantire che la funzionalità funzioni come previsto.

Utilizzando il nostro sito, riconosci di aver letto e compreso le nostre Informativa sui cookie e Informativa sulla privacy.
Licensed under cc by-sa 3.0 with attribution required.