ref-filter: move get_head_description() from branch.c

Move the implementation of get_head_description() from branch.c to ref-filter. This gives a description of the HEAD ref if called. This is used as the refname for the HEAD ref whenever the FILTER_REFS_DETACHED_HEAD option is used. Make it public because we need it to calculate the length of the HEAD refs description in branch.c:calc_maxwidth() when we port branch.c to use ref-filter APIs. Mentored-by: Christian Couder <christian.couder@gmail.com> Mentored-by: Matthieu Moy <matthieu.moy@grenoble-inp.fr> Signed-off-by: Karthik Nayak <karthik.188@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Karthik Nayak committed Jan 10, 2017 at 14:19 UTC d4919bb288e46c81b92d6fe02c4f4564b8477fd3
3 files changed +38 -35
builtin/branch.c
-33
@@ -364,39 +364,6 @@ static void add_verbose_info(struct strbuf *out, struct ref_array_item *item,
364 strbuf_release(&subject);
365 }
366
367 -static char *get_head_description(void)
368 -{
369 - struct strbuf desc = STRBUF_INIT;
370 - struct wt_status_state state;
371 - memset(&state, 0, sizeof(state));
372 - wt_status_get_state(&state, 1);
373 - if (state.rebase_in_progress ||
374 - state.rebase_interactive_in_progress)
375 - strbuf_addf(&desc, _("(no branch, rebasing %s)"),
376 - state.branch);
377 - else if (state.bisect_in_progress)
378 - strbuf_addf(&desc, _("(no branch, bisect started on %s)"),
379 - state.branch);
380 - else if (state.detached_from) {
381 - if (state.detached_at)
382 - /* TRANSLATORS: make sure this matches
383 - "HEAD detached at " in wt-status.c */
384 - strbuf_addf(&desc, _("(HEAD detached at %s)"),
385 - state.detached_from);
386 - else
387 - /* TRANSLATORS: make sure this matches
388 - "HEAD detached from " in wt-status.c */
389 - strbuf_addf(&desc, _("(HEAD detached from %s)"),
390 - state.detached_from);
391 - }
392 - else
393 - strbuf_addstr(&desc, _("(no branch)"));
394 - free(state.branch);
395 - free(state.onto);
396 - free(state.detached_from);
397 - return strbuf_detach(&desc, NULL);
398 -}
399 -
367 static void format_and_print_ref_item(struct ref_array_item *item, int maxwidth,
368 struct ref_filter *filter, const char *remote_prefix)
369 {
ref-filter.c
+36 -2
@@ -14,6 +14,7 @@
14 #include "git-compat-util.h"
15 #include "version.h"
16 #include "trailer.h"
17 +#include "wt-status.h"
18
19 typedef enum { FIELD_STR, FIELD_ULONG, FIELD_TIME } cmp_type;
20 typedef enum { COMPARE_EQUAL, COMPARE_UNEQUAL, COMPARE_NONE } cmp_status;
@@ -1101,6 +1102,37 @@ static void fill_remote_ref_details(struct used_atom *atom, const char *refname,
1102 *s = refname;
1103 }
1104
1105 +char *get_head_description(void)
1106 +{
1107 + struct strbuf desc = STRBUF_INIT;
1108 + struct wt_status_state state;
1109 + memset(&state, 0, sizeof(state));
1110 + wt_status_get_state(&state, 1);
1111 + if (state.rebase_in_progress ||
1112 + state.rebase_interactive_in_progress)
1113 + strbuf_addf(&desc, _("(no branch, rebasing %s)"),
1114 + state.branch);
1115 + else if (state.bisect_in_progress)
1116 + strbuf_addf(&desc, _("(no branch, bisect started on %s)"),
1117 + state.branch);
1118 + else if (state.detached_from) {
1119 + /* TRANSLATORS: make sure these match _("HEAD detached at ")
1120 + and _("HEAD detached from ") in wt-status.c */
1121 + if (state.detached_at)
1122 + strbuf_addf(&desc, _("(HEAD detached at %s)"),
1123 + state.detached_from);
1124 + else
1125 + strbuf_addf(&desc, _("(HEAD detached from %s)"),
1126 + state.detached_from);
1127 + }
1128 + else
1129 + strbuf_addstr(&desc, _("(no branch)"));
1130 + free(state.branch);
1131 + free(state.onto);
1132 + free(state.detached_from);
1133 + return strbuf_detach(&desc, NULL);
1134 +}
1135 +
1136 /*
1137 * Parse the object referred by ref, and grab needed value.
1138 */
@@ -1140,9 +1172,11 @@ static void populate_value(struct ref_array_item *ref)
1172 name++;
1173 }
1174
1143 - if (starts_with(name, "refname"))
1175 + if (starts_with(name, "refname")) {
1176 refname = ref->refname;
1145 - else if (starts_with(name, "symref"))
1177 + if (ref->kind & FILTER_REFS_DETACHED_HEAD)
1178 + refname = get_head_description();
1179 + } else if (starts_with(name, "symref"))
1180 refname = ref->symref ? ref->symref : "";
1181 else if (starts_with(name, "upstream")) {
1182 const char *branch_name;
ref-filter.h
+2
@@ -108,5 +108,7 @@ int parse_opt_ref_sorting(const struct option *opt, const char *arg, int unset);
108 struct ref_sorting *ref_default_sorting(void);
109 /* Function to parse --merged and --no-merged options */
110 int parse_opt_merge_filter(const struct option *opt, const char *arg, int unset);
111 +/* Get the current HEAD's description */
112 +char *get_head_description(void);
113
114 #endif /* REF_FILTER_H */