Skip to content

Commit

Permalink
strbuf: convert strbuf_add_unique_abbrev to use struct object_id
Browse files Browse the repository at this point in the history
Convert the declaration and definition of strbuf_add_unique_abbrev to
make it take a pointer to struct object_id.  Predeclare the struct in
strbuf.h, as cache.h includes strbuf.h before it declares the struct,
and otherwise the struct declaration would have the wrong scope.

Apply the following semantic patch, along with the standard object_id
transforms, to adjust the callers:

@@
expression E1, E2, E3;
@@
- strbuf_add_unique_abbrev(E1, E2.hash, E3);
+ strbuf_add_unique_abbrev(E1, &E2, E3);

@@
expression E1, E2, E3;
@@
- strbuf_add_unique_abbrev(E1, E2->hash, E3);
+ strbuf_add_unique_abbrev(E1, E2, E3);

Signed-off-by: brian m. carlson <[email protected]>
Signed-off-by: Junio C Hamano <[email protected]>
  • Loading branch information
bk2204 authored and gitster committed Mar 14, 2018
1 parent 1776979 commit 30e677e
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 21 deletions.
2 changes: 1 addition & 1 deletion builtin/checkout.c
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ static int add_pending_uninteresting_ref(const char *refname,
static void describe_one_orphan(struct strbuf *sb, struct commit *commit)
{
strbuf_addstr(sb, " ");
strbuf_add_unique_abbrev(sb, commit->object.oid.hash, DEFAULT_ABBREV);
strbuf_add_unique_abbrev(sb, &commit->object.oid, DEFAULT_ABBREV);
strbuf_addch(sb, ' ');
if (!parse_commit(commit))
pp_commit_easy(CMIT_FMT_ONELINE, commit, sb);
Expand Down
8 changes: 4 additions & 4 deletions builtin/fetch.c
Original file line number Diff line number Diff line change
Expand Up @@ -708,9 +708,9 @@ static int update_local_ref(struct ref *ref,
if (in_merge_bases(current, updated)) {
struct strbuf quickref = STRBUF_INIT;
int r;
strbuf_add_unique_abbrev(&quickref, current->object.oid.hash, DEFAULT_ABBREV);
strbuf_add_unique_abbrev(&quickref, &current->object.oid, DEFAULT_ABBREV);
strbuf_addstr(&quickref, "..");
strbuf_add_unique_abbrev(&quickref, ref->new_oid.hash, DEFAULT_ABBREV);
strbuf_add_unique_abbrev(&quickref, &ref->new_oid, DEFAULT_ABBREV);
if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
(recurse_submodules != RECURSE_SUBMODULES_ON))
check_for_new_submodule_commits(&ref->new_oid);
Expand All @@ -723,9 +723,9 @@ static int update_local_ref(struct ref *ref,
} else if (force || ref->force) {
struct strbuf quickref = STRBUF_INIT;
int r;
strbuf_add_unique_abbrev(&quickref, current->object.oid.hash, DEFAULT_ABBREV);
strbuf_add_unique_abbrev(&quickref, &current->object.oid, DEFAULT_ABBREV);
strbuf_addstr(&quickref, "...");
strbuf_add_unique_abbrev(&quickref, ref->new_oid.hash, DEFAULT_ABBREV);
strbuf_add_unique_abbrev(&quickref, &ref->new_oid, DEFAULT_ABBREV);
if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
(recurse_submodules != RECURSE_SUBMODULES_ON))
check_for_new_submodule_commits(&ref->new_oid);
Expand Down
2 changes: 1 addition & 1 deletion builtin/tag.c
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ static void create_reflog_msg(const struct object_id *oid, struct strbuf *sb)
strbuf_addstr(sb, rla);
} else {
strbuf_addstr(sb, "tag: tagging ");
strbuf_add_unique_abbrev(sb, oid->hash, DEFAULT_ABBREV);
strbuf_add_unique_abbrev(sb, oid, DEFAULT_ABBREV);
}

strbuf_addstr(sb, " (");
Expand Down
2 changes: 1 addition & 1 deletion merge-recursive.c
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ static void output_commit_title(struct merge_options *o, struct commit *commit)
strbuf_addf(&o->obuf, "virtual %s\n",
merge_remote_util(commit)->name);
else {
strbuf_add_unique_abbrev(&o->obuf, commit->object.oid.hash,
strbuf_add_unique_abbrev(&o->obuf, &commit->object.oid,
DEFAULT_ABBREV);
strbuf_addch(&o->obuf, ' ');
if (parse_commit(commit) != 0)
Expand Down
8 changes: 4 additions & 4 deletions pretty.c
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ static void add_merge_info(const struct pretty_print_context *pp,
struct object_id *oidp = &parent->item->object.oid;
strbuf_addch(sb, ' ');
if (pp->abbrev)
strbuf_add_unique_abbrev(sb, oidp->hash, pp->abbrev);
strbuf_add_unique_abbrev(sb, oidp, pp->abbrev);
else
strbuf_addstr(sb, oid_to_hex(oidp));
parent = parent->next;
Expand Down Expand Up @@ -1156,15 +1156,15 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
return 1;
case 'h': /* abbreviated commit hash */
strbuf_addstr(sb, diff_get_color(c->auto_color, DIFF_COMMIT));
strbuf_add_unique_abbrev(sb, commit->object.oid.hash,
strbuf_add_unique_abbrev(sb, &commit->object.oid,
c->pretty_ctx->abbrev);
strbuf_addstr(sb, diff_get_color(c->auto_color, DIFF_RESET));
return 1;
case 'T': /* tree hash */
strbuf_addstr(sb, oid_to_hex(&commit->tree->object.oid));
return 1;
case 't': /* abbreviated tree hash */
strbuf_add_unique_abbrev(sb, commit->tree->object.oid.hash,
strbuf_add_unique_abbrev(sb, &commit->tree->object.oid,
c->pretty_ctx->abbrev);
return 1;
case 'P': /* parent hashes */
Expand All @@ -1178,7 +1178,7 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
for (p = commit->parents; p; p = p->next) {
if (p != commit->parents)
strbuf_addch(sb, ' ');
strbuf_add_unique_abbrev(sb, p->item->object.oid.hash,
strbuf_add_unique_abbrev(sb, &p->item->object.oid,
c->pretty_ctx->abbrev);
}
return 1;
Expand Down
4 changes: 2 additions & 2 deletions strbuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -873,12 +873,12 @@ void strbuf_addftime(struct strbuf *sb, const char *fmt, const struct tm *tm,
strbuf_setlen(sb, sb->len + len);
}

void strbuf_add_unique_abbrev(struct strbuf *sb, const unsigned char *sha1,
void strbuf_add_unique_abbrev(struct strbuf *sb, const struct object_id *oid,
int abbrev_len)
{
int r;
strbuf_grow(sb, GIT_SHA1_HEXSZ + 1);
r = find_unique_abbrev_r(sb->buf + sb->len, sha1, abbrev_len);
r = find_unique_abbrev_r(sb->buf + sb->len, oid->hash, abbrev_len);
strbuf_setlen(sb, sb->len + r);
}

Expand Down
8 changes: 7 additions & 1 deletion strbuf.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ struct strbuf {
extern char strbuf_slopbuf[];
#define STRBUF_INIT { .alloc = 0, .len = 0, .buf = strbuf_slopbuf }

/*
* Predeclare this here, since cache.h includes this file before it defines the
* struct.
*/
struct object_id;

/**
* Life Cycle Functions
* --------------------
Expand Down Expand Up @@ -539,7 +545,7 @@ extern void strbuf_list_free(struct strbuf **);
* the strbuf `sb`.
*/
extern void strbuf_add_unique_abbrev(struct strbuf *sb,
const unsigned char *sha1,
const struct object_id *oid,
int abbrev_len);

/**
Expand Down
4 changes: 2 additions & 2 deletions submodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -540,9 +540,9 @@ static void show_submodule_header(struct diff_options *o, const char *path,

output_header:
strbuf_addf(&sb, "Submodule %s ", path);
strbuf_add_unique_abbrev(&sb, one->hash, DEFAULT_ABBREV);
strbuf_add_unique_abbrev(&sb, one, DEFAULT_ABBREV);
strbuf_addstr(&sb, (fast_backward || fast_forward) ? ".." : "...");
strbuf_add_unique_abbrev(&sb, two->hash, DEFAULT_ABBREV);
strbuf_add_unique_abbrev(&sb, two, DEFAULT_ABBREV);
if (message)
strbuf_addf(&sb, " %s\n", message);
else
Expand Down
4 changes: 2 additions & 2 deletions transport.c
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ static void print_ok_ref_status(struct ref *ref, int porcelain, int summary_widt
char type;
const char *msg;

strbuf_add_unique_abbrev(&quickref, ref->old_oid.hash,
strbuf_add_unique_abbrev(&quickref, &ref->old_oid,
DEFAULT_ABBREV);
if (ref->forced_update) {
strbuf_addstr(&quickref, "...");
Expand All @@ -378,7 +378,7 @@ static void print_ok_ref_status(struct ref *ref, int porcelain, int summary_widt
type = ' ';
msg = NULL;
}
strbuf_add_unique_abbrev(&quickref, ref->new_oid.hash,
strbuf_add_unique_abbrev(&quickref, &ref->new_oid,
DEFAULT_ABBREV);

print_ref_status(type, quickref.buf, ref, ref->peer_ref, msg,
Expand Down
6 changes: 3 additions & 3 deletions wt-status.c
Original file line number Diff line number Diff line change
Expand Up @@ -1188,7 +1188,7 @@ static void abbrev_sha1_in_line(struct strbuf *line)
strbuf_trim(split[1]);
if (!get_oid(split[1]->buf, &oid)) {
strbuf_reset(split[1]);
strbuf_add_unique_abbrev(split[1], oid.hash,
strbuf_add_unique_abbrev(split[1], &oid,
DEFAULT_ABBREV);
strbuf_addch(split[1], ' ');
strbuf_reset(line);
Expand Down Expand Up @@ -1422,7 +1422,7 @@ static char *get_branch(const struct worktree *wt, const char *path)
;
else if (!get_oid_hex(sb.buf, &oid)) {
strbuf_reset(&sb);
strbuf_add_unique_abbrev(&sb, oid.hash, DEFAULT_ABBREV);
strbuf_add_unique_abbrev(&sb, &oid, DEFAULT_ABBREV);
} else if (!strcmp(sb.buf, "detached HEAD")) /* rebase */
goto got_nothing;
else /* bisect */
Expand Down Expand Up @@ -1459,7 +1459,7 @@ static int grab_1st_switch(struct object_id *ooid, struct object_id *noid,
if (!strcmp(cb->buf.buf, "HEAD")) {
/* HEAD is relative. Resolve it to the right reflog entry. */
strbuf_reset(&cb->buf);
strbuf_add_unique_abbrev(&cb->buf, noid->hash, DEFAULT_ABBREV);
strbuf_add_unique_abbrev(&cb->buf, noid, DEFAULT_ABBREV);
}
return 1;
}
Expand Down

0 comments on commit 30e677e

Please sign in to comment.