summaryrefslogtreecommitdiff
path: root/builtin/show-branch.c
diff options
context:
space:
mode:
Diffstat (limited to 'builtin/show-branch.c')
-rw-r--r--builtin/show-branch.c90
1 files changed, 60 insertions, 30 deletions
diff --git a/builtin/show-branch.c b/builtin/show-branch.c
index b01ec761d2..cd6bdf63bc 100644
--- a/builtin/show-branch.c
+++ b/builtin/show-branch.c
@@ -1,3 +1,4 @@
+#define USE_THE_REPOSITORY_VARIABLE
#include "builtin.h"
#include "config.h"
#include "environment.h"
@@ -10,7 +11,7 @@
#include "strvec.h"
#include "object-name.h"
#include "parse-options.h"
-#include "repository.h"
+
#include "dir.h"
#include "commit-slab.h"
#include "date.h"
@@ -410,7 +411,7 @@ static int append_ref(const char *refname, const struct object_id *oid,
return 0;
}
-static int append_head_ref(const char *refname, const struct object_id *oid,
+static int append_head_ref(const char *refname, const char *referent UNUSED, const struct object_id *oid,
int flag UNUSED, void *cb_data UNUSED)
{
struct object_id tmp;
@@ -425,7 +426,7 @@ static int append_head_ref(const char *refname, const struct object_id *oid,
return append_ref(refname + ofs, oid, 0);
}
-static int append_remote_ref(const char *refname, const struct object_id *oid,
+static int append_remote_ref(const char *refname, const char *referent UNUSED, const struct object_id *oid,
int flag UNUSED, void *cb_data UNUSED)
{
struct object_id tmp;
@@ -451,7 +452,7 @@ static int append_tag_ref(const char *refname, const struct object_id *oid,
static const char *match_ref_pattern = NULL;
static int match_ref_slash = 0;
-static int append_matching_ref(const char *refname, const struct object_id *oid,
+static int append_matching_ref(const char *refname, const char *referent UNUSED, const struct object_id *oid,
int flag, void *cb_data)
{
/* we want to allow pattern hold/<asterisk> to show all
@@ -468,7 +469,7 @@ static int append_matching_ref(const char *refname, const struct object_id *oid,
if (wildmatch(match_ref_pattern, tail, 0))
return 0;
if (starts_with(refname, "refs/heads/"))
- return append_head_ref(refname, oid, flag, cb_data);
+ return append_head_ref(refname, NULL, oid, flag, cb_data);
if (starts_with(refname, "refs/tags/"))
return append_tag_ref(refname, oid, flag, cb_data);
return append_ref(refname, oid, 0);
@@ -479,13 +480,15 @@ static void snarf_refs(int head, int remotes)
if (head) {
int orig_cnt = ref_name_cnt;
- for_each_ref(append_head_ref, NULL);
+ refs_for_each_ref(get_main_ref_store(the_repository),
+ append_head_ref, NULL);
sort_ref_range(orig_cnt, ref_name_cnt);
}
if (remotes) {
int orig_cnt = ref_name_cnt;
- for_each_ref(append_remote_ref, NULL);
+ refs_for_each_ref(get_main_ref_store(the_repository),
+ append_remote_ref, NULL);
sort_ref_range(orig_cnt, ref_name_cnt);
}
}
@@ -500,14 +503,14 @@ static int rev_is_head(const char *head, const char *name)
return !strcmp(head, name);
}
-static int show_merge_base(struct commit_list *seen, int num_rev)
+static int show_merge_base(const struct commit_list *seen, int num_rev)
{
int all_mask = ((1u << (REV_SHIFT + num_rev)) - 1);
int all_revs = all_mask & ~((1u << REV_SHIFT) - 1);
int exit_status = 1;
- while (seen) {
- struct commit *commit = pop_commit(&seen);
+ for (const struct commit_list *s = seen; s; s = s->next) {
+ struct commit *commit = s->item;
int flags = commit->object.flags & all_mask;
if (!(flags & UNINTERESTING) &&
((flags & all_revs) == all_revs)) {
@@ -549,7 +552,8 @@ static void append_one_rev(const char *av)
match_ref_pattern = av;
match_ref_slash = count_slashes(av);
- for_each_ref(append_matching_ref, NULL);
+ refs_for_each_ref(get_main_ref_store(the_repository),
+ append_matching_ref, NULL);
if (saved_matches == ref_name_cnt &&
ref_name_cnt < MAX_REVS)
error(_("no matching refs with %s"), av);
@@ -629,10 +633,13 @@ static int parse_reflog_param(const struct option *opt, const char *arg,
return 0;
}
-int cmd_show_branch(int ac, const char **av, const char *prefix)
+int cmd_show_branch(int ac,
+ const char **av,
+ const char *prefix,
+ struct repository *repo UNUSED)
{
struct commit *rev[MAX_REVS], *commit;
- char *reflog_msg[MAX_REVS];
+ char *reflog_msg[MAX_REVS] = {0};
struct commit_list *list = NULL, *seen = NULL;
unsigned int rev_mask[MAX_REVS];
int num_rev, i, extra = 0;
@@ -689,6 +696,8 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
parse_reflog_param),
OPT_END()
};
+ const char **args_copy = NULL;
+ int ret;
init_commit_name_slab(&name_slab);
@@ -696,8 +705,9 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
/* If nothing is specified, try the default first */
if (ac == 1 && default_args.nr) {
+ DUP_ARRAY(args_copy, default_args.v, default_args.nr);
ac = default_args.nr;
- av = default_args.v;
+ av = args_copy;
}
ac = parse_options(ac, av, prefix, builtin_show_branch_options,
@@ -740,9 +750,11 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
if (ac == 0) {
static const char *fake_av[2];
- fake_av[0] = resolve_refdup("HEAD",
- RESOLVE_REF_READING, &oid,
- NULL);
+ fake_av[0] = refs_resolve_refdup(get_main_ref_store(the_repository),
+ "HEAD",
+ RESOLVE_REF_READING,
+ &oid,
+ NULL);
fake_av[1] = NULL;
av = fake_av;
ac = 1;
@@ -775,7 +787,7 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
}
for (i = 0; i < reflog; i++) {
- char *logmsg;
+ char *logmsg = NULL;
char *nth_desc;
const char *msg;
char *end;
@@ -785,6 +797,7 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
if (read_ref_at(get_main_ref_store(the_repository),
ref, flags, 0, base + i, &oid, &logmsg,
&timestamp, &tz, NULL)) {
+ free(logmsg);
reflog = i;
break;
}
@@ -815,8 +828,9 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
snarf_refs(all_heads, all_remotes);
}
- head = resolve_refdup("HEAD", RESOLVE_REF_READING,
- &head_oid, NULL);
+ head = refs_resolve_refdup(get_main_ref_store(the_repository), "HEAD",
+ RESOLVE_REF_READING,
+ &head_oid, NULL);
if (with_current_branch && head) {
int has_head = 0;
@@ -836,7 +850,8 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
if (!ref_name_cnt) {
fprintf(stderr, "No revs to be shown.\n");
- exit(0);
+ ret = 0;
+ goto out;
}
for (num_rev = 0; ref_name[num_rev]; num_rev++) {
@@ -873,11 +888,15 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
commit_list_sort_by_date(&seen);
- if (merge_base)
- return show_merge_base(seen, num_rev);
+ if (merge_base) {
+ ret = show_merge_base(seen, num_rev);
+ goto out;
+ }
- if (independent)
- return show_independent(rev, num_rev, rev_mask);
+ if (independent) {
+ ret = show_independent(rev, num_rev, rev_mask);
+ goto out;
+ }
/* Show list; --more=-1 means list-only */
if (1 < num_rev || extra < 0) {
@@ -913,8 +932,10 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
putchar('\n');
}
}
- if (extra < 0)
- exit(0);
+ if (extra < 0) {
+ ret = 0;
+ goto out;
+ }
/* Sort topologically */
sort_in_topological_order(&seen, sort_order);
@@ -926,8 +947,8 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
all_mask = ((1u << (REV_SHIFT + num_rev)) - 1);
all_revs = all_mask & ~((1u << REV_SHIFT) - 1);
- while (seen) {
- struct commit *commit = pop_commit(&seen);
+ for (struct commit_list *l = seen; l; l = l->next) {
+ struct commit *commit = l->item;
int this_flag = commit->object.flags;
int is_merge_point = ((this_flag & all_revs) == all_revs);
@@ -967,6 +988,15 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
if (shown_merge_point && --extra < 0)
break;
}
+
+ ret = 0;
+
+out:
+ for (size_t i = 0; i < ARRAY_SIZE(reflog_msg); i++)
+ free(reflog_msg[i]);
+ free_commit_list(seen);
+ free_commit_list(list);
+ free(args_copy);
free(head);
- return 0;
+ return ret;
}