All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] use pop_commit() for consuming the first entry of a struct commit_list
@ 2015-10-24 16:21 René Scharfe
  2015-10-26 21:33 ` Junio C Hamano
  0 siblings, 1 reply; 3+ messages in thread
From: René Scharfe @ 2015-10-24 16:21 UTC (permalink / raw)
  To: Git List; +Cc: Junio C Hamano

Instead of open-coding the function pop_commit() just call it.  This
makes the intent clearer and reduces code size.

Signed-off-by: Rene Scharfe <l.s.r@web.de>
---
 builtin/fmt-merge-msg.c |  9 +++------
 builtin/merge.c         | 12 +++++-------
 builtin/reflog.c        |  6 +-----
 builtin/rev-parse.c     |  7 ++-----
 builtin/show-branch.c   | 17 +++--------------
 commit.c                | 27 +++++++--------------------
 remote.c                |  6 ++----
 revision.c              | 27 +++++----------------------
 shallow.c               |  6 +-----
 upload-pack.c           |  6 ++----
 10 files changed, 31 insertions(+), 92 deletions(-)

diff --git a/builtin/fmt-merge-msg.c b/builtin/fmt-merge-msg.c
index 4ba7f28..846004b 100644
--- a/builtin/fmt-merge-msg.c
+++ b/builtin/fmt-merge-msg.c
@@ -537,7 +537,7 @@ static void fmt_merge_msg_sigs(struct strbuf *out)
 static void find_merge_parents(struct merge_parents *result,
 			       struct strbuf *in, unsigned char *head)
 {
-	struct commit_list *parents, *next;
+	struct commit_list *parents;
 	struct commit *head_commit;
 	int pos = 0, i, j;
 
@@ -576,13 +576,10 @@ static void find_merge_parents(struct merge_parents *result,
 	parents = reduce_heads(parents);
 
 	while (parents) {
+		struct commit *cmit = pop_commit(&parents);
 		for (i = 0; i < result->nr; i++)
-			if (!hashcmp(result->item[i].commit,
-				     parents->item->object.sha1))
+			if (!hashcmp(result->item[i].commit, cmit->object.sha1))
 				result->item[i].used = 1;
-		next = parents->next;
-		free(parents);
-		parents = next;
 	}
 
 	for (i = j = 0; i < result->nr; i++) {
diff --git a/builtin/merge.c b/builtin/merge.c
index a0a9328..31241b8 100644
--- a/builtin/merge.c
+++ b/builtin/merge.c
@@ -1019,7 +1019,7 @@ static struct commit_list *reduce_parents(struct commit *head_commit,
 					  int *head_subsumed,
 					  struct commit_list *remoteheads)
 {
-	struct commit_list *parents, *next, **remotes = &remoteheads;
+	struct commit_list *parents, **remotes;
 
 	/*
 	 * Is the current HEAD reachable from another commit being
@@ -1033,16 +1033,14 @@ static struct commit_list *reduce_parents(struct commit *head_commit,
 	/* Find what parents to record by checking independent ones. */
 	parents = reduce_heads(remoteheads);
 
-	for (remoteheads = NULL, remotes = &remoteheads;
-	     parents;
-	     parents = next) {
-		struct commit *commit = parents->item;
-		next = parents->next;
+	remoteheads = NULL;
+	remotes = &remoteheads;
+	while (parents) {
+		struct commit *commit = pop_commit(&parents);
 		if (commit == head_commit)
 			*head_subsumed = 0;
 		else
 			remotes = &commit_list_insert(commit, remotes)->next;
-		free(parents);
 	}
 	return remoteheads;
 }
diff --git a/builtin/reflog.c b/builtin/reflog.c
index f96ca2a..cf1145e 100644
--- a/builtin/reflog.c
+++ b/builtin/reflog.c
@@ -218,7 +218,6 @@ static int keep_entry(struct commit **it, unsigned char *sha1)
  */
 static void mark_reachable(struct expire_reflog_policy_cb *cb)
 {
-	struct commit *commit;
 	struct commit_list *pending;
 	unsigned long expire_limit = cb->mark_limit;
 	struct commit_list *leftover = NULL;
@@ -228,11 +227,8 @@ static void mark_reachable(struct expire_reflog_policy_cb *cb)
 
 	pending = cb->mark_list;
 	while (pending) {
-		struct commit_list *entry = pending;
 		struct commit_list *parent;
-		pending = entry->next;
-		commit = entry->item;
-		free(entry);
+		struct commit *commit = pop_commit(&pending);
 		if (commit->object.flags & REACHABLE)
 			continue;
 		if (parse_commit(commit))
diff --git a/builtin/rev-parse.c b/builtin/rev-parse.c
index 02d747d..e92a782 100644
--- a/builtin/rev-parse.c
+++ b/builtin/rev-parse.c
@@ -281,11 +281,8 @@ static int try_difference(const char *arg)
 			b = lookup_commit_reference(end);
 			exclude = get_merge_bases(a, b);
 			while (exclude) {
-				struct commit_list *n = exclude->next;
-				show_rev(REVERSED,
-					 exclude->item->object.sha1,NULL);
-				free(exclude);
-				exclude = n;
+				struct commit *commit = pop_commit(&exclude);
+				show_rev(REVERSED, commit->object.sha1, NULL);
 			}
 		}
 		*dotdot = '.';
diff --git a/builtin/show-branch.c b/builtin/show-branch.c
index 092b59b..ac5141d 100644
--- a/builtin/show-branch.c
+++ b/builtin/show-branch.c
@@ -53,17 +53,6 @@ static struct commit *interesting(struct commit_list *list)
 	return NULL;
 }
 
-static struct commit *pop_one_commit(struct commit_list **list_p)
-{
-	struct commit *commit;
-	struct commit_list *list;
-	list = *list_p;
-	commit = list->item;
-	*list_p = list->next;
-	free(list);
-	return commit;
-}
-
 struct commit_name {
 	const char *head_name; /* which head's ancestor? */
 	int generation; /* how many parents away from head_name */
@@ -213,7 +202,7 @@ static void join_revs(struct commit_list **list_p,
 	while (*list_p) {
 		struct commit_list *parents;
 		int still_interesting = !!interesting(*list_p);
-		struct commit *commit = pop_one_commit(list_p);
+		struct commit *commit = pop_commit(list_p);
 		int flags = commit->object.flags & all_mask;
 
 		if (!still_interesting && extra <= 0)
@@ -504,7 +493,7 @@ static int show_merge_base(struct commit_list *seen, int num_rev)
 	int exit_status = 1;
 
 	while (seen) {
-		struct commit *commit = pop_one_commit(&seen);
+		struct commit *commit = pop_commit(&seen);
 		int flags = commit->object.flags & all_mask;
 		if (!(flags & UNINTERESTING) &&
 		    ((flags & all_revs) == all_revs)) {
@@ -929,7 +918,7 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
 	all_revs = all_mask & ~((1u << REV_SHIFT) - 1);
 
 	while (seen) {
-		struct commit *commit = pop_one_commit(&seen);
+		struct commit *commit = pop_commit(&seen);
 		int this_flag = commit->object.flags;
 		int is_merge_point = ((this_flag & all_revs) == all_revs);
 
diff --git a/commit.c b/commit.c
index 494615d..d1810c9 100644
--- a/commit.c
+++ b/commit.c
@@ -455,11 +455,8 @@ struct commit_list *copy_commit_list(struct commit_list *list)
 
 void free_commit_list(struct commit_list *list)
 {
-	while (list) {
-		struct commit_list *temp = list;
-		list = temp->next;
-		free(temp);
-	}
+	while (list)
+		pop_commit(&list);
 }
 
 struct commit_list * commit_list_insert_by_date(struct commit *item, struct commit_list **list)
@@ -505,12 +502,8 @@ void commit_list_sort_by_date(struct commit_list **list)
 struct commit *pop_most_recent_commit(struct commit_list **list,
 				      unsigned int mark)
 {
-	struct commit *ret = (*list)->item;
+	struct commit *ret = pop_commit(list);
 	struct commit_list *parents = ret->parents;
-	struct commit_list *old = *list;
-
-	*list = (*list)->next;
-	free(old);
 
 	while (parents) {
 		struct commit *commit = parents->item;
@@ -861,11 +854,9 @@ static struct commit_list *merge_bases_many(struct commit *one, int n, struct co
 	list = paint_down_to_common(one, n, twos);
 
 	while (list) {
-		struct commit_list *next = list->next;
-		if (!(list->item->object.flags & STALE))
-			commit_list_insert_by_date(list->item, &result);
-		free(list);
-		list = next;
+		struct commit *commit = pop_commit(&list);
+		if (!(commit->object.flags & STALE))
+			commit_list_insert_by_date(commit, &result);
 	}
 	return result;
 }
@@ -1546,13 +1537,9 @@ int commit_tree_extended(const char *msg, size_t msg_len,
 	 * if everything else stays the same.
 	 */
 	while (parents) {
-		struct commit_list *next = parents->next;
-		struct commit *parent = parents->item;
-
+		struct commit *parent = pop_commit(&parents);
 		strbuf_addf(&buffer, "parent %s\n",
 			    sha1_to_hex(parent->object.sha1));
-		free(parents);
-		parents = next;
 	}
 
 	/* Person/date information */
diff --git a/remote.c b/remote.c
index 1101f82..977e34e 100644
--- a/remote.c
+++ b/remote.c
@@ -1938,10 +1938,8 @@ int resolve_remote_symref(struct ref *ref, struct ref *list)
 static void unmark_and_free(struct commit_list *list, unsigned int mark)
 {
 	while (list) {
-		struct commit_list *temp = list;
-		temp->item->object.flags &= ~mark;
-		list = temp->next;
-		free(temp);
+		struct commit *commit = pop_commit(&list);
+		commit->object.flags &= ~mark;
 	}
 }
 
diff --git a/revision.c b/revision.c
index 2236463..0fbb684 100644
--- a/revision.c
+++ b/revision.c
@@ -153,10 +153,7 @@ void mark_parents_uninteresting(struct commit *commit)
 		commit_list_insert(l->item, &parents);
 
 	while (parents) {
-		struct commit *commit = parents->item;
-		l = parents;
-		parents = parents->next;
-		free(l);
+		struct commit *commit = pop_commit(&parents);
 
 		while (commit) {
 			/*
@@ -1102,14 +1099,10 @@ static int limit_list(struct rev_info *revs)
 	}
 
 	while (list) {
-		struct commit_list *entry = list;
-		struct commit *commit = list->item;
+		struct commit *commit = pop_commit(&list);
 		struct object *obj = &commit->object;
 		show_early_output_fn_t show;
 
-		list = list->next;
-		free(entry);
-
 		if (commit == interesting_cache)
 			interesting_cache = NULL;
 
@@ -2733,10 +2726,7 @@ static void simplify_merges(struct rev_info *revs)
 		yet_to_do = NULL;
 		tail = &yet_to_do;
 		while (list) {
-			commit = list->item;
-			next = list->next;
-			free(list);
-			list = next;
+			commit = pop_commit(&list);
 			tail = simplify_one(revs, commit, tail);
 		}
 	}
@@ -2748,10 +2738,7 @@ static void simplify_merges(struct rev_info *revs)
 	while (list) {
 		struct merge_simplify_state *st;
 
-		commit = list->item;
-		next = list->next;
-		free(list);
-		list = next;
+		commit = pop_commit(&list);
 		st = locate_simplify_state(revs, commit);
 		if (st->simplified == commit)
 			tail = &commit_list_insert(commit, tail)->next;
@@ -3125,11 +3112,7 @@ static struct commit *get_revision_1(struct rev_info *revs)
 		return NULL;
 
 	do {
-		struct commit_list *entry = revs->commits;
-		struct commit *commit = entry->item;
-
-		revs->commits = entry->next;
-		free(entry);
+		struct commit *commit = pop_commit(&revs->commits);
 
 		if (revs->reflog_info) {
 			save_parents(revs, commit);
diff --git a/shallow.c b/shallow.c
index d49a3d6..4dcb454 100644
--- a/shallow.c
+++ b/shallow.c
@@ -401,13 +401,9 @@ static void paint_down(struct paint_info *info, const unsigned char *sha1,
 	commit_list_insert(c, &head);
 	while (head) {
 		struct commit_list *p;
-		struct commit *c = head->item;
+		struct commit *c = pop_commit(&head);
 		uint32_t **refs = ref_bitmap_at(&info->ref_bitmap, c);
 
-		p = head;
-		head = head->next;
-		free(p);
-
 		/* XXX check "UNINTERESTING" from pack bitmaps if available */
 		if (c->object.flags & (SEEN | UNINTERESTING))
 			continue;
diff --git a/upload-pack.c b/upload-pack.c
index 89e832b..d0bc3ca 100644
--- a/upload-pack.c
+++ b/upload-pack.c
@@ -316,10 +316,8 @@ static int reachable(struct commit *want)
 
 	commit_list_insert_by_date(want, &work);
 	while (work) {
-		struct commit_list *list = work->next;
-		struct commit *commit = work->item;
-		free(work);
-		work = list;
+		struct commit_list *list;
+		struct commit *commit = pop_commit(&work);
 
 		if (commit->object.flags & THEY_HAVE) {
 			want->object.flags |= COMMON_KNOWN;
-- 
2.6.2

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] use pop_commit() for consuming the first entry of a struct commit_list
  2015-10-24 16:21 [PATCH] use pop_commit() for consuming the first entry of a struct commit_list René Scharfe
@ 2015-10-26 21:33 ` Junio C Hamano
  2015-10-27 21:29   ` René Scharfe
  0 siblings, 1 reply; 3+ messages in thread
From: Junio C Hamano @ 2015-10-26 21:33 UTC (permalink / raw)
  To: René Scharfe; +Cc: Git List

René Scharfe <l.s.r@web.de> writes:

> Instead of open-coding the function pop_commit() just call it.  This
> makes the intent clearer and reduces code size.
>
> Signed-off-by: Rene Scharfe <l.s.r@web.de>
> ---
>  builtin/fmt-merge-msg.c |  9 +++------
>  builtin/merge.c         | 12 +++++-------
>  builtin/reflog.c        |  6 +-----
>  builtin/rev-parse.c     |  7 ++-----
>  builtin/show-branch.c   | 17 +++--------------
>  commit.c                | 27 +++++++--------------------
>  remote.c                |  6 ++----
>  revision.c              | 27 +++++----------------------
>  shallow.c               |  6 +-----
>  upload-pack.c           |  6 ++----
>  10 files changed, 31 insertions(+), 92 deletions(-)

While I appreciate this kind of simplification very much, I also
hate to review this kind of simplification at the same time, as it
is very easy to make and miss simple mistakes.

Let me try to go through it very carefully.

> diff --git a/builtin/fmt-merge-msg.c b/builtin/fmt-merge-msg.c
> index 4ba7f28..846004b 100644
> --- a/builtin/fmt-merge-msg.c
> +++ b/builtin/fmt-merge-msg.c
> @@ -537,7 +537,7 @@ static void fmt_merge_msg_sigs(struct strbuf *out)
>  static void find_merge_parents(struct merge_parents *result,
>  			       struct strbuf *in, unsigned char *head)
>  {
> -	struct commit_list *parents, *next;
> +	struct commit_list *parents;
>  	struct commit *head_commit;
>  	int pos = 0, i, j;
>  
> @@ -576,13 +576,10 @@ static void find_merge_parents(struct merge_parents *result,
>  	parents = reduce_heads(parents);
>  
>  	while (parents) {
> +		struct commit *cmit = pop_commit(&parents);
>  		for (i = 0; i < result->nr; i++)
> -			if (!hashcmp(result->item[i].commit,
> -				     parents->item->object.sha1))
> +			if (!hashcmp(result->item[i].commit, cmit->object.sha1))
>  				result->item[i].used = 1;
> -		next = parents->next;
> -		free(parents);
> -		parents = next;

OK, I would have called it "commit" not "cmit", but this is
trivially equivalent to the original.

> diff --git a/builtin/merge.c b/builtin/merge.c
> index a0a9328..31241b8 100644
> --- a/builtin/merge.c
> +++ b/builtin/merge.c
> @@ -1019,7 +1019,7 @@ static struct commit_list *reduce_parents(struct commit *head_commit,
>  					  int *head_subsumed,
>  					  struct commit_list *remoteheads)
>  {
> -	struct commit_list *parents, *next, **remotes = &remoteheads;
> +	struct commit_list *parents, **remotes;

Interesting.  I viewed the result of applying this patch with "git
show -U32" to make sure that this initialization of remotes was
unnecessary.

> @@ -1033,16 +1033,14 @@ static struct commit_list *reduce_parents(struct commit *head_commit,
>  	/* Find what parents to record by checking independent ones. */
>  	parents = reduce_heads(remoteheads);
>  
> -	for (remoteheads = NULL, remotes = &remoteheads;
> -	     parents;
> -	     parents = next) {
> -		struct commit *commit = parents->item;
> -		next = parents->next;
> +	remoteheads = NULL;
> +	remotes = &remoteheads;
> +	while (parents) {
> +		struct commit *commit = pop_commit(&parents);
>  		if (commit == head_commit)
>  			*head_subsumed = 0;
>  		else
>  			remotes = &commit_list_insert(commit, remotes)->next;
> -		free(parents);
>  	}
>  	return remoteheads;
>  }

Trivially equivalent.  Good.  I'll stop saying this if there is
nothing noteworthy from now on.

> diff --git a/builtin/show-branch.c b/builtin/show-branch.c
> index 092b59b..ac5141d 100644
> --- a/builtin/show-branch.c
> +++ b/builtin/show-branch.c
> @@ -53,17 +53,6 @@ static struct commit *interesting(struct commit_list *list)
>  	return NULL;
>  }
>  
> -static struct commit *pop_one_commit(struct commit_list **list_p)
> -{
> -	struct commit *commit;
> -	struct commit_list *list;
> -	list = *list_p;
> -	commit = list->item;
> -	*list_p = list->next;
> -	free(list);
> -	return commit;
> -}
> -

Interesting.  We had our own implementation that is less lenient
than the global one.  And this one is newer by two months!  Good
riddance.

> @@ -2733,10 +2726,7 @@ static void simplify_merges(struct rev_info *revs)
>  		yet_to_do = NULL;
>  		tail = &yet_to_do;
>  		while (list) {
> -			commit = list->item;
> -			next = list->next;
> -			free(list);
> -			list = next;
> +			commit = pop_commit(&list);
>  			tail = simplify_one(revs, commit, tail);
>  		}
>  	}

Any conversion in this set that does not eliminate the intermediate
variable "next" (or named similarly but differently in some
contexts) is suspect, but this is correct.  The variable is used in
an earlier loop to walk a list in an non-destructive way (strictly
speaking, I do not think that loop needs 'next' variable, though).

> diff --git a/shallow.c b/shallow.c
> index d49a3d6..4dcb454 100644
> --- a/shallow.c
> +++ b/shallow.c
> @@ -401,13 +401,9 @@ static void paint_down(struct paint_info *info, const unsigned char *sha1,
>  	commit_list_insert(c, &head);
>  	while (head) {
>  		struct commit_list *p;
> -		struct commit *c = head->item;
> +		struct commit *c = pop_commit(&head);
>  		uint32_t **refs = ref_bitmap_at(&info->ref_bitmap, c);
>  
> -		p = head;
> -		head = head->next;
> -		free(p);
> -
>  		/* XXX check "UNINTERESTING" from pack bitmaps if available */
>  		if (c->object.flags & (SEEN | UNINTERESTING))
>  			continue;

Again, 'p' is used in another loop in the same scope, and the
conversion is correct.

> diff --git a/upload-pack.c b/upload-pack.c
> index 89e832b..d0bc3ca 100644
> --- a/upload-pack.c
> +++ b/upload-pack.c
> @@ -316,10 +316,8 @@ static int reachable(struct commit *want)
>  
>  	commit_list_insert_by_date(want, &work);
>  	while (work) {
> -		struct commit_list *list = work->next;
> -		struct commit *commit = work->item;
> -		free(work);
> -		work = list;
> +		struct commit_list *list;
> +		struct commit *commit = pop_commit(&work);
>  
>  		if (commit->object.flags & THEY_HAVE) {
>  			want->object.flags |= COMMON_KNOWN;

Likewise, "list" is used in the same scope for different purposes,
and the conversion is correct.

Thanks, will apply.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] use pop_commit() for consuming the first entry of a struct commit_list
  2015-10-26 21:33 ` Junio C Hamano
@ 2015-10-27 21:29   ` René Scharfe
  0 siblings, 0 replies; 3+ messages in thread
From: René Scharfe @ 2015-10-27 21:29 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Git List

Am 26.10.2015 um 22:33 schrieb Junio C Hamano:
> René Scharfe <l.s.r@web.de> writes:
>
>> Instead of open-coding the function pop_commit() just call it.  This
>> makes the intent clearer and reduces code size.
>>
>> Signed-off-by: Rene Scharfe <l.s.r@web.de>
>> ---
>>   builtin/fmt-merge-msg.c |  9 +++------
>>   builtin/merge.c         | 12 +++++-------
>>   builtin/reflog.c        |  6 +-----
>>   builtin/rev-parse.c     |  7 ++-----
>>   builtin/show-branch.c   | 17 +++--------------
>>   commit.c                | 27 +++++++--------------------
>>   remote.c                |  6 ++----
>>   revision.c              | 27 +++++----------------------
>>   shallow.c               |  6 +-----
>>   upload-pack.c           |  6 ++----
>>   10 files changed, 31 insertions(+), 92 deletions(-)
>
> While I appreciate this kind of simplification very much, I also
> hate to review this kind of simplification at the same time, as it
> is very easy to make and miss simple mistakes.

Yeah, it's tempting to doze off after a few similar cases.

> Let me try to go through it very carefully.
>
>> diff --git a/builtin/fmt-merge-msg.c b/builtin/fmt-merge-msg.c
>> index 4ba7f28..846004b 100644
>> --- a/builtin/fmt-merge-msg.c
>> +++ b/builtin/fmt-merge-msg.c
>> @@ -537,7 +537,7 @@ static void fmt_merge_msg_sigs(struct strbuf *out)
>>   static void find_merge_parents(struct merge_parents *result,
>>   			       struct strbuf *in, unsigned char *head)
>>   {
>> -	struct commit_list *parents, *next;
>> +	struct commit_list *parents;
>>   	struct commit *head_commit;
>>   	int pos = 0, i, j;
>>
>> @@ -576,13 +576,10 @@ static void find_merge_parents(struct merge_parents *result,
>>   	parents = reduce_heads(parents);
>>
>>   	while (parents) {
>> +		struct commit *cmit = pop_commit(&parents);
>>   		for (i = 0; i < result->nr; i++)
>> -			if (!hashcmp(result->item[i].commit,
>> -				     parents->item->object.sha1))
>> +			if (!hashcmp(result->item[i].commit, cmit->object.sha1))
>>   				result->item[i].used = 1;
>> -		next = parents->next;
>> -		free(parents);
>> -		parents = next;
>
> OK, I would have called it "commit" not "cmit", but this is
> trivially equivalent to the original.

This is just to keep the line shorter than 80 characters, and it's not 
the first "cmit" in the tree..

>
>> diff --git a/builtin/merge.c b/builtin/merge.c
>> index a0a9328..31241b8 100644
>> --- a/builtin/merge.c
>> +++ b/builtin/merge.c
>> @@ -1019,7 +1019,7 @@ static struct commit_list *reduce_parents(struct commit *head_commit,
>>   					  int *head_subsumed,
>>   					  struct commit_list *remoteheads)
>>   {
>> -	struct commit_list *parents, *next, **remotes = &remoteheads;
>> +	struct commit_list *parents, **remotes;
>
> Interesting.  I viewed the result of applying this patch with "git
> show -U32" to make sure that this initialization of remotes was
> unnecessary.

With "git show -W" you don't need any magic number. ;)

Thanks for reviewing!

René

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2015-10-27 21:30 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-10-24 16:21 [PATCH] use pop_commit() for consuming the first entry of a struct commit_list René Scharfe
2015-10-26 21:33 ` Junio C Hamano
2015-10-27 21:29   ` René Scharfe

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.