Skip to content

Commit 07601b5

Browse files
committed
Merge branch 'ma/worktree-cleanups'
Code clean-up. * ma/worktree-cleanups: worktree: use skip_prefix to parse target worktree: rename copy-pasted variable worktree: update renamed variable in comment worktree: inline `worktree_ref()` into its only caller wt-status: introduce wt_status_state_free_buffers() wt-status: print to s->fp, not stdout wt-status: replace sha1 mentions with oid
2 parents 34415c7 + a46d1f7 commit 07601b5

File tree

5 files changed

+70
-66
lines changed

5 files changed

+70
-66
lines changed

ref-filter.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1557,9 +1557,7 @@ char *get_head_description(void)
15571557
strbuf_addstr(&desc, _("no branch"));
15581558
strbuf_addch(&desc, ')');
15591559

1560-
free(state.branch);
1561-
free(state.onto);
1562-
free(state.detached_from);
1560+
wt_status_state_free_buffers(&state);
15631561
return strbuf_detach(&desc, NULL);
15641562
}
15651563

worktree.c

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ void free_worktrees(struct worktree **worktrees)
2121
}
2222

2323
/**
24-
* Update head_sha1, head_ref and is_detached of the given worktree
24+
* Update head_oid, head_ref and is_detached of the given worktree
2525
*/
2626
static void add_head_info(struct worktree *wt)
2727
{
@@ -352,29 +352,28 @@ int is_worktree_being_rebased(const struct worktree *wt,
352352

353353
memset(&state, 0, sizeof(state));
354354
found_rebase = wt_status_check_rebase(wt, &state) &&
355-
((state.rebase_in_progress ||
356-
state.rebase_interactive_in_progress) &&
357-
state.branch &&
358-
starts_with(target, "refs/heads/") &&
359-
!strcmp(state.branch, target + strlen("refs/heads/")));
360-
free(state.branch);
361-
free(state.onto);
355+
(state.rebase_in_progress ||
356+
state.rebase_interactive_in_progress) &&
357+
state.branch &&
358+
skip_prefix(target, "refs/heads/", &target) &&
359+
!strcmp(state.branch, target);
360+
wt_status_state_free_buffers(&state);
362361
return found_rebase;
363362
}
364363

365364
int is_worktree_being_bisected(const struct worktree *wt,
366365
const char *target)
367366
{
368367
struct wt_status_state state;
369-
int found_rebase;
368+
int found_bisect;
370369

371370
memset(&state, 0, sizeof(state));
372-
found_rebase = wt_status_check_bisect(wt, &state) &&
373-
state.branch &&
374-
starts_with(target, "refs/heads/") &&
375-
!strcmp(state.branch, target + strlen("refs/heads/"));
376-
free(state.branch);
377-
return found_rebase;
371+
found_bisect = wt_status_check_bisect(wt, &state) &&
372+
state.branch &&
373+
skip_prefix(target, "refs/heads/", &target) &&
374+
!strcmp(state.branch, target);
375+
wt_status_state_free_buffers(&state);
376+
return found_bisect;
378377
}
379378

380379
/*
@@ -537,18 +536,10 @@ void strbuf_worktree_ref(const struct worktree *wt,
537536
strbuf_addstr(sb, refname);
538537
}
539538

540-
const char *worktree_ref(const struct worktree *wt, const char *refname)
541-
{
542-
static struct strbuf sb = STRBUF_INIT;
543-
544-
strbuf_reset(&sb);
545-
strbuf_worktree_ref(wt, &sb, refname);
546-
return sb.buf;
547-
}
548-
549539
int other_head_refs(each_ref_fn fn, void *cb_data)
550540
{
551541
struct worktree **worktrees, **p;
542+
struct strbuf refname = STRBUF_INIT;
552543
int ret = 0;
553544

554545
worktrees = get_worktrees();
@@ -560,15 +551,18 @@ int other_head_refs(each_ref_fn fn, void *cb_data)
560551
if (wt->is_current)
561552
continue;
562553

554+
strbuf_reset(&refname);
555+
strbuf_worktree_ref(wt, &refname, "HEAD");
563556
if (!refs_read_ref_full(get_main_ref_store(the_repository),
564-
worktree_ref(wt, "HEAD"),
557+
refname.buf,
565558
RESOLVE_REF_READING,
566559
&oid, &flag))
567-
ret = fn(worktree_ref(wt, "HEAD"), &oid, flag, cb_data);
560+
ret = fn(refname.buf, &oid, flag, cb_data);
568561
if (ret)
569562
break;
570563
}
571564
free_worktrees(worktrees);
565+
strbuf_release(&refname);
572566
return ret;
573567
}
574568

worktree.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,4 @@ void strbuf_worktree_ref(const struct worktree *wt,
159159
struct strbuf *sb,
160160
const char *refname);
161161

162-
/*
163-
* Return a refname suitable for access from the current ref
164-
* store. The result will be destroyed at the next call.
165-
*/
166-
const char *worktree_ref(const struct worktree *wt,
167-
const char *refname);
168-
169162
#endif

wt-status.c

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -776,9 +776,14 @@ void wt_status_collect(struct wt_status *s)
776776

777777
void wt_status_collect_free_buffers(struct wt_status *s)
778778
{
779-
free(s->state.branch);
780-
free(s->state.onto);
781-
free(s->state.detached_from);
779+
wt_status_state_free_buffers(&s->state);
780+
}
781+
782+
void wt_status_state_free_buffers(struct wt_status_state *state)
783+
{
784+
FREE_AND_NULL(state->branch);
785+
FREE_AND_NULL(state->onto);
786+
FREE_AND_NULL(state->detached_from);
782787
}
783788

784789
static void wt_longstatus_print_unmerged(struct wt_status *s)
@@ -1225,7 +1230,7 @@ static int split_commit_in_progress(struct wt_status *s)
12251230
* The function assumes that the line does not contain useless spaces
12261231
* before or after the command.
12271232
*/
1228-
static void abbrev_sha1_in_line(struct strbuf *line)
1233+
static void abbrev_oid_in_line(struct strbuf *line)
12291234
{
12301235
struct strbuf **split;
12311236
int i;
@@ -1275,7 +1280,7 @@ static int read_rebase_todolist(const char *fname, struct string_list *lines)
12751280
strbuf_trim(&line);
12761281
if (!line.len)
12771282
continue;
1278-
abbrev_sha1_in_line(&line);
1283+
abbrev_oid_in_line(&line);
12791284
string_list_append(lines, line.buf);
12801285
}
12811286
fclose(f);
@@ -1568,9 +1573,9 @@ static void wt_status_get_detached_from(struct repository *r,
15681573
}
15691574

15701575
if (dwim_ref(cb.buf.buf, cb.buf.len, &oid, &ref, 1) == 1 &&
1571-
/* sha1 is a commit? match without further lookup */
1576+
/* oid is a commit? match without further lookup */
15721577
(oideq(&cb.noid, &oid) ||
1573-
/* perhaps sha1 is a tag, try to dereference to a commit */
1578+
/* perhaps oid is a tag, try to dereference to a commit */
15741579
((commit = lookup_commit_reference_gently(r, &oid, 1)) != NULL &&
15751580
oideq(&cb.noid, &commit->object.oid)))) {
15761581
const char *from = ref;
@@ -1799,29 +1804,36 @@ static void wt_longstatus_print(struct wt_status *s)
17991804
; /* nothing */
18001805
else if (s->workdir_dirty) {
18011806
if (s->hints)
1802-
printf(_("no changes added to commit "
1803-
"(use \"git add\" and/or \"git commit -a\")\n"));
1807+
fprintf(s->fp, _("no changes added to commit "
1808+
"(use \"git add\" and/or "
1809+
"\"git commit -a\")\n"));
18041810
else
1805-
printf(_("no changes added to commit\n"));
1811+
fprintf(s->fp, _("no changes added to "
1812+
"commit\n"));
18061813
} else if (s->untracked.nr) {
18071814
if (s->hints)
1808-
printf(_("nothing added to commit but untracked files "
1809-
"present (use \"git add\" to track)\n"));
1815+
fprintf(s->fp, _("nothing added to commit but "
1816+
"untracked files present (use "
1817+
"\"git add\" to track)\n"));
18101818
else
1811-
printf(_("nothing added to commit but untracked files present\n"));
1819+
fprintf(s->fp, _("nothing added to commit but "
1820+
"untracked files present\n"));
18121821
} else if (s->is_initial) {
18131822
if (s->hints)
1814-
printf(_("nothing to commit (create/copy files "
1815-
"and use \"git add\" to track)\n"));
1823+
fprintf(s->fp, _("nothing to commit (create/"
1824+
"copy files and use \"git "
1825+
"add\" to track)\n"));
18161826
else
1817-
printf(_("nothing to commit\n"));
1827+
fprintf(s->fp, _("nothing to commit\n"));
18181828
} else if (!s->show_untracked_files) {
18191829
if (s->hints)
1820-
printf(_("nothing to commit (use -u to show untracked files)\n"));
1830+
fprintf(s->fp, _("nothing to commit (use -u to "
1831+
"show untracked files)\n"));
18211832
else
1822-
printf(_("nothing to commit\n"));
1833+
fprintf(s->fp, _("nothing to commit\n"));
18231834
} else
1824-
printf(_("nothing to commit, working tree clean\n"));
1835+
fprintf(s->fp, _("nothing to commit, working tree "
1836+
"clean\n"));
18251837
}
18261838
if(s->show_stash)
18271839
wt_longstatus_print_stash_summary(s);
@@ -1844,12 +1856,12 @@ static void wt_shortstatus_unmerged(struct string_list_item *it,
18441856
}
18451857
color_fprintf(s->fp, color(WT_STATUS_UNMERGED, s), "%s", how);
18461858
if (s->null_termination) {
1847-
fprintf(stdout, " %s%c", it->string, 0);
1859+
fprintf(s->fp, " %s%c", it->string, 0);
18481860
} else {
18491861
struct strbuf onebuf = STRBUF_INIT;
18501862
const char *one;
18511863
one = quote_path(it->string, s->prefix, &onebuf, QUOTE_PATH_QUOTE_SP);
1852-
printf(" %s\n", one);
1864+
fprintf(s->fp, " %s\n", one);
18531865
strbuf_release(&onebuf);
18541866
}
18551867
}
@@ -1862,28 +1874,28 @@ static void wt_shortstatus_status(struct string_list_item *it,
18621874
if (d->index_status)
18631875
color_fprintf(s->fp, color(WT_STATUS_UPDATED, s), "%c", d->index_status);
18641876
else
1865-
putchar(' ');
1877+
fputc(' ', s->fp);
18661878
if (d->worktree_status)
18671879
color_fprintf(s->fp, color(WT_STATUS_CHANGED, s), "%c", d->worktree_status);
18681880
else
1869-
putchar(' ');
1870-
putchar(' ');
1881+
fputc(' ', s->fp);
1882+
fputc(' ', s->fp);
18711883
if (s->null_termination) {
1872-
fprintf(stdout, "%s%c", it->string, 0);
1884+
fprintf(s->fp, "%s%c", it->string, 0);
18731885
if (d->rename_source)
1874-
fprintf(stdout, "%s%c", d->rename_source, 0);
1886+
fprintf(s->fp, "%s%c", d->rename_source, 0);
18751887
} else {
18761888
struct strbuf onebuf = STRBUF_INIT;
18771889
const char *one;
18781890

18791891
if (d->rename_source) {
18801892
one = quote_path(d->rename_source, s->prefix, &onebuf,
18811893
QUOTE_PATH_QUOTE_SP);
1882-
printf("%s -> ", one);
1894+
fprintf(s->fp, "%s -> ", one);
18831895
strbuf_release(&onebuf);
18841896
}
18851897
one = quote_path(it->string, s->prefix, &onebuf, QUOTE_PATH_QUOTE_SP);
1886-
printf("%s\n", one);
1898+
fprintf(s->fp, "%s\n", one);
18871899
strbuf_release(&onebuf);
18881900
}
18891901
}
@@ -1892,13 +1904,13 @@ static void wt_shortstatus_other(struct string_list_item *it,
18921904
struct wt_status *s, const char *sign)
18931905
{
18941906
if (s->null_termination) {
1895-
fprintf(stdout, "%s %s%c", sign, it->string, 0);
1907+
fprintf(s->fp, "%s %s%c", sign, it->string, 0);
18961908
} else {
18971909
struct strbuf onebuf = STRBUF_INIT;
18981910
const char *one;
18991911
one = quote_path(it->string, s->prefix, &onebuf, QUOTE_PATH_QUOTE_SP);
19001912
color_fprintf(s->fp, color(WT_STATUS_UNTRACKED, s), "%s", sign);
1901-
printf(" %s\n", one);
1913+
fprintf(s->fp, " %s\n", one);
19021914
strbuf_release(&onebuf);
19031915
}
19041916
}

wt-status.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,14 @@ void wt_status_add_cut_line(FILE *fp);
151151
void wt_status_prepare(struct repository *r, struct wt_status *s);
152152
void wt_status_print(struct wt_status *s);
153153
void wt_status_collect(struct wt_status *s);
154+
/*
155+
* Frees the buffers allocated by wt_status_collect.
156+
*/
154157
void wt_status_collect_free_buffers(struct wt_status *s);
158+
/*
159+
* Frees the buffers of the wt_status_state.
160+
*/
161+
void wt_status_state_free_buffers(struct wt_status_state *s);
155162
void wt_status_get_state(struct repository *repo,
156163
struct wt_status_state *state,
157164
int get_detached_from);

0 commit comments

Comments
 (0)