object-name: move related functions to object-name

Move object-name-related functions from strbuf.[ch] to object-name.[ch] so that strbuf is focused on string manipulation routines with minimal dependencies. dir.h relied on the forward declration of the repository struct in strbuf.h. Since that is removed in this patch, add the forward declaration to dir.h. Signed-off-by: Calvin Wan <calvinwan@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Calvin Wan committed Jun 6, 2023 at 19:48 UTC f94018506c6103331a0183d78b206f39cf739ec3
6 files changed +27 -26
dir.h
+2
@@ -40,6 +40,8 @@
40 *
41 */
42
43 +struct repository;
44 +
45 struct dir_entry {
46 unsigned int len;
47 char name[FLEX_ARRAY]; /* more */
object-name.c
+15
@@ -768,6 +768,21 @@ static void find_abbrev_len_packed(struct min_abbrev_data *mad)
768 find_abbrev_len_for_pack(p, mad);
769 }
770
771 +void strbuf_repo_add_unique_abbrev(struct strbuf *sb, struct repository *repo,
772 + const struct object_id *oid, int abbrev_len)
773 +{
774 + int r;
775 + strbuf_grow(sb, GIT_MAX_HEXSZ + 1);
776 + r = repo_find_unique_abbrev_r(repo, sb->buf + sb->len, oid, abbrev_len);
777 + strbuf_setlen(sb, sb->len + r);
778 +}
779 +
780 +void strbuf_add_unique_abbrev(struct strbuf *sb, const struct object_id *oid,
781 + int abbrev_len)
782 +{
783 + strbuf_repo_add_unique_abbrev(sb, the_repository, oid, abbrev_len);
784 +}
785 +
786 int repo_find_unique_abbrev_r(struct repository *r, char *hex,
787 const struct object_id *oid, int len)
788 {
object-name.h
+9
@@ -40,6 +40,15 @@ struct object_context {
40 const char *repo_find_unique_abbrev(struct repository *r, const struct object_id *oid, int len);
41 int repo_find_unique_abbrev_r(struct repository *r, char *hex, const struct object_id *oid, int len);
42
43 +/**
44 + * Add the abbreviation, as generated by repo_find_unique_abbrev(), of `sha1` to
45 + * the strbuf `sb`.
46 + */
47 +void strbuf_repo_add_unique_abbrev(struct strbuf *sb, struct repository *repo,
48 + const struct object_id *oid, int abbrev_len);
49 +void strbuf_add_unique_abbrev(struct strbuf *sb, const struct object_id *oid,
50 + int abbrev_len);
51 +
52 int repo_get_oid(struct repository *r, const char *str, struct object_id *oid);
53 __attribute__((format (printf, 2, 3)))
54 int get_oidf(struct object_id *oid, const char *fmt, ...);
pretty.c
+1
@@ -18,6 +18,7 @@
18 #include "gpg-interface.h"
19 #include "trailer.h"
20 #include "run-command.h"
21 +#include "object-name.h"
22
23 /*
24 * The limit for formatting directives, which enable the caller to append
strbuf.c
-16
@@ -3,7 +3,6 @@
3 #include "environment.h"
4 #include "gettext.h"
5 #include "hex.h"
6 -#include "object-name.h"
6 #include "repository.h"
7 #include "strbuf.h"
8 #include "string-list.h"
@@ -1024,21 +1023,6 @@ void strbuf_addftime(struct strbuf *sb, const char *fmt, const struct tm *tm,
1023 strbuf_setlen(sb, sb->len + len);
1024 }
1025
1027 -void strbuf_repo_add_unique_abbrev(struct strbuf *sb, struct repository *repo,
1028 - const struct object_id *oid, int abbrev_len)
1029 -{
1030 - int r;
1031 - strbuf_grow(sb, GIT_MAX_HEXSZ + 1);
1032 - r = repo_find_unique_abbrev_r(repo, sb->buf + sb->len, oid, abbrev_len);
1033 - strbuf_setlen(sb, sb->len + r);
1034 -}
1035 -
1036 -void strbuf_add_unique_abbrev(struct strbuf *sb, const struct object_id *oid,
1037 - int abbrev_len)
1038 -{
1039 - strbuf_repo_add_unique_abbrev(sb, the_repository, oid, abbrev_len);
1040 -}
1041 -
1026 /*
1027 * Returns the length of a line, without trailing spaces.
1028 *
strbuf.h
-10
@@ -616,16 +616,6 @@ void strbuf_add_separated_string_list(struct strbuf *str,
616 */
617 void strbuf_list_free(struct strbuf **list);
618
619 -/**
620 - * Add the abbreviation, as generated by repo_find_unique_abbrev(), of `sha1` to
621 - * the strbuf `sb`.
622 - */
623 -struct repository;
624 -void strbuf_repo_add_unique_abbrev(struct strbuf *sb, struct repository *repo,
625 - const struct object_id *oid, int abbrev_len);
626 -void strbuf_add_unique_abbrev(struct strbuf *sb, const struct object_id *oid,
627 - int abbrev_len);
628 -
619 /*
620 * Remove the filename from the provided path string. If the path
621 * contains a trailing separator, then the path is considered a directory