Skip to content

Commit

Permalink
Merge branch 'mh/reflife'
Browse files Browse the repository at this point in the history
Define memory ownership and lifetime rules for what for-each-ref
feeds to its callbacks (in short, "you do not own it, so make a
copy if you want to keep it").

* mh/reflife: (25 commits)
  refs: document the lifetime of the args passed to each_ref_fn
  register_ref(): make a copy of the bad reference SHA-1
  exclude_existing(): set existing_refs.strdup_strings
  string_list_add_refs_by_glob(): add a comment about memory management
  string_list_add_one_ref(): rename first parameter to "refname"
  show_head_ref(): rename first parameter to "refname"
  show_head_ref(): do not shadow name of argument
  add_existing(): do not retain a reference to sha1
  do_fetch(): clean up existing_refs before exiting
  do_fetch(): reduce scope of peer_item
  object_array_entry: fix memory handling of the name field
  find_first_merges(): remove unnecessary code
  find_first_merges(): initialize merges variable using initializer
  fsck: don't put a void*-shaped peg in a char*-shaped hole
  object_array_remove_duplicates(): rewrite to reduce copying
  revision: use object_array_filter() in implementation of gc_boundary()
  object_array: add function object_array_filter()
  revision: split some overly-long lines
  cmd_diff(): make it obvious which cases are exclusive of each other
  cmd_diff(): rename local variable "list" -> "entry"
  ...
  • Loading branch information
Junio C Hamano committed Jun 14, 2013
2 parents b27a79d + 4f78c24 commit ede63a1
Show file tree
Hide file tree
Showing 15 changed files with 229 additions and 121 deletions.
5 changes: 3 additions & 2 deletions bisect.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
static struct sha1_array good_revs;
static struct sha1_array skipped_revs;

static const unsigned char *current_bad_sha1;
static unsigned char *current_bad_sha1;

static const char *argv_checkout[] = {"checkout", "-q", NULL, "--", NULL};
static const char *argv_show_branch[] = {"show-branch", NULL, NULL};
Expand Down Expand Up @@ -404,7 +404,8 @@ static int register_ref(const char *refname, const unsigned char *sha1,
int flags, void *cb_data)
{
if (!strcmp(refname, "bad")) {
current_bad_sha1 = sha1;
current_bad_sha1 = xmalloc(20);
hashcpy(current_bad_sha1, sha1);
} else if (!prefixcmp(refname, "good-")) {
sha1_array_append(&good_revs, sha1);
} else if (!prefixcmp(refname, "skip-")) {
Expand Down
6 changes: 4 additions & 2 deletions builtin/describe.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ struct commit_name {
unsigned prio:2; /* annotated tag = 2, tag = 1, head = 0 */
unsigned name_checked:1;
unsigned char sha1[20];
const char *path;
char *path;
};
static const char *prio_names[] = {
"head", "lightweight", "annotated",
Expand Down Expand Up @@ -127,12 +127,14 @@ static void add_to_known_names(const char *path,
} else {
e->next = NULL;
}
e->path = NULL;
}
e->tag = tag;
e->prio = prio;
e->name_checked = 0;
hashcpy(e->sha1, sha1);
e->path = path;
free(e->path);
e->path = xstrdup(path);
}
}

Expand Down
69 changes: 34 additions & 35 deletions builtin/diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,21 +153,23 @@ static int builtin_diff_index(struct rev_info *revs,

static int builtin_diff_tree(struct rev_info *revs,
int argc, const char **argv,
struct object_array_entry *ent)
struct object_array_entry *ent0,
struct object_array_entry *ent1)
{
const unsigned char *(sha1[2]);
int swap = 0;

if (argc > 1)
usage(builtin_diff_usage);

/* We saw two trees, ent[0] and ent[1].
* if ent[1] is uninteresting, they are swapped
/*
* We saw two trees, ent0 and ent1. If ent1 is uninteresting,
* swap them.
*/
if (ent[1].item->flags & UNINTERESTING)
if (ent1->item->flags & UNINTERESTING)
swap = 1;
sha1[swap] = ent[0].item->sha1;
sha1[1-swap] = ent[1].item->sha1;
sha1[swap] = ent0->item->sha1;
sha1[1-swap] = ent1->item->sha1;
diff_tree_sha1(sha1[0], sha1[1], "", &revs->diffopt);
log_tree_diff_flush(revs);
return 0;
Expand Down Expand Up @@ -251,8 +253,8 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
{
int i;
struct rev_info rev;
struct object_array_entry ent[100];
int ents = 0, blobs = 0, paths = 0;
struct object_array ent = OBJECT_ARRAY_INIT;
int blobs = 0, paths = 0;
const char *path = NULL;
struct blobinfo blob[2];
int nongit;
Expand Down Expand Up @@ -337,9 +339,9 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
}

for (i = 0; i < rev.pending.nr; i++) {
struct object_array_entry *list = rev.pending.objects+i;
struct object *obj = list->item;
const char *name = list->name;
struct object_array_entry *entry = &rev.pending.objects[i];
struct object *obj = entry->item;
const char *name = entry->name;
int flags = (obj->flags & UNINTERESTING);
if (!obj->parsed)
obj = parse_object(obj->sha1);
Expand All @@ -348,27 +350,21 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
die(_("invalid object '%s' given."), name);
if (obj->type == OBJ_COMMIT)
obj = &((struct commit *)obj)->tree->object;

if (obj->type == OBJ_TREE) {
if (ARRAY_SIZE(ent) <= ents)
die(_("more than %d trees given: '%s'"),
(int) ARRAY_SIZE(ent), name);
obj->flags |= flags;
ent[ents].item = obj;
ent[ents].name = name;
ents++;
continue;
}
if (obj->type == OBJ_BLOB) {
add_object_array(obj, name, &ent);
} else if (obj->type == OBJ_BLOB) {
if (2 <= blobs)
die(_("more than two blobs given: '%s'"), name);
hashcpy(blob[blobs].sha1, obj->sha1);
blob[blobs].name = name;
blob[blobs].mode = list->mode;
blob[blobs].mode = entry->mode;
blobs++;
continue;

} else {
die(_("unhandled object '%s' given."), name);
}
die(_("unhandled object '%s' given."), name);
}
if (rev.prune_data.nr) {
if (!path)
Expand All @@ -379,7 +375,7 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
/*
* Now, do the arguments look reasonable?
*/
if (!ents) {
if (!ent.nr) {
switch (blobs) {
case 0:
result = builtin_diff_files(&rev, argc, argv);
Expand All @@ -400,23 +396,26 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
}
else if (blobs)
usage(builtin_diff_usage);
else if (ents == 1)
else if (ent.nr == 1)
result = builtin_diff_index(&rev, argc, argv);
else if (ents == 2)
result = builtin_diff_tree(&rev, argc, argv, ent);
else if (ent[0].item->flags & UNINTERESTING) {
else if (ent.nr == 2)
result = builtin_diff_tree(&rev, argc, argv,
&ent.objects[0], &ent.objects[1]);
else if (ent.objects[0].item->flags & UNINTERESTING) {
/*
* diff A...B where there is at least one merge base
* between A and B. We have ent[0] == merge-base,
* ent[ents-2] == A, and ent[ents-1] == B. Show diff
* between the base and B. Note that we pick one
* merge base at random if there are more than one.
* between A and B. We have ent.objects[0] ==
* merge-base, ent.objects[ents-2] == A, and
* ent.objects[ents-1] == B. Show diff between the
* base and B. Note that we pick one merge base at
* random if there are more than one.
*/
ent[1] = ent[ents-1];
result = builtin_diff_tree(&rev, argc, argv, ent);
result = builtin_diff_tree(&rev, argc, argv,
&ent.objects[0],
&ent.objects[ent.nr-1]);
} else
result = builtin_diff_combined(&rev, argc, argv,
ent, ents);
ent.objects, ent.nr);
result = diff_result_code(&rev.diffopt, result);
if (1 < rev.diffopt.skip_stat_unmatch)
refresh_index_quietly();
Expand Down
29 changes: 17 additions & 12 deletions builtin/fetch.c
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,8 @@ static int add_existing(const char *refname, const unsigned char *sha1,
{
struct string_list *list = (struct string_list *)cbdata;
struct string_list_item *item = string_list_insert(list, refname);
item->util = (void *)sha1;
item->util = xmalloc(20);
hashcpy(item->util, sha1);
return 0;
}

Expand All @@ -619,7 +620,7 @@ static void find_non_local_tags(struct transport *transport,
struct ref **head,
struct ref ***tail)
{
struct string_list existing_refs = STRING_LIST_INIT_NODUP;
struct string_list existing_refs = STRING_LIST_INIT_DUP;
struct string_list remote_refs = STRING_LIST_INIT_NODUP;
const struct ref *ref;
struct string_list_item *item = NULL;
Expand Down Expand Up @@ -665,7 +666,7 @@ static void find_non_local_tags(struct transport *transport,
item = string_list_insert(&remote_refs, ref->name);
item->util = (void *)ref->old_sha1;
}
string_list_clear(&existing_refs, 0);
string_list_clear(&existing_refs, 1);

/*
* We may have a final lightweight tag that needs to be
Expand Down Expand Up @@ -722,11 +723,11 @@ static int truncate_fetch_head(void)
static int do_fetch(struct transport *transport,
struct refspec *refs, int ref_count)
{
struct string_list existing_refs = STRING_LIST_INIT_NODUP;
struct string_list_item *peer_item = NULL;
struct string_list existing_refs = STRING_LIST_INIT_DUP;
struct ref *ref_map;
struct ref *rm;
int autotags = (transport->remote->fetch_tags == 1);
int retcode = 0;

for_each_ref(add_existing, &existing_refs);

Expand All @@ -742,9 +743,9 @@ static int do_fetch(struct transport *transport,

/* if not appending, truncate FETCH_HEAD */
if (!append && !dry_run) {
int errcode = truncate_fetch_head();
if (errcode)
return errcode;
retcode = truncate_fetch_head();
if (retcode)
goto cleanup;
}

ref_map = get_ref_map(transport, refs, ref_count, tags, &autotags);
Expand All @@ -753,8 +754,9 @@ static int do_fetch(struct transport *transport,

for (rm = ref_map; rm; rm = rm->next) {
if (rm->peer_ref) {
peer_item = string_list_lookup(&existing_refs,
rm->peer_ref->name);
struct string_list_item *peer_item =
string_list_lookup(&existing_refs,
rm->peer_ref->name);
if (peer_item)
hashcpy(rm->peer_ref->old_sha1,
peer_item->util);
Expand All @@ -765,7 +767,8 @@ static int do_fetch(struct transport *transport,
transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, "1");
if (fetch_refs(transport, ref_map)) {
free_refs(ref_map);
return 1;
retcode = 1;
goto cleanup;
}
if (prune) {
/* If --tags was specified, pretend the user gave us the canonical tags refspec */
Expand Down Expand Up @@ -808,7 +811,9 @@ static int do_fetch(struct transport *transport,
free_refs(ref_map);
}

return 0;
cleanup:
string_list_clear(&existing_refs, 1);
return retcode;
}

static void set_option(const char *name, const char *value)
Expand Down
2 changes: 1 addition & 1 deletion builtin/fsck.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ static int mark_object(struct object *obj, int type, void *data)
return 1;
}

add_object_array(obj, (void *) parent, &pending);
add_object_array(obj, NULL, &pending);
return 0;
}

Expand Down
2 changes: 1 addition & 1 deletion builtin/show-ref.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ static int add_existing(const char *refname, const unsigned char *sha1, int flag
*/
static int exclude_existing(const char *match)
{
static struct string_list existing_refs = STRING_LIST_INIT_NODUP;
static struct string_list existing_refs = STRING_LIST_INIT_DUP;
char buf[1024];
int matchlen = match ? strlen(match) : 0;

Expand Down
2 changes: 1 addition & 1 deletion bundle.c
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ int create_bundle(struct bundle_header *header, const char *path,
if (!get_sha1_hex(buf.buf + 1, sha1)) {
struct object *object = parse_object_or_die(sha1, buf.buf);
object->flags |= UNINTERESTING;
add_pending_object(&revs, object, xstrdup(buf.buf));
add_pending_object(&revs, object, buf.buf);
}
} else if (!get_sha1_hex(buf.buf, sha1)) {
struct object *object = parse_object_or_die(sha1, buf.buf);
Expand Down
6 changes: 3 additions & 3 deletions http-backend.c
Original file line number Diff line number Diff line change
Expand Up @@ -410,14 +410,14 @@ static void get_info_refs(char *arg)
strbuf_release(&buf);
}

static int show_head_ref(const char *name, const unsigned char *sha1,
static int show_head_ref(const char *refname, const unsigned char *sha1,
int flag, void *cb_data)
{
struct strbuf *buf = cb_data;

if (flag & REF_ISSYMREF) {
unsigned char sha1[20];
const char *target = resolve_ref_unsafe(name, sha1, 1, NULL);
unsigned char unused[20];
const char *target = resolve_ref_unsafe(refname, unused, 1, NULL);
const char *target_nons = strip_namespace(target);

strbuf_addf(buf, "ref: %s\n", target_nons);
Expand Down
10 changes: 7 additions & 3 deletions notes.c
Original file line number Diff line number Diff line change
Expand Up @@ -918,17 +918,21 @@ int combine_notes_cat_sort_uniq(unsigned char *cur_sha1,
return ret;
}

static int string_list_add_one_ref(const char *path, const unsigned char *sha1,
static int string_list_add_one_ref(const char *refname, const unsigned char *sha1,
int flag, void *cb)
{
struct string_list *refs = cb;
if (!unsorted_string_list_has_string(refs, path))
string_list_append(refs, path);
if (!unsorted_string_list_has_string(refs, refname))
string_list_append(refs, refname);
return 0;
}

/*
* The list argument must have strdup_strings set on it.
*/
void string_list_add_refs_by_glob(struct string_list *list, const char *glob)
{
assert(list->strdup_strings);
if (has_glob_specials(glob)) {
for_each_glob_ref(string_list_add_one_ref, glob, list);
} else {
Expand Down
Loading

0 comments on commit ede63a1

Please sign in to comment.