Skip to content

Commit d4919bb

Browse files
KarthikNayakgitster
authored andcommitted
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 <[email protected]> Mentored-by: Matthieu Moy <[email protected]> Signed-off-by: Karthik Nayak <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 42d0eb0 commit d4919bb

File tree

3 files changed

+38
-35
lines changed

3 files changed

+38
-35
lines changed

builtin/branch.c

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -364,39 +364,6 @@ static void add_verbose_info(struct strbuf *out, struct ref_array_item *item,
364364
strbuf_release(&subject);
365365
}
366366

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-
400367
static void format_and_print_ref_item(struct ref_array_item *item, int maxwidth,
401368
struct ref_filter *filter, const char *remote_prefix)
402369
{

ref-filter.c

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "git-compat-util.h"
1515
#include "version.h"
1616
#include "trailer.h"
17+
#include "wt-status.h"
1718

1819
typedef enum { FIELD_STR, FIELD_ULONG, FIELD_TIME } cmp_type;
1920
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,
11011102
*s = refname;
11021103
}
11031104

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+
11041136
/*
11051137
* Parse the object referred by ref, and grab needed value.
11061138
*/
@@ -1140,9 +1172,11 @@ static void populate_value(struct ref_array_item *ref)
11401172
name++;
11411173
}
11421174

1143-
if (starts_with(name, "refname"))
1175+
if (starts_with(name, "refname")) {
11441176
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"))
11461180
refname = ref->symref ? ref->symref : "";
11471181
else if (starts_with(name, "upstream")) {
11481182
const char *branch_name;

ref-filter.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,5 +108,7 @@ int parse_opt_ref_sorting(const struct option *opt, const char *arg, int unset);
108108
struct ref_sorting *ref_default_sorting(void);
109109
/* Function to parse --merged and --no-merged options */
110110
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);
111113

112114
#endif /* REF_FILTER_H */

0 commit comments

Comments
 (0)