ls-files: factor out tag calculation

Signed-off-by: Brandon Williams <bmwill@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Brandon Williams committed Jun 12, 2017 at 15:14 UTC a84f3e59ebde9e891275ef8325c432db6bdf950c
1 file changed +25 -16
builtin/ls-files.c
+25 -16
@@ -93,6 +93,30 @@ static void write_name(const char *name)
93 strbuf_release(&full_name);
94 }
95
96 +static const char *get_tag(const struct cache_entry *ce, const char *tag)
97 +{
98 + static char alttag[4];
99 +
100 + if (tag && *tag && show_valid_bit && (ce->ce_flags & CE_VALID)) {
101 + memcpy(alttag, tag, 3);
102 +
103 + if (isalpha(tag[0])) {
104 + alttag[0] = tolower(tag[0]);
105 + } else if (tag[0] == '?') {
106 + alttag[0] = '!';
107 + } else {
108 + alttag[0] = 'v';
109 + alttag[1] = tag[0];
110 + alttag[2] = ' ';
111 + alttag[3] = 0;
112 + }
113 +
114 + tag = alttag;
115 + }
116 +
117 + return tag;
118 +}
119 +
120 static void print_debug(const struct cache_entry *ce)
121 {
122 if (debug_mode) {
@@ -264,22 +288,7 @@ static void show_ce_entry(const struct index_state *istate,
288 len, ps_matched,
289 S_ISDIR(ce->ce_mode) ||
290 S_ISGITLINK(ce->ce_mode))) {
267 - if (tag && *tag && show_valid_bit &&
268 - (ce->ce_flags & CE_VALID)) {
269 - static char alttag[4];
270 - memcpy(alttag, tag, 3);
271 - if (isalpha(tag[0]))
272 - alttag[0] = tolower(tag[0]);
273 - else if (tag[0] == '?')
274 - alttag[0] = '!';
275 - else {
276 - alttag[0] = 'v';
277 - alttag[1] = tag[0];
278 - alttag[2] = ' ';
279 - alttag[3] = 0;
280 - }
281 - tag = alttag;
282 - }
291 + tag = get_tag(ce, tag);
292
293 if (!show_stage) {
294 fputs(tag, stdout);