Use ALLOC_GROW in remote.{c,h}
Signed-off-by: Daniel Barkalow <barkalow@iabervon.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
parent
cf5c51efc9
commit
2d31347ba5
118
remote.c
118
remote.c
@ -3,10 +3,12 @@
|
|||||||
#include "refs.h"
|
#include "refs.h"
|
||||||
|
|
||||||
static struct remote **remotes;
|
static struct remote **remotes;
|
||||||
static int allocated_remotes;
|
static int remotes_alloc;
|
||||||
|
static int remotes_nr;
|
||||||
|
|
||||||
static struct branch **branches;
|
static struct branch **branches;
|
||||||
static int allocated_branches;
|
static int branches_alloc;
|
||||||
|
static int branches_nr;
|
||||||
|
|
||||||
static struct branch *current_branch;
|
static struct branch *current_branch;
|
||||||
static const char *default_remote_name;
|
static const char *default_remote_name;
|
||||||
@ -16,109 +18,81 @@ static char buffer[BUF_SIZE];
|
|||||||
|
|
||||||
static void add_push_refspec(struct remote *remote, const char *ref)
|
static void add_push_refspec(struct remote *remote, const char *ref)
|
||||||
{
|
{
|
||||||
int nr = remote->push_refspec_nr + 1;
|
ALLOC_GROW(remote->push_refspec,
|
||||||
remote->push_refspec =
|
remote->push_refspec_nr + 1,
|
||||||
xrealloc(remote->push_refspec, nr * sizeof(char *));
|
remote->push_refspec_alloc);
|
||||||
remote->push_refspec[nr-1] = ref;
|
remote->push_refspec[remote->push_refspec_nr++] = ref;
|
||||||
remote->push_refspec_nr = nr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void add_fetch_refspec(struct remote *remote, const char *ref)
|
static void add_fetch_refspec(struct remote *remote, const char *ref)
|
||||||
{
|
{
|
||||||
int nr = remote->fetch_refspec_nr + 1;
|
ALLOC_GROW(remote->fetch_refspec,
|
||||||
remote->fetch_refspec =
|
remote->fetch_refspec_nr + 1,
|
||||||
xrealloc(remote->fetch_refspec, nr * sizeof(char *));
|
remote->fetch_refspec_alloc);
|
||||||
remote->fetch_refspec[nr-1] = ref;
|
remote->fetch_refspec[remote->fetch_refspec_nr++] = ref;
|
||||||
remote->fetch_refspec_nr = nr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void add_url(struct remote *remote, const char *url)
|
static void add_url(struct remote *remote, const char *url)
|
||||||
{
|
{
|
||||||
int nr = remote->url_nr + 1;
|
ALLOC_GROW(remote->url, remote->url_nr + 1, remote->url_alloc);
|
||||||
remote->url =
|
remote->url[remote->url_nr++] = url;
|
||||||
xrealloc(remote->url, nr * sizeof(char *));
|
|
||||||
remote->url[nr-1] = url;
|
|
||||||
remote->url_nr = nr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct remote *make_remote(const char *name, int len)
|
static struct remote *make_remote(const char *name, int len)
|
||||||
{
|
{
|
||||||
int i, empty = -1;
|
struct remote *ret;
|
||||||
|
int i;
|
||||||
|
|
||||||
for (i = 0; i < allocated_remotes; i++) {
|
for (i = 0; i < remotes_nr; i++) {
|
||||||
if (!remotes[i]) {
|
if (len ? (!strncmp(name, remotes[i]->name, len) &&
|
||||||
if (empty < 0)
|
!remotes[i]->name[len]) :
|
||||||
empty = i;
|
!strcmp(name, remotes[i]->name))
|
||||||
} else {
|
return remotes[i];
|
||||||
if (len ? (!strncmp(name, remotes[i]->name, len) &&
|
|
||||||
!remotes[i]->name[len]) :
|
|
||||||
!strcmp(name, remotes[i]->name))
|
|
||||||
return remotes[i];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (empty < 0) {
|
ret = xcalloc(1, sizeof(struct remote));
|
||||||
empty = allocated_remotes;
|
ALLOC_GROW(remotes, remotes_nr + 1, remotes_alloc);
|
||||||
allocated_remotes += allocated_remotes ? allocated_remotes : 1;
|
remotes[remotes_nr++] = ret;
|
||||||
remotes = xrealloc(remotes,
|
|
||||||
sizeof(*remotes) * allocated_remotes);
|
|
||||||
memset(remotes + empty, 0,
|
|
||||||
(allocated_remotes - empty) * sizeof(*remotes));
|
|
||||||
}
|
|
||||||
remotes[empty] = xcalloc(1, sizeof(struct remote));
|
|
||||||
if (len)
|
if (len)
|
||||||
remotes[empty]->name = xstrndup(name, len);
|
ret->name = xstrndup(name, len);
|
||||||
else
|
else
|
||||||
remotes[empty]->name = xstrdup(name);
|
ret->name = xstrdup(name);
|
||||||
return remotes[empty];
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void add_merge(struct branch *branch, const char *name)
|
static void add_merge(struct branch *branch, const char *name)
|
||||||
{
|
{
|
||||||
int nr = branch->merge_nr + 1;
|
ALLOC_GROW(branch->merge_name, branch->merge_nr + 1,
|
||||||
branch->merge_name =
|
branch->merge_alloc);
|
||||||
xrealloc(branch->merge_name, nr * sizeof(char *));
|
branch->merge_name[branch->merge_nr++] = name;
|
||||||
branch->merge_name[nr-1] = name;
|
|
||||||
branch->merge_nr = nr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct branch *make_branch(const char *name, int len)
|
static struct branch *make_branch(const char *name, int len)
|
||||||
{
|
{
|
||||||
int i, empty = -1;
|
struct branch *ret;
|
||||||
|
int i;
|
||||||
char *refname;
|
char *refname;
|
||||||
|
|
||||||
for (i = 0; i < allocated_branches; i++) {
|
for (i = 0; i < branches_nr; i++) {
|
||||||
if (!branches[i]) {
|
if (len ? (!strncmp(name, branches[i]->name, len) &&
|
||||||
if (empty < 0)
|
!branches[i]->name[len]) :
|
||||||
empty = i;
|
!strcmp(name, branches[i]->name))
|
||||||
} else {
|
return branches[i];
|
||||||
if (len ? (!strncmp(name, branches[i]->name, len) &&
|
|
||||||
!branches[i]->name[len]) :
|
|
||||||
!strcmp(name, branches[i]->name))
|
|
||||||
return branches[i];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (empty < 0) {
|
ALLOC_GROW(branches, branches_nr + 1, branches_alloc);
|
||||||
empty = allocated_branches;
|
ret = xcalloc(1, sizeof(struct branch));
|
||||||
allocated_branches += allocated_branches ? allocated_branches : 1;
|
branches[branches_nr++] = ret;
|
||||||
branches = xrealloc(branches,
|
|
||||||
sizeof(*branches) * allocated_branches);
|
|
||||||
memset(branches + empty, 0,
|
|
||||||
(allocated_branches - empty) * sizeof(*branches));
|
|
||||||
}
|
|
||||||
branches[empty] = xcalloc(1, sizeof(struct branch));
|
|
||||||
if (len)
|
if (len)
|
||||||
branches[empty]->name = xstrndup(name, len);
|
ret->name = xstrndup(name, len);
|
||||||
else
|
else
|
||||||
branches[empty]->name = xstrdup(name);
|
ret->name = xstrdup(name);
|
||||||
refname = malloc(strlen(name) + strlen("refs/heads/") + 1);
|
refname = malloc(strlen(name) + strlen("refs/heads/") + 1);
|
||||||
strcpy(refname, "refs/heads/");
|
strcpy(refname, "refs/heads/");
|
||||||
strcpy(refname + strlen("refs/heads/"),
|
strcpy(refname + strlen("refs/heads/"), ret->name);
|
||||||
branches[empty]->name);
|
ret->refname = refname;
|
||||||
branches[empty]->refname = refname;
|
|
||||||
|
|
||||||
return branches[empty];
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void read_remotes_file(struct remote *remote)
|
static void read_remotes_file(struct remote *remote)
|
||||||
@ -380,7 +354,7 @@ int for_each_remote(each_remote_fn fn, void *priv)
|
|||||||
{
|
{
|
||||||
int i, result = 0;
|
int i, result = 0;
|
||||||
read_config();
|
read_config();
|
||||||
for (i = 0; i < allocated_remotes && !result; i++) {
|
for (i = 0; i < remotes_nr && !result; i++) {
|
||||||
struct remote *r = remotes[i];
|
struct remote *r = remotes[i];
|
||||||
if (!r)
|
if (!r)
|
||||||
continue;
|
continue;
|
||||||
|
4
remote.h
4
remote.h
@ -6,14 +6,17 @@ struct remote {
|
|||||||
|
|
||||||
const char **url;
|
const char **url;
|
||||||
int url_nr;
|
int url_nr;
|
||||||
|
int url_alloc;
|
||||||
|
|
||||||
const char **push_refspec;
|
const char **push_refspec;
|
||||||
struct refspec *push;
|
struct refspec *push;
|
||||||
int push_refspec_nr;
|
int push_refspec_nr;
|
||||||
|
int push_refspec_alloc;
|
||||||
|
|
||||||
const char **fetch_refspec;
|
const char **fetch_refspec;
|
||||||
struct refspec *fetch;
|
struct refspec *fetch;
|
||||||
int fetch_refspec_nr;
|
int fetch_refspec_nr;
|
||||||
|
int fetch_refspec_alloc;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* -1 to never fetch tags
|
* -1 to never fetch tags
|
||||||
@ -100,6 +103,7 @@ struct branch {
|
|||||||
const char **merge_name;
|
const char **merge_name;
|
||||||
struct refspec **merge;
|
struct refspec **merge;
|
||||||
int merge_nr;
|
int merge_nr;
|
||||||
|
int merge_alloc;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct branch *branch_get(const char *name);
|
struct branch *branch_get(const char *name);
|
||||||
|
Loading…
Reference in New Issue
Block a user