Skip to content

Commit 4a2dcb1

Browse files
chooglengitster
authored andcommitted
remote: die if branch is not found in repository
In a subsequent commit, we would like external-facing functions to be able to accept "struct repository" and "struct branch" as a pair. This is useful for functions like pushremote_for_branch(), which need to take values from the remote_state and branch, even if branch == NULL. However, a caller may supply an unrelated repository and branch, which is not supported behavior. To prevent misuse, add a die_on_missing_branch() helper function that dies if a given branch is not from a given repository. Speed up the existence check by replacing the branches list with a branches_hash hashmap. Like read_config(), die_on_missing_branch() is only called from non-static functions; static functions are less prone to misuse because they have strong conventions for keeping remote_state and branch in sync. Signed-off-by: Glen Choo <[email protected]> Reviewed-by: Jonathan Tan <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 56eed34 commit 4a2dcb1

File tree

2 files changed

+70
-19
lines changed

2 files changed

+70
-19
lines changed

remote.c

Lines changed: 68 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -166,25 +166,74 @@ static void add_merge(struct branch *branch, const char *name)
166166
branch->merge_name[branch->merge_nr++] = name;
167167
}
168168

169+
struct branches_hash_key {
170+
const char *str;
171+
int len;
172+
};
173+
174+
static int branches_hash_cmp(const void *unused_cmp_data,
175+
const struct hashmap_entry *eptr,
176+
const struct hashmap_entry *entry_or_key,
177+
const void *keydata)
178+
{
179+
const struct branch *a, *b;
180+
const struct branches_hash_key *key = keydata;
181+
182+
a = container_of(eptr, const struct branch, ent);
183+
b = container_of(entry_or_key, const struct branch, ent);
184+
185+
if (key)
186+
return strncmp(a->name, key->str, key->len) ||
187+
a->name[key->len];
188+
else
189+
return strcmp(a->name, b->name);
190+
}
191+
192+
static struct branch *find_branch(struct remote_state *remote_state,
193+
const char *name, size_t len)
194+
{
195+
struct branches_hash_key lookup;
196+
struct hashmap_entry lookup_entry, *e;
197+
198+
if (!len)
199+
len = strlen(name);
200+
201+
lookup.str = name;
202+
lookup.len = len;
203+
hashmap_entry_init(&lookup_entry, memhash(name, len));
204+
205+
e = hashmap_get(&remote_state->branches_hash, &lookup_entry, &lookup);
206+
if (e)
207+
return container_of(e, struct branch, ent);
208+
209+
return NULL;
210+
}
211+
212+
static void die_on_missing_branch(struct repository *repo,
213+
struct branch *branch)
214+
{
215+
/* branch == NULL is always valid because it represents detached HEAD. */
216+
if (branch &&
217+
branch != find_branch(repo->remote_state, branch->name, 0))
218+
die("branch %s was not found in the repository", branch->name);
219+
}
220+
169221
static struct branch *make_branch(struct remote_state *remote_state,
170222
const char *name, size_t len)
171223
{
172224
struct branch *ret;
173-
int i;
174225

175-
for (i = 0; i < remote_state->branches_nr; i++) {
176-
if (!strncmp(name, remote_state->branches[i]->name, len) &&
177-
!remote_state->branches[i]->name[len])
178-
return remote_state->branches[i];
179-
}
226+
ret = find_branch(remote_state, name, len);
227+
if (ret)
228+
return ret;
180229

181-
ALLOC_GROW(remote_state->branches, remote_state->branches_nr + 1,
182-
remote_state->branches_alloc);
183230
CALLOC_ARRAY(ret, 1);
184-
remote_state->branches[remote_state->branches_nr++] = ret;
185231
ret->name = xstrndup(name, len);
186232
ret->refname = xstrfmt("refs/heads/%s", ret->name);
187233

234+
hashmap_entry_init(&ret->ent, memhash(name, len));
235+
if (hashmap_put_entry(&remote_state->branches_hash, ret, ent))
236+
BUG("hashmap_put overwrote entry after hashmap_get returned NULL");
188237
return ret;
189238
}
190239

@@ -500,6 +549,8 @@ static const char *remotes_remote_for_branch(struct remote_state *remote_state,
500549
const char *remote_for_branch(struct branch *branch, int *explicit)
501550
{
502551
read_config(the_repository);
552+
die_on_missing_branch(the_repository, branch);
553+
503554
return remotes_remote_for_branch(the_repository->remote_state, branch,
504555
explicit);
505556
}
@@ -524,6 +575,8 @@ remotes_pushremote_for_branch(struct remote_state *remote_state,
524575
const char *pushremote_for_branch(struct branch *branch, int *explicit)
525576
{
526577
read_config(the_repository);
578+
die_on_missing_branch(the_repository, branch);
579+
527580
return remotes_pushremote_for_branch(the_repository->remote_state,
528581
branch, explicit);
529582
}
@@ -534,6 +587,8 @@ static struct remote *remotes_remote_get(struct remote_state *remote_state,
534587
const char *remote_ref_for_branch(struct branch *branch, int for_push)
535588
{
536589
read_config(the_repository);
590+
die_on_missing_branch(the_repository, branch);
591+
537592
if (branch) {
538593
if (!for_push) {
539594
if (branch->merge_nr) {
@@ -1879,6 +1934,8 @@ static const char *branch_get_push_1(struct remote_state *remote_state,
18791934
const char *branch_get_push(struct branch *branch, struct strbuf *err)
18801935
{
18811936
read_config(the_repository);
1937+
die_on_missing_branch(the_repository, branch);
1938+
18821939
if (!branch)
18831940
return error_buf(err, _("HEAD does not point to a branch"));
18841941

@@ -2652,6 +2709,7 @@ struct remote_state *remote_state_new(void)
26522709
memset(r, 0, sizeof(*r));
26532710

26542711
hashmap_init(&r->remotes_hash, remotes_hash_cmp, NULL, 0);
2712+
hashmap_init(&r->branches_hash, branches_hash_cmp, NULL, 0);
26552713
return r;
26562714
}
26572715

@@ -2667,11 +2725,5 @@ void remote_state_clear(struct remote_state *remote_state)
26672725
remote_state->remotes_nr = 0;
26682726

26692727
hashmap_clear_and_free(&remote_state->remotes_hash, struct remote, ent);
2670-
2671-
for (i = 0; i < remote_state->branches_nr; i++) {
2672-
FREE_AND_NULL(remote_state->branches[i]);
2673-
}
2674-
FREE_AND_NULL(remote_state->branches);
2675-
remote_state->branches_alloc = 0;
2676-
remote_state->branches_nr = 0;
2728+
hashmap_clear_and_free(&remote_state->branches_hash, struct remote, ent);
26772729
}

remote.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@ struct remote_state {
4343
int remotes_nr;
4444
struct hashmap remotes_hash;
4545

46-
struct branch **branches;
47-
int branches_alloc;
48-
int branches_nr;
46+
struct hashmap branches_hash;
4947

5048
struct branch *current_branch;
5149
const char *pushremote_name;
@@ -292,6 +290,7 @@ int remote_find_tracking(struct remote *remote, struct refspec_item *refspec);
292290
* branch_get(name) for "refs/heads/{name}", or with branch_get(NULL) for HEAD.
293291
*/
294292
struct branch {
293+
struct hashmap_entry ent;
295294

296295
/* The short name of the branch. */
297296
const char *name;

0 commit comments

Comments
 (0)