[PATCH 04/10] branch: roll show_detached HEAD into regular ref_list

2015-08-04 Thread Karthik Nayak
Remove show_detached() and make detached HEAD to be rolled into
regular ref_list by adding REF_DETACHED_HEAD as a kind of branch and
supporting the same in append_ref(). This eliminates the need for an
extra function and helps in easier porting of branch.c to use
ref-filter APIs.

Before show_detached() used to check if the HEAD branch satisfies the
'--contains' option, now that is taken care by append_ref().

Based-on-patch-by: Jeff King 
Mentored-by: Christian Couder 
Mentored-by: Matthieu Moy 
Signed-off-by: Karthik Nayak 
---
 builtin/branch.c | 68 +---
 1 file changed, 40 insertions(+), 28 deletions(-)

diff --git a/builtin/branch.c b/builtin/branch.c
index 65f6d0d..81815c9 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -30,6 +30,7 @@ static const char * const builtin_branch_usage[] = {
 
 #define REF_LOCAL_BRANCH0x01
 #define REF_REMOTE_BRANCH   0x02
+#define REF_DETACHED_HEAD   0x04
 
 static const char *head;
 static unsigned char head_sha1[20];
@@ -352,8 +353,12 @@ static int append_ref(const char *refname, const struct 
object_id *oid, int flag
break;
}
}
-   if (ARRAY_SIZE(ref_kind) <= i)
-   return 0;
+   if (ARRAY_SIZE(ref_kind) <= i) {
+   if (!strcmp(refname, "HEAD"))
+   kind = REF_DETACHED_HEAD;
+   else
+   return 0;
+   }
 
/* Don't add types the caller doesn't want */
if ((kind & ref_list->kinds) == 0)
@@ -535,6 +540,7 @@ static void print_ref_item(struct ref_item *item, int 
maxwidth, int verbose,
int color;
struct strbuf out = STRBUF_INIT, name = STRBUF_INIT;
const char *prefix = "";
+   const char *desc = item->name;
 
if (item->ignore)
return;
@@ -547,6 +553,10 @@ static void print_ref_item(struct ref_item *item, int 
maxwidth, int verbose,
color = BRANCH_COLOR_REMOTE;
prefix = remote_prefix;
break;
+   case REF_DETACHED_HEAD:
+   color = BRANCH_COLOR_CURRENT;
+   desc = get_head_description();
+   break;
default:
color = BRANCH_COLOR_PLAIN;
break;
@@ -558,7 +568,7 @@ static void print_ref_item(struct ref_item *item, int 
maxwidth, int verbose,
color = BRANCH_COLOR_CURRENT;
}
 
-   strbuf_addf(&name, "%s%s", prefix, item->name);
+   strbuf_addf(&name, "%s%s", prefix, desc);
if (verbose) {
int utf8_compensation = strlen(name.buf) - 
utf8_strwidth(name.buf);
strbuf_addf(&out, "%c %s%-*s%s", c, branch_get_color(color),
@@ -581,6 +591,8 @@ static void print_ref_item(struct ref_item *item, int 
maxwidth, int verbose,
}
strbuf_release(&name);
strbuf_release(&out);
+   if (item->kind == REF_DETACHED_HEAD)
+   free((void *)desc);
 }
 
 static int calc_maxwidth(struct ref_list *refs, int remote_bonus)
@@ -600,25 +612,9 @@ static int calc_maxwidth(struct ref_list *refs, int 
remote_bonus)
return max;
 }
 
-static void show_detached(struct ref_list *ref_list, int maxwidth)
-{
-   struct commit *head_commit = lookup_commit_reference_gently(head_sha1, 
1);
-
-   if (head_commit && is_descendant_of(head_commit, 
ref_list->with_commit)) {
-   struct ref_item item;
-   item.name = get_head_description();
-   item.kind = REF_LOCAL_BRANCH;
-   item.dest = NULL;
-   item.commit = head_commit;
-   item.ignore = 0;
-   print_ref_item(&item, maxwidth, ref_list->verbose, 
ref_list->abbrev, 1, "");
-   free(item.name);
-   }
-}
-
 static int print_ref_list(int kinds, int detached, int verbose, int abbrev, 
struct commit_list *with_commit, const char **pattern)
 {
-   int i;
+   int i, index;
struct append_ref_cb cb;
struct ref_list ref_list;
int maxwidth = 0;
@@ -642,7 +638,14 @@ static int print_ref_list(int kinds, int detached, int 
verbose, int abbrev, stru
cb.ref_list = &ref_list;
cb.pattern = pattern;
cb.ret = 0;
+   /*
+* First we obtain all regular branch refs and then if the
+* HEAD is detached then we insert that ref to the end of the
+* ref_fist so that it can be printed first.
+*/
for_each_rawref(append_ref, &cb);
+   if (detached)
+   head_ref(append_ref, &cb);
/*
 * The following implementation is currently duplicated in ref-filter. 
It
 * will eventually be removed when we port branch.c to use ref-filter 
APIs.
@@ -678,15 +681,20 @@ static int print_ref_list(int kinds, int detached, int 
verbose, int abbrev, stru
if (verbose)
maxwidth = calc_maxwidth(&ref_list, strlen(remote_prefix));
 
-   qsort(ref_list.list, ref

Re: [PATCH 04/10] branch: roll show_detached HEAD into regular ref_list

2015-08-10 Thread Eric Sunshine
On Tue, Aug 4, 2015 at 9:01 AM, Karthik Nayak  wrote:
> Remove show_detached() and make detached HEAD to be rolled into
> regular ref_list by adding REF_DETACHED_HEAD as a kind of branch and
> supporting the same in append_ref(). This eliminates the need for an
> extra function and helps in easier porting of branch.c to use
> ref-filter APIs.
>
> Before show_detached() used to check if the HEAD branch satisfies the
> '--contains' option, now that is taken care by append_ref().
>
> Signed-off-by: Karthik Nayak 
> ---
> diff --git a/builtin/branch.c b/builtin/branch.c
> index 65f6d0d..81815c9 100644
> --- a/builtin/branch.c
> +++ b/builtin/branch.c
> @@ -535,6 +540,7 @@ static void print_ref_item(struct ref_item *item, int 
> maxwidth, int verbose,
> int color;
> struct strbuf out = STRBUF_INIT, name = STRBUF_INIT;
> const char *prefix = "";
> +   const char *desc = item->name;
>
> if (item->ignore)
> return;
> @@ -547,6 +553,10 @@ static void print_ref_item(struct ref_item *item, int 
> maxwidth, int verbose,
> color = BRANCH_COLOR_REMOTE;
> prefix = remote_prefix;
> break;
> +   case REF_DETACHED_HEAD:
> +   color = BRANCH_COLOR_CURRENT;
> +   desc = get_head_description();
> +   break;
> default:
> color = BRANCH_COLOR_PLAIN;
> break;
> @@ -558,7 +568,7 @@ static void print_ref_item(struct ref_item *item, int 
> maxwidth, int verbose,
> color = BRANCH_COLOR_CURRENT;
> }
>
> -   strbuf_addf(&name, "%s%s", prefix, item->name);
> +   strbuf_addf(&name, "%s%s", prefix, desc);
> if (verbose) {
> int utf8_compensation = strlen(name.buf) - 
> utf8_strwidth(name.buf);
> strbuf_addf(&out, "%c %s%-*s%s", c, branch_get_color(color),
> @@ -581,6 +591,8 @@ static void print_ref_item(struct ref_item *item, int 
> maxwidth, int verbose,
> }
> strbuf_release(&name);
> strbuf_release(&out);
> +   if (item->kind == REF_DETACHED_HEAD)
> +   free((void *)desc);

This would be cleaner, and more easily extended to other cases if you
used a 'to_free' variable. For instance, something like this:

const char *desc = item->name;
char *to_free = NULL;
...
case REF_DETACHED_HEAD:
...
desc = to_free = get_head_description();
break;
...
strbuf_addf(&name, "%s%s", prefix, desc);
...
free(to_free);

Note that it's safe to free 'to_free' when it's NULL, so you don't
need to protect the free() with that ugly specialized 'if' which
checks for REF_DETACHED_HEAD. You can also do away with the "cast to
non-const" when freeing.

>  }
> @@ -642,7 +638,14 @@ static int print_ref_list(int kinds, int detached, int 
> verbose, int abbrev, stru
> cb.ref_list = &ref_list;
> cb.pattern = pattern;
> cb.ret = 0;
> +   /*
> +* First we obtain all regular branch refs and then if the

s/then//

> +* HEAD is detached then we insert that ref to the end of the
> +* ref_fist so that it can be printed first.
> +*/
> for_each_rawref(append_ref, &cb);
> +   if (detached)
> +   head_ref(append_ref, &cb);
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [PATCH 04/10] branch: roll show_detached HEAD into regular ref_list

2015-08-11 Thread Karthik Nayak
On Tue, Aug 11, 2015 at 8:11 AM, Eric Sunshine  wrote:
> On Tue, Aug 4, 2015 at 9:01 AM, Karthik Nayak  wrote:
>> Remove show_detached() and make detached HEAD to be rolled into
>> regular ref_list by adding REF_DETACHED_HEAD as a kind of branch and
>> supporting the same in append_ref(). This eliminates the need for an
>> extra function and helps in easier porting of branch.c to use
>> ref-filter APIs.
>>
>> Before show_detached() used to check if the HEAD branch satisfies the
>> '--contains' option, now that is taken care by append_ref().
>>
>> Signed-off-by: Karthik Nayak 
>> ---
>> diff --git a/builtin/branch.c b/builtin/branch.c
>> index 65f6d0d..81815c9 100644
>> --- a/builtin/branch.c
>> +++ b/builtin/branch.c
>> @@ -535,6 +540,7 @@ static void print_ref_item(struct ref_item *item, int 
>> maxwidth, int verbose,
>> int color;
>> struct strbuf out = STRBUF_INIT, name = STRBUF_INIT;
>> const char *prefix = "";
>> +   const char *desc = item->name;
>>
>> if (item->ignore)
>> return;
>> @@ -547,6 +553,10 @@ static void print_ref_item(struct ref_item *item, int 
>> maxwidth, int verbose,
>> color = BRANCH_COLOR_REMOTE;
>> prefix = remote_prefix;
>> break;
>> +   case REF_DETACHED_HEAD:
>> +   color = BRANCH_COLOR_CURRENT;
>> +   desc = get_head_description();
>> +   break;
>> default:
>> color = BRANCH_COLOR_PLAIN;
>> break;
>> @@ -558,7 +568,7 @@ static void print_ref_item(struct ref_item *item, int 
>> maxwidth, int verbose,
>> color = BRANCH_COLOR_CURRENT;
>> }
>>
>> -   strbuf_addf(&name, "%s%s", prefix, item->name);
>> +   strbuf_addf(&name, "%s%s", prefix, desc);
>> if (verbose) {
>> int utf8_compensation = strlen(name.buf) - 
>> utf8_strwidth(name.buf);
>> strbuf_addf(&out, "%c %s%-*s%s", c, branch_get_color(color),
>> @@ -581,6 +591,8 @@ static void print_ref_item(struct ref_item *item, int 
>> maxwidth, int verbose,
>> }
>> strbuf_release(&name);
>> strbuf_release(&out);
>> +   if (item->kind == REF_DETACHED_HEAD)
>> +   free((void *)desc);
>
> This would be cleaner, and more easily extended to other cases if you
> used a 'to_free' variable. For instance, something like this:
>
> const char *desc = item->name;
> char *to_free = NULL;
> ...
> case REF_DETACHED_HEAD:
> ...
> desc = to_free = get_head_description();
> break;
> ...
> strbuf_addf(&name, "%s%s", prefix, desc);
> ...
> free(to_free);
>

This looks neater!

> Note that it's safe to free 'to_free' when it's NULL, so you don't
> need to protect the free() with that ugly specialized 'if' which
> checks for REF_DETACHED_HEAD. You can also do away with the "cast to
> non-const" when freeing.
>

Yea makes sense will change to what you suggested.

>>  }
>> @@ -642,7 +638,14 @@ static int print_ref_list(int kinds, int detached, int 
>> verbose, int abbrev, stru
>> cb.ref_list = &ref_list;
>> cb.pattern = pattern;
>> cb.ret = 0;
>> +   /*
>> +* First we obtain all regular branch refs and then if the
>
> s/then//
>

Thanks

-- 
Regards,
Karthik Nayak
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html