1
0
Fork 0
mirror of https://github.com/git/git.git synced 2024-05-08 08:36:30 +02:00

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
This commit is contained in:
Junio C Hamano 2020-10-05 14:01:52 -07:00
commit 07601b5b36
5 changed files with 70 additions and 66 deletions

View File

@ -1557,9 +1557,7 @@ char *get_head_description(void)
strbuf_addstr(&desc, _("no branch"));
strbuf_addch(&desc, ')');
free(state.branch);
free(state.onto);
free(state.detached_from);
wt_status_state_free_buffers(&state);
return strbuf_detach(&desc, NULL);
}

View File

@ -21,7 +21,7 @@ void free_worktrees(struct worktree **worktrees)
}
/**
* Update head_sha1, head_ref and is_detached of the given worktree
* Update head_oid, head_ref and is_detached of the given worktree
*/
static void add_head_info(struct worktree *wt)
{
@ -352,13 +352,12 @@ int is_worktree_being_rebased(const struct worktree *wt,
memset(&state, 0, sizeof(state));
found_rebase = wt_status_check_rebase(wt, &state) &&
((state.rebase_in_progress ||
state.rebase_interactive_in_progress) &&
state.branch &&
starts_with(target, "refs/heads/") &&
!strcmp(state.branch, target + strlen("refs/heads/")));
free(state.branch);
free(state.onto);
(state.rebase_in_progress ||
state.rebase_interactive_in_progress) &&
state.branch &&
skip_prefix(target, "refs/heads/", &target) &&
!strcmp(state.branch, target);
wt_status_state_free_buffers(&state);
return found_rebase;
}
@ -366,15 +365,15 @@ int is_worktree_being_bisected(const struct worktree *wt,
const char *target)
{
struct wt_status_state state;
int found_rebase;
int found_bisect;
memset(&state, 0, sizeof(state));
found_rebase = wt_status_check_bisect(wt, &state) &&
state.branch &&
starts_with(target, "refs/heads/") &&
!strcmp(state.branch, target + strlen("refs/heads/"));
free(state.branch);
return found_rebase;
found_bisect = wt_status_check_bisect(wt, &state) &&
state.branch &&
skip_prefix(target, "refs/heads/", &target) &&
!strcmp(state.branch, target);
wt_status_state_free_buffers(&state);
return found_bisect;
}
/*
@ -537,18 +536,10 @@ void strbuf_worktree_ref(const struct worktree *wt,
strbuf_addstr(sb, refname);
}
const char *worktree_ref(const struct worktree *wt, const char *refname)
{
static struct strbuf sb = STRBUF_INIT;
strbuf_reset(&sb);
strbuf_worktree_ref(wt, &sb, refname);
return sb.buf;
}
int other_head_refs(each_ref_fn fn, void *cb_data)
{
struct worktree **worktrees, **p;
struct strbuf refname = STRBUF_INIT;
int ret = 0;
worktrees = get_worktrees();
@ -560,15 +551,18 @@ int other_head_refs(each_ref_fn fn, void *cb_data)
if (wt->is_current)
continue;
strbuf_reset(&refname);
strbuf_worktree_ref(wt, &refname, "HEAD");
if (!refs_read_ref_full(get_main_ref_store(the_repository),
worktree_ref(wt, "HEAD"),
refname.buf,
RESOLVE_REF_READING,
&oid, &flag))
ret = fn(worktree_ref(wt, "HEAD"), &oid, flag, cb_data);
ret = fn(refname.buf, &oid, flag, cb_data);
if (ret)
break;
}
free_worktrees(worktrees);
strbuf_release(&refname);
return ret;
}

View File

@ -159,11 +159,4 @@ void strbuf_worktree_ref(const struct worktree *wt,
struct strbuf *sb,
const char *refname);
/*
* Return a refname suitable for access from the current ref
* store. The result will be destroyed at the next call.
*/
const char *worktree_ref(const struct worktree *wt,
const char *refname);
#endif

View File

@ -776,9 +776,14 @@ void wt_status_collect(struct wt_status *s)
void wt_status_collect_free_buffers(struct wt_status *s)
{
free(s->state.branch);
free(s->state.onto);
free(s->state.detached_from);
wt_status_state_free_buffers(&s->state);
}
void wt_status_state_free_buffers(struct wt_status_state *state)
{
FREE_AND_NULL(state->branch);
FREE_AND_NULL(state->onto);
FREE_AND_NULL(state->detached_from);
}
static void wt_longstatus_print_unmerged(struct wt_status *s)
@ -1225,7 +1230,7 @@ static int split_commit_in_progress(struct wt_status *s)
* The function assumes that the line does not contain useless spaces
* before or after the command.
*/
static void abbrev_sha1_in_line(struct strbuf *line)
static void abbrev_oid_in_line(struct strbuf *line)
{
struct strbuf **split;
int i;
@ -1275,7 +1280,7 @@ static int read_rebase_todolist(const char *fname, struct string_list *lines)
strbuf_trim(&line);
if (!line.len)
continue;
abbrev_sha1_in_line(&line);
abbrev_oid_in_line(&line);
string_list_append(lines, line.buf);
}
fclose(f);
@ -1568,9 +1573,9 @@ static void wt_status_get_detached_from(struct repository *r,
}
if (dwim_ref(cb.buf.buf, cb.buf.len, &oid, &ref, 1) == 1 &&
/* sha1 is a commit? match without further lookup */
/* oid is a commit? match without further lookup */
(oideq(&cb.noid, &oid) ||
/* perhaps sha1 is a tag, try to dereference to a commit */
/* perhaps oid is a tag, try to dereference to a commit */
((commit = lookup_commit_reference_gently(r, &oid, 1)) != NULL &&
oideq(&cb.noid, &commit->object.oid)))) {
const char *from = ref;
@ -1799,29 +1804,36 @@ static void wt_longstatus_print(struct wt_status *s)
; /* nothing */
else if (s->workdir_dirty) {
if (s->hints)
printf(_("no changes added to commit "
"(use \"git add\" and/or \"git commit -a\")\n"));
fprintf(s->fp, _("no changes added to commit "
"(use \"git add\" and/or "
"\"git commit -a\")\n"));
else
printf(_("no changes added to commit\n"));
fprintf(s->fp, _("no changes added to "
"commit\n"));
} else if (s->untracked.nr) {
if (s->hints)
printf(_("nothing added to commit but untracked files "
"present (use \"git add\" to track)\n"));
fprintf(s->fp, _("nothing added to commit but "
"untracked files present (use "
"\"git add\" to track)\n"));
else
printf(_("nothing added to commit but untracked files present\n"));
fprintf(s->fp, _("nothing added to commit but "
"untracked files present\n"));
} else if (s->is_initial) {
if (s->hints)
printf(_("nothing to commit (create/copy files "
"and use \"git add\" to track)\n"));
fprintf(s->fp, _("nothing to commit (create/"
"copy files and use \"git "
"add\" to track)\n"));
else
printf(_("nothing to commit\n"));
fprintf(s->fp, _("nothing to commit\n"));
} else if (!s->show_untracked_files) {
if (s->hints)
printf(_("nothing to commit (use -u to show untracked files)\n"));
fprintf(s->fp, _("nothing to commit (use -u to "
"show untracked files)\n"));
else
printf(_("nothing to commit\n"));
fprintf(s->fp, _("nothing to commit\n"));
} else
printf(_("nothing to commit, working tree clean\n"));
fprintf(s->fp, _("nothing to commit, working tree "
"clean\n"));
}
if(s->show_stash)
wt_longstatus_print_stash_summary(s);
@ -1844,12 +1856,12 @@ static void wt_shortstatus_unmerged(struct string_list_item *it,
}
color_fprintf(s->fp, color(WT_STATUS_UNMERGED, s), "%s", how);
if (s->null_termination) {
fprintf(stdout, " %s%c", it->string, 0);
fprintf(s->fp, " %s%c", it->string, 0);
} else {
struct strbuf onebuf = STRBUF_INIT;
const char *one;
one = quote_path(it->string, s->prefix, &onebuf, QUOTE_PATH_QUOTE_SP);
printf(" %s\n", one);
fprintf(s->fp, " %s\n", one);
strbuf_release(&onebuf);
}
}
@ -1862,16 +1874,16 @@ static void wt_shortstatus_status(struct string_list_item *it,
if (d->index_status)
color_fprintf(s->fp, color(WT_STATUS_UPDATED, s), "%c", d->index_status);
else
putchar(' ');
fputc(' ', s->fp);
if (d->worktree_status)
color_fprintf(s->fp, color(WT_STATUS_CHANGED, s), "%c", d->worktree_status);
else
putchar(' ');
putchar(' ');
fputc(' ', s->fp);
fputc(' ', s->fp);
if (s->null_termination) {
fprintf(stdout, "%s%c", it->string, 0);
fprintf(s->fp, "%s%c", it->string, 0);
if (d->rename_source)
fprintf(stdout, "%s%c", d->rename_source, 0);
fprintf(s->fp, "%s%c", d->rename_source, 0);
} else {
struct strbuf onebuf = STRBUF_INIT;
const char *one;
@ -1879,11 +1891,11 @@ static void wt_shortstatus_status(struct string_list_item *it,
if (d->rename_source) {
one = quote_path(d->rename_source, s->prefix, &onebuf,
QUOTE_PATH_QUOTE_SP);
printf("%s -> ", one);
fprintf(s->fp, "%s -> ", one);
strbuf_release(&onebuf);
}
one = quote_path(it->string, s->prefix, &onebuf, QUOTE_PATH_QUOTE_SP);
printf("%s\n", one);
fprintf(s->fp, "%s\n", one);
strbuf_release(&onebuf);
}
}
@ -1892,13 +1904,13 @@ static void wt_shortstatus_other(struct string_list_item *it,
struct wt_status *s, const char *sign)
{
if (s->null_termination) {
fprintf(stdout, "%s %s%c", sign, it->string, 0);
fprintf(s->fp, "%s %s%c", sign, it->string, 0);
} else {
struct strbuf onebuf = STRBUF_INIT;
const char *one;
one = quote_path(it->string, s->prefix, &onebuf, QUOTE_PATH_QUOTE_SP);
color_fprintf(s->fp, color(WT_STATUS_UNTRACKED, s), "%s", sign);
printf(" %s\n", one);
fprintf(s->fp, " %s\n", one);
strbuf_release(&onebuf);
}
}

View File

@ -151,7 +151,14 @@ void wt_status_add_cut_line(FILE *fp);
void wt_status_prepare(struct repository *r, struct wt_status *s);
void wt_status_print(struct wt_status *s);
void wt_status_collect(struct wt_status *s);
/*
* Frees the buffers allocated by wt_status_collect.
*/
void wt_status_collect_free_buffers(struct wt_status *s);
/*
* Frees the buffers of the wt_status_state.
*/
void wt_status_state_free_buffers(struct wt_status_state *s);
void wt_status_get_state(struct repository *repo,
struct wt_status_state *state,
int get_detached_from);