Skip to content

Commit 781dbc1

Browse files
john-caiJohn Cai
authored andcommitted
object-name.c: refactor find_unique_abbrev_len into its own exported function
This commit makes it easier to determine the length of the hash that git log --oneline or git log --pretty will print. * object-name.c: refactor finding abbreviated sha length into its own function * cache.h: expose function find_unique_abbrev_len Signed-off-by: John Cai <johncai86@gmail.com>
1 parent e9e5ba3 commit 781dbc1

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

cache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,6 +1147,7 @@ const char *repo_find_unique_abbrev(struct repository *r, const struct object_id
11471147
#define find_unique_abbrev(oid, len) repo_find_unique_abbrev(the_repository, oid, len)
11481148
int repo_find_unique_abbrev_r(struct repository *r, char *hex, const struct object_id *oid, int len);
11491149
#define find_unique_abbrev_r(hex, oid, len) repo_find_unique_abbrev_r(the_repository, hex, oid, len)
1150+
int find_unique_abbrev_len(struct repository *r, int len);
11501151

11511152
/* set default permissions by passing mode arguments to open(2) */
11521153
int git_mkstemps_mode(char *pattern, int suffix_len, int mode);

object-name.c

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -660,14 +660,8 @@ static void find_abbrev_len_packed(struct min_abbrev_data *mad)
660660
find_abbrev_len_for_pack(p, mad);
661661
}
662662

663-
int repo_find_unique_abbrev_r(struct repository *r, char *hex,
664-
const struct object_id *oid, int len)
663+
int find_unique_abbrev_len(struct repository *r, int len)
665664
{
666-
struct disambiguate_state ds;
667-
struct min_abbrev_data mad;
668-
struct object_id oid_ret;
669-
const unsigned hexsz = r->hash_algo->hexsz;
670-
671665
if (len < 0) {
672666
unsigned long count = repo_approximate_object_count(r);
673667
/*
@@ -690,6 +684,19 @@ int repo_find_unique_abbrev_r(struct repository *r, char *hex,
690684
len = FALLBACK_DEFAULT_ABBREV;
691685
}
692686

687+
return len;
688+
}
689+
690+
int repo_find_unique_abbrev_r(struct repository *r, char *hex,
691+
const struct object_id *oid, int len)
692+
{
693+
struct disambiguate_state ds;
694+
struct min_abbrev_data mad;
695+
struct object_id oid_ret;
696+
const unsigned hexsz = r->hash_algo->hexsz;
697+
698+
len = find_unique_abbrev_len(r, len);
699+
693700
oid_to_hex_r(hex, oid);
694701
if (len == hexsz || !len)
695702
return hexsz;

0 commit comments

Comments
 (0)