2005-08-21 17:51:10 +08:00
|
|
|
#include <stdlib.h>
|
2005-12-05 07:58:50 +08:00
|
|
|
#include <fnmatch.h>
|
2005-08-21 17:51:10 +08:00
|
|
|
#include "cache.h"
|
|
|
|
#include "commit.h"
|
|
|
|
#include "refs.h"
|
2006-05-23 20:15:35 +08:00
|
|
|
#include "builtin.h"
|
2005-08-21 17:51:10 +08:00
|
|
|
|
|
|
|
static const char show_branch_usage[] =
|
2006-07-07 10:21:47 +08:00
|
|
|
"git-show-branch [--sparse] [--current] [--all] [--heads] [--tags] [--topo-order] [--more=count | --list | --independent | --merge-base ] [--topics] [<refs>...]";
|
2005-08-21 17:51:10 +08:00
|
|
|
|
2006-08-16 01:23:48 +08:00
|
|
|
static int default_num;
|
|
|
|
static int default_alloc;
|
|
|
|
static const char **default_arg;
|
2006-01-10 05:29:23 +08:00
|
|
|
|
2005-08-21 17:51:10 +08:00
|
|
|
#define UNINTERESTING 01
|
|
|
|
|
|
|
|
#define REV_SHIFT 2
|
Shrink "struct object" a bit
This shrinks "struct object" by a small amount, by getting rid of the
"struct type *" pointer and replacing it with a 3-bit bitfield instead.
In addition, we merge the bitfields and the "flags" field, which
incidentally should also remove a useless 4-byte padding from the object
when in 64-bit mode.
Now, our "struct object" is still too damn large, but it's now less
obviously bloated, and of the remaining fields, only the "util" (which is
not used by most things) is clearly something that should be eventually
discarded.
This shrinks the "git-rev-list --all" memory use by about 2.5% on the
kernel archive (and, perhaps more importantly, on the larger mozilla
archive). That may not sound like much, but I suspect it's more on a
64-bit platform.
There are other remaining inefficiencies (the parent lists, for example,
probably have horrible malloc overhead), but this was pretty obvious.
Most of the patch is just changing the comparison of the "type" pointer
from one of the constant string pointers to the appropriate new TYPE_xxx
small integer constant.
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
Signed-off-by: Junio C Hamano <junkio@cox.net>
2006-06-15 07:45:13 +08:00
|
|
|
#define MAX_REVS (FLAG_BITS - REV_SHIFT) /* should not exceed bits_per_int - REV_SHIFT */
|
2005-08-21 17:51:10 +08:00
|
|
|
|
|
|
|
static struct commit *interesting(struct commit_list *list)
|
|
|
|
{
|
|
|
|
while (list) {
|
|
|
|
struct commit *commit = list->item;
|
|
|
|
list = list->next;
|
|
|
|
if (commit->object.flags & UNINTERESTING)
|
|
|
|
continue;
|
|
|
|
return commit;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct commit *pop_one_commit(struct commit_list **list_p)
|
|
|
|
{
|
|
|
|
struct commit *commit;
|
|
|
|
struct commit_list *list;
|
|
|
|
list = *list_p;
|
|
|
|
commit = list->item;
|
|
|
|
*list_p = list->next;
|
|
|
|
free(list);
|
|
|
|
return commit;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct commit_name {
|
2005-08-30 08:19:47 +08:00
|
|
|
const char *head_name; /* which head's ancestor? */
|
|
|
|
int generation; /* how many parents away from head_name */
|
2005-08-21 17:51:10 +08:00
|
|
|
};
|
|
|
|
|
2005-08-30 08:19:47 +08:00
|
|
|
/* Name the commit as nth generation ancestor of head_name;
|
2005-08-21 17:51:10 +08:00
|
|
|
* we count only the first-parent relationship for naming purposes.
|
|
|
|
*/
|
2005-08-30 08:19:47 +08:00
|
|
|
static void name_commit(struct commit *commit, const char *head_name, int nth)
|
2005-08-21 17:51:10 +08:00
|
|
|
{
|
|
|
|
struct commit_name *name;
|
2006-06-18 09:26:18 +08:00
|
|
|
if (!commit->util)
|
|
|
|
commit->util = xmalloc(sizeof(struct commit_name));
|
|
|
|
name = commit->util;
|
2005-08-30 08:19:47 +08:00
|
|
|
name->head_name = head_name;
|
2005-08-21 17:51:10 +08:00
|
|
|
name->generation = nth;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parent is the first parent of the commit. We may name it
|
2005-08-30 08:19:47 +08:00
|
|
|
* as (n+1)th generation ancestor of the same head_name as
|
2005-12-09 06:10:02 +08:00
|
|
|
* commit is nth generation ancestor of, if that generation
|
2005-08-21 17:51:10 +08:00
|
|
|
* number is better than the name it already has.
|
|
|
|
*/
|
|
|
|
static void name_parent(struct commit *commit, struct commit *parent)
|
|
|
|
{
|
2006-06-18 09:26:18 +08:00
|
|
|
struct commit_name *commit_name = commit->util;
|
|
|
|
struct commit_name *parent_name = parent->util;
|
2005-08-21 17:51:10 +08:00
|
|
|
if (!commit_name)
|
|
|
|
return;
|
|
|
|
if (!parent_name ||
|
|
|
|
commit_name->generation + 1 < parent_name->generation)
|
2005-08-30 08:19:47 +08:00
|
|
|
name_commit(parent, commit_name->head_name,
|
2005-08-21 17:51:10 +08:00
|
|
|
commit_name->generation + 1);
|
|
|
|
}
|
|
|
|
|
2005-08-30 08:19:47 +08:00
|
|
|
static int name_first_parent_chain(struct commit *c)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
while (c) {
|
|
|
|
struct commit *p;
|
2006-06-18 09:26:18 +08:00
|
|
|
if (!c->util)
|
2005-08-30 08:19:47 +08:00
|
|
|
break;
|
|
|
|
if (!c->parents)
|
|
|
|
break;
|
|
|
|
p = c->parents->item;
|
2006-06-18 09:26:18 +08:00
|
|
|
if (!p->util) {
|
2005-08-30 08:19:47 +08:00
|
|
|
name_parent(c, p);
|
|
|
|
i++;
|
|
|
|
}
|
2006-07-24 01:51:04 +08:00
|
|
|
else
|
|
|
|
break;
|
2005-08-30 08:19:47 +08:00
|
|
|
c = p;
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void name_commits(struct commit_list *list,
|
|
|
|
struct commit **rev,
|
|
|
|
char **ref_name,
|
|
|
|
int num_rev)
|
|
|
|
{
|
|
|
|
struct commit_list *cl;
|
|
|
|
struct commit *c;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* First give names to the given heads */
|
|
|
|
for (cl = list; cl; cl = cl->next) {
|
|
|
|
c = cl->item;
|
2006-06-18 09:26:18 +08:00
|
|
|
if (c->util)
|
2005-08-30 08:19:47 +08:00
|
|
|
continue;
|
|
|
|
for (i = 0; i < num_rev; i++) {
|
|
|
|
if (rev[i] == c) {
|
|
|
|
name_commit(c, ref_name[i], 0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Then commits on the first parent ancestry chain */
|
|
|
|
do {
|
|
|
|
i = 0;
|
|
|
|
for (cl = list; cl; cl = cl->next) {
|
|
|
|
i += name_first_parent_chain(cl->item);
|
|
|
|
}
|
|
|
|
} while (i);
|
|
|
|
|
|
|
|
/* Finally, any unnamed commits */
|
|
|
|
do {
|
|
|
|
i = 0;
|
|
|
|
for (cl = list; cl; cl = cl->next) {
|
|
|
|
struct commit_list *parents;
|
|
|
|
struct commit_name *n;
|
|
|
|
int nth;
|
|
|
|
c = cl->item;
|
2006-06-18 09:26:18 +08:00
|
|
|
if (!c->util)
|
2005-08-30 08:19:47 +08:00
|
|
|
continue;
|
2006-06-18 09:26:18 +08:00
|
|
|
n = c->util;
|
2005-08-30 08:19:47 +08:00
|
|
|
parents = c->parents;
|
|
|
|
nth = 0;
|
|
|
|
while (parents) {
|
|
|
|
struct commit *p = parents->item;
|
2005-10-12 06:22:48 +08:00
|
|
|
char newname[1000], *en;
|
2005-08-30 08:19:47 +08:00
|
|
|
parents = parents->next;
|
|
|
|
nth++;
|
2006-06-18 09:26:18 +08:00
|
|
|
if (p->util)
|
2005-08-30 08:19:47 +08:00
|
|
|
continue;
|
2005-10-12 06:22:48 +08:00
|
|
|
en = newname;
|
2005-09-25 14:33:02 +08:00
|
|
|
switch (n->generation) {
|
|
|
|
case 0:
|
2005-10-12 06:22:48 +08:00
|
|
|
en += sprintf(en, "%s", n->head_name);
|
2005-09-25 14:33:02 +08:00
|
|
|
break;
|
|
|
|
case 1:
|
2005-10-12 06:22:48 +08:00
|
|
|
en += sprintf(en, "%s^", n->head_name);
|
2005-09-25 14:33:02 +08:00
|
|
|
break;
|
|
|
|
default:
|
2005-10-12 06:22:48 +08:00
|
|
|
en += sprintf(en, "%s~%d",
|
|
|
|
n->head_name, n->generation);
|
|
|
|
break;
|
2005-09-25 14:33:02 +08:00
|
|
|
}
|
2005-10-12 06:22:48 +08:00
|
|
|
if (nth == 1)
|
|
|
|
en += sprintf(en, "^");
|
|
|
|
else
|
|
|
|
en += sprintf(en, "^%d", nth);
|
2005-08-30 08:19:47 +08:00
|
|
|
name_commit(p, strdup(newname), 0);
|
|
|
|
i++;
|
|
|
|
name_first_parent_chain(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (i);
|
|
|
|
}
|
|
|
|
|
2005-08-21 17:51:10 +08:00
|
|
|
static int mark_seen(struct commit *commit, struct commit_list **seen_p)
|
|
|
|
{
|
|
|
|
if (!commit->object.flags) {
|
2006-07-16 15:00:09 +08:00
|
|
|
commit_list_insert(commit, seen_p);
|
2005-08-21 17:51:10 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void join_revs(struct commit_list **list_p,
|
|
|
|
struct commit_list **seen_p,
|
|
|
|
int num_rev, int extra)
|
|
|
|
{
|
|
|
|
int all_mask = ((1u << (REV_SHIFT + num_rev)) - 1);
|
|
|
|
int all_revs = all_mask & ~((1u << REV_SHIFT) - 1);
|
|
|
|
|
|
|
|
while (*list_p) {
|
|
|
|
struct commit_list *parents;
|
2005-11-10 15:36:15 +08:00
|
|
|
int still_interesting = !!interesting(*list_p);
|
2005-08-21 17:51:10 +08:00
|
|
|
struct commit *commit = pop_one_commit(list_p);
|
|
|
|
int flags = commit->object.flags & all_mask;
|
|
|
|
|
2005-11-10 15:36:15 +08:00
|
|
|
if (!still_interesting && extra <= 0)
|
2005-08-21 17:51:10 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
mark_seen(commit, seen_p);
|
|
|
|
if ((flags & all_revs) == all_revs)
|
|
|
|
flags |= UNINTERESTING;
|
|
|
|
parents = commit->parents;
|
|
|
|
|
|
|
|
while (parents) {
|
|
|
|
struct commit *p = parents->item;
|
|
|
|
int this_flag = p->object.flags;
|
|
|
|
parents = parents->next;
|
|
|
|
if ((this_flag & flags) == flags)
|
|
|
|
continue;
|
2005-11-11 07:47:58 +08:00
|
|
|
if (!p->object.parsed)
|
|
|
|
parse_commit(p);
|
2005-08-21 17:51:10 +08:00
|
|
|
if (mark_seen(p, seen_p) && !still_interesting)
|
|
|
|
extra--;
|
|
|
|
p->object.flags |= flags;
|
|
|
|
insert_by_date(p, list_p);
|
|
|
|
}
|
|
|
|
}
|
2005-11-11 07:47:58 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Postprocess to complete well-poisoning.
|
|
|
|
*
|
|
|
|
* At this point we have all the commits we have seen in
|
2006-07-16 15:00:09 +08:00
|
|
|
* seen_p list. Mark anything that can be reached from
|
|
|
|
* uninteresting commits not interesting.
|
2005-11-11 07:47:58 +08:00
|
|
|
*/
|
|
|
|
for (;;) {
|
|
|
|
int changed = 0;
|
|
|
|
struct commit_list *s;
|
|
|
|
for (s = *seen_p; s; s = s->next) {
|
|
|
|
struct commit *c = s->item;
|
|
|
|
struct commit_list *parents;
|
|
|
|
|
|
|
|
if (((c->object.flags & all_revs) != all_revs) &&
|
|
|
|
!(c->object.flags & UNINTERESTING))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* The current commit is either a merge base or
|
|
|
|
* already uninteresting one. Mark its parents
|
|
|
|
* as uninteresting commits _only_ if they are
|
|
|
|
* already parsed. No reason to find new ones
|
|
|
|
* here.
|
|
|
|
*/
|
|
|
|
parents = c->parents;
|
|
|
|
while (parents) {
|
|
|
|
struct commit *p = parents->item;
|
|
|
|
parents = parents->next;
|
|
|
|
if (!(p->object.flags & UNINTERESTING)) {
|
|
|
|
p->object.flags |= UNINTERESTING;
|
|
|
|
changed = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!changed)
|
|
|
|
break;
|
|
|
|
}
|
2005-08-21 17:51:10 +08:00
|
|
|
}
|
|
|
|
|
2005-10-12 06:22:48 +08:00
|
|
|
static void show_one_commit(struct commit *commit, int no_name)
|
2005-08-21 17:51:10 +08:00
|
|
|
{
|
2005-11-11 07:47:58 +08:00
|
|
|
char pretty[256], *cp;
|
2006-06-18 09:26:18 +08:00
|
|
|
struct commit_name *name = commit->util;
|
2005-09-19 16:12:18 +08:00
|
|
|
if (commit->object.parsed)
|
2006-01-27 17:54:59 +08:00
|
|
|
pretty_print_commit(CMIT_FMT_ONELINE, commit, ~0,
|
2006-05-20 21:40:29 +08:00
|
|
|
pretty, sizeof(pretty), 0, NULL, NULL);
|
2005-09-19 16:12:18 +08:00
|
|
|
else
|
|
|
|
strcpy(pretty, "(unavailable)");
|
2005-08-21 17:51:10 +08:00
|
|
|
if (!strncmp(pretty, "[PATCH] ", 8))
|
|
|
|
cp = pretty + 8;
|
|
|
|
else
|
|
|
|
cp = pretty;
|
2005-10-12 06:22:48 +08:00
|
|
|
|
|
|
|
if (!no_name) {
|
|
|
|
if (name && name->head_name) {
|
|
|
|
printf("[%s", name->head_name);
|
|
|
|
if (name->generation) {
|
|
|
|
if (name->generation == 1)
|
|
|
|
printf("^");
|
|
|
|
else
|
|
|
|
printf("~%d", name->generation);
|
|
|
|
}
|
|
|
|
printf("] ");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
printf("[%s] ",
|
|
|
|
find_unique_abbrev(commit->object.sha1, 7));
|
2005-08-21 17:51:10 +08:00
|
|
|
}
|
|
|
|
puts(cp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *ref_name[MAX_REVS + 1];
|
|
|
|
static int ref_name_cnt;
|
|
|
|
|
2005-12-24 04:47:18 +08:00
|
|
|
static const char *find_digit_prefix(const char *s, int *v)
|
|
|
|
{
|
|
|
|
const char *p;
|
|
|
|
int ver;
|
|
|
|
char ch;
|
|
|
|
|
|
|
|
for (p = s, ver = 0;
|
|
|
|
'0' <= (ch = *p) && ch <= '9';
|
|
|
|
p++)
|
|
|
|
ver = ver * 10 + ch - '0';
|
|
|
|
*v = ver;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int version_cmp(const char *a, const char *b)
|
|
|
|
{
|
|
|
|
while (1) {
|
|
|
|
int va, vb;
|
|
|
|
|
|
|
|
a = find_digit_prefix(a, &va);
|
|
|
|
b = find_digit_prefix(b, &vb);
|
|
|
|
if (va != vb)
|
|
|
|
return va - vb;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
int ca = *a;
|
|
|
|
int cb = *b;
|
|
|
|
if ('0' <= ca && ca <= '9')
|
|
|
|
ca = 0;
|
|
|
|
if ('0' <= cb && cb <= '9')
|
|
|
|
cb = 0;
|
|
|
|
if (ca != cb)
|
|
|
|
return ca - cb;
|
|
|
|
if (!ca)
|
|
|
|
break;
|
|
|
|
a++;
|
|
|
|
b++;
|
|
|
|
}
|
|
|
|
if (!*a && !*b)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-08-25 14:26:20 +08:00
|
|
|
static int compare_ref_name(const void *a_, const void *b_)
|
|
|
|
{
|
|
|
|
const char * const*a = a_, * const*b = b_;
|
2005-12-24 04:47:18 +08:00
|
|
|
return version_cmp(*a, *b);
|
2005-08-25 14:26:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void sort_ref_range(int bottom, int top)
|
|
|
|
{
|
|
|
|
qsort(ref_name + bottom, top - bottom, sizeof(ref_name[0]),
|
|
|
|
compare_ref_name);
|
|
|
|
}
|
|
|
|
|
2005-08-21 17:51:10 +08:00
|
|
|
static int append_ref(const char *refname, const unsigned char *sha1)
|
|
|
|
{
|
|
|
|
struct commit *commit = lookup_commit_reference_gently(sha1, 1);
|
2005-12-24 04:47:18 +08:00
|
|
|
int i;
|
|
|
|
|
2005-08-21 17:51:10 +08:00
|
|
|
if (!commit)
|
|
|
|
return 0;
|
2005-12-24 04:47:18 +08:00
|
|
|
/* Avoid adding the same thing twice */
|
|
|
|
for (i = 0; i < ref_name_cnt; i++)
|
|
|
|
if (!strcmp(refname, ref_name[i]))
|
|
|
|
return 0;
|
|
|
|
|
2005-10-23 16:18:42 +08:00
|
|
|
if (MAX_REVS <= ref_name_cnt) {
|
2005-08-21 17:51:10 +08:00
|
|
|
fprintf(stderr, "warning: ignoring %s; "
|
2005-12-15 15:47:30 +08:00
|
|
|
"cannot handle more than %d refs\n",
|
2005-08-21 17:51:10 +08:00
|
|
|
refname, MAX_REVS);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
ref_name[ref_name_cnt++] = strdup(refname);
|
|
|
|
ref_name[ref_name_cnt] = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int append_head_ref(const char *refname, const unsigned char *sha1)
|
|
|
|
{
|
2005-11-21 16:43:12 +08:00
|
|
|
unsigned char tmp[20];
|
|
|
|
int ofs = 11;
|
|
|
|
if (strncmp(refname, "refs/heads/", ofs))
|
2005-08-21 17:51:10 +08:00
|
|
|
return 0;
|
2005-11-21 16:43:12 +08:00
|
|
|
/* If both heads/foo and tags/foo exists, get_sha1 would
|
|
|
|
* get confused.
|
|
|
|
*/
|
|
|
|
if (get_sha1(refname + ofs, tmp) || memcmp(tmp, sha1, 20))
|
|
|
|
ofs = 5;
|
|
|
|
return append_ref(refname + ofs, sha1);
|
2005-08-21 17:51:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int append_tag_ref(const char *refname, const unsigned char *sha1)
|
|
|
|
{
|
|
|
|
if (strncmp(refname, "refs/tags/", 10))
|
|
|
|
return 0;
|
|
|
|
return append_ref(refname + 5, sha1);
|
|
|
|
}
|
|
|
|
|
2005-12-05 07:58:50 +08:00
|
|
|
static const char *match_ref_pattern = NULL;
|
|
|
|
static int match_ref_slash = 0;
|
|
|
|
static int count_slash(const char *s)
|
|
|
|
{
|
|
|
|
int cnt = 0;
|
|
|
|
while (*s)
|
|
|
|
if (*s++ == '/')
|
|
|
|
cnt++;
|
|
|
|
return cnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int append_matching_ref(const char *refname, const unsigned char *sha1)
|
|
|
|
{
|
|
|
|
/* we want to allow pattern hold/<asterisk> to show all
|
|
|
|
* branches under refs/heads/hold/, and v0.99.9? to show
|
|
|
|
* refs/tags/v0.99.9a and friends.
|
|
|
|
*/
|
|
|
|
const char *tail;
|
|
|
|
int slash = count_slash(refname);
|
|
|
|
for (tail = refname; *tail && match_ref_slash < slash; )
|
|
|
|
if (*tail++ == '/')
|
|
|
|
slash--;
|
|
|
|
if (!*tail)
|
|
|
|
return 0;
|
|
|
|
if (fnmatch(match_ref_pattern, tail, 0))
|
|
|
|
return 0;
|
|
|
|
if (!strncmp("refs/heads/", refname, 11))
|
|
|
|
return append_head_ref(refname, sha1);
|
|
|
|
if (!strncmp("refs/tags/", refname, 10))
|
|
|
|
return append_tag_ref(refname, sha1);
|
|
|
|
return append_ref(refname, sha1);
|
|
|
|
}
|
|
|
|
|
2005-08-21 17:51:10 +08:00
|
|
|
static void snarf_refs(int head, int tag)
|
|
|
|
{
|
2005-08-25 14:26:20 +08:00
|
|
|
if (head) {
|
|
|
|
int orig_cnt = ref_name_cnt;
|
2005-08-21 17:51:10 +08:00
|
|
|
for_each_ref(append_head_ref);
|
2005-08-25 14:26:20 +08:00
|
|
|
sort_ref_range(orig_cnt, ref_name_cnt);
|
|
|
|
}
|
|
|
|
if (tag) {
|
|
|
|
int orig_cnt = ref_name_cnt;
|
2005-08-21 17:51:10 +08:00
|
|
|
for_each_ref(append_tag_ref);
|
2005-08-25 14:26:20 +08:00
|
|
|
sort_ref_range(orig_cnt, ref_name_cnt);
|
|
|
|
}
|
2005-08-21 17:51:10 +08:00
|
|
|
}
|
|
|
|
|
2006-01-11 16:16:42 +08:00
|
|
|
static int rev_is_head(char *head_path, int headlen, char *name,
|
2005-08-21 17:51:10 +08:00
|
|
|
unsigned char *head_sha1, unsigned char *sha1)
|
|
|
|
{
|
|
|
|
int namelen;
|
2006-01-11 16:16:42 +08:00
|
|
|
if ((!head_path[0]) ||
|
|
|
|
(head_sha1 && sha1 && memcmp(head_sha1, sha1, 20)))
|
2005-08-21 17:51:10 +08:00
|
|
|
return 0;
|
|
|
|
namelen = strlen(name);
|
|
|
|
if ((headlen < namelen) ||
|
|
|
|
memcmp(head_path + headlen - namelen, name, namelen))
|
|
|
|
return 0;
|
|
|
|
if (headlen == namelen ||
|
|
|
|
head_path[headlen - namelen - 1] == '/')
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int show_merge_base(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);
|
2005-09-09 03:15:52 +08:00
|
|
|
int exit_status = 1;
|
2005-08-21 17:51:10 +08:00
|
|
|
|
|
|
|
while (seen) {
|
|
|
|
struct commit *commit = pop_one_commit(&seen);
|
|
|
|
int flags = commit->object.flags & all_mask;
|
|
|
|
if (!(flags & UNINTERESTING) &&
|
|
|
|
((flags & all_revs) == all_revs)) {
|
|
|
|
puts(sha1_to_hex(commit->object.sha1));
|
2005-09-09 03:15:52 +08:00
|
|
|
exit_status = 0;
|
|
|
|
commit->object.flags |= UNINTERESTING;
|
2005-08-21 17:51:10 +08:00
|
|
|
}
|
|
|
|
}
|
2005-09-09 03:15:52 +08:00
|
|
|
return exit_status;
|
2005-08-21 17:51:10 +08:00
|
|
|
}
|
|
|
|
|
2005-09-10 06:40:45 +08:00
|
|
|
static int show_independent(struct commit **rev,
|
|
|
|
int num_rev,
|
|
|
|
char **ref_name,
|
|
|
|
unsigned int *rev_mask)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < num_rev; i++) {
|
|
|
|
struct commit *commit = rev[i];
|
|
|
|
unsigned int flag = rev_mask[i];
|
|
|
|
|
|
|
|
if (commit->object.flags == flag)
|
|
|
|
puts(sha1_to_hex(commit->object.sha1));
|
|
|
|
commit->object.flags |= UNINTERESTING;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-12-05 07:58:50 +08:00
|
|
|
static void append_one_rev(const char *av)
|
|
|
|
{
|
|
|
|
unsigned char revkey[20];
|
|
|
|
if (!get_sha1(av, revkey)) {
|
|
|
|
append_ref(av, revkey);
|
|
|
|
return;
|
|
|
|
}
|
2006-01-11 16:20:25 +08:00
|
|
|
if (strchr(av, '*') || strchr(av, '?') || strchr(av, '[')) {
|
2005-12-05 07:58:50 +08:00
|
|
|
/* glob style match */
|
|
|
|
int saved_matches = ref_name_cnt;
|
|
|
|
match_ref_pattern = av;
|
|
|
|
match_ref_slash = count_slash(av);
|
|
|
|
for_each_ref(append_matching_ref);
|
|
|
|
if (saved_matches == ref_name_cnt &&
|
|
|
|
ref_name_cnt < MAX_REVS)
|
|
|
|
error("no matching refs with %s", av);
|
2005-12-16 09:53:44 +08:00
|
|
|
if (saved_matches + 1 < ref_name_cnt)
|
|
|
|
sort_ref_range(saved_matches, ref_name_cnt);
|
2005-12-05 07:58:50 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
die("bad sha1 reference %s", av);
|
|
|
|
}
|
|
|
|
|
2006-01-10 05:29:23 +08:00
|
|
|
static int git_show_branch_config(const char *var, const char *value)
|
|
|
|
{
|
|
|
|
if (!strcmp(var, "showbranch.default")) {
|
|
|
|
if (default_alloc <= default_num + 1) {
|
|
|
|
default_alloc = default_alloc * 3 / 2 + 20;
|
|
|
|
default_arg = xrealloc(default_arg, sizeof *default_arg * default_alloc);
|
|
|
|
}
|
|
|
|
default_arg[default_num++] = strdup(value);
|
|
|
|
default_arg[default_num] = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return git_default_config(var, value);
|
|
|
|
}
|
|
|
|
|
2006-05-02 08:12:26 +08:00
|
|
|
static int omit_in_dense(struct commit *commit, struct commit **rev, int n)
|
|
|
|
{
|
|
|
|
/* If the commit is tip of the named branches, do not
|
|
|
|
* omit it.
|
|
|
|
* Otherwise, if it is a merge that is reachable from only one
|
|
|
|
* tip, it is not that interesting.
|
|
|
|
*/
|
|
|
|
int i, flag, count;
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
if (rev[i] == commit)
|
|
|
|
return 0;
|
|
|
|
flag = commit->object.flags;
|
|
|
|
for (i = count = 0; i < n; i++) {
|
|
|
|
if (flag & (1u << (i + REV_SHIFT)))
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
if (count == 1)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-29 13:44:25 +08:00
|
|
|
int cmd_show_branch(int ac, const char **av, const char *prefix)
|
2005-08-21 17:51:10 +08:00
|
|
|
{
|
|
|
|
struct commit *rev[MAX_REVS], *commit;
|
|
|
|
struct commit_list *list = NULL, *seen = NULL;
|
2005-09-10 06:40:45 +08:00
|
|
|
unsigned int rev_mask[MAX_REVS];
|
2005-08-21 17:51:10 +08:00
|
|
|
int num_rev, i, extra = 0;
|
|
|
|
int all_heads = 0, all_tags = 0;
|
2005-11-11 07:47:58 +08:00
|
|
|
int all_mask, all_revs;
|
2006-02-16 14:05:33 +08:00
|
|
|
int lifo = 1;
|
2005-08-21 17:51:10 +08:00
|
|
|
char head_path[128];
|
2005-10-01 05:26:57 +08:00
|
|
|
const char *head_path_p;
|
2005-08-21 17:51:10 +08:00
|
|
|
int head_path_len;
|
|
|
|
unsigned char head_sha1[20];
|
|
|
|
int merge_base = 0;
|
2005-09-10 06:40:45 +08:00
|
|
|
int independent = 0;
|
2005-10-12 06:22:48 +08:00
|
|
|
int no_name = 0;
|
|
|
|
int sha1_name = 0;
|
2005-11-11 07:47:58 +08:00
|
|
|
int shown_merge_point = 0;
|
2006-01-11 16:16:42 +08:00
|
|
|
int with_current_branch = 0;
|
2006-01-12 06:02:38 +08:00
|
|
|
int head_at = -1;
|
2006-03-03 09:14:00 +08:00
|
|
|
int topics = 0;
|
2006-05-02 08:12:26 +08:00
|
|
|
int dense = 1;
|
2005-08-21 17:51:10 +08:00
|
|
|
|
2006-02-10 06:41:39 +08:00
|
|
|
git_config(git_show_branch_config);
|
2005-08-31 07:59:37 +08:00
|
|
|
|
2006-01-10 05:29:23 +08:00
|
|
|
/* If nothing is specified, try the default first */
|
|
|
|
if (ac == 1 && default_num) {
|
|
|
|
ac = default_num + 1;
|
|
|
|
av = default_arg - 1; /* ick; we would not address av[0] */
|
|
|
|
}
|
|
|
|
|
2005-08-21 17:51:10 +08:00
|
|
|
while (1 < ac && av[1][0] == '-') {
|
2006-05-23 20:15:35 +08:00
|
|
|
const char *arg = av[1];
|
2006-01-10 05:29:23 +08:00
|
|
|
if (!strcmp(arg, "--")) {
|
|
|
|
ac--; av++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (!strcmp(arg, "--all"))
|
2005-08-21 17:51:10 +08:00
|
|
|
all_heads = all_tags = 1;
|
|
|
|
else if (!strcmp(arg, "--heads"))
|
|
|
|
all_heads = 1;
|
|
|
|
else if (!strcmp(arg, "--tags"))
|
|
|
|
all_tags = 1;
|
|
|
|
else if (!strcmp(arg, "--more"))
|
|
|
|
extra = 1;
|
2005-09-10 06:40:45 +08:00
|
|
|
else if (!strcmp(arg, "--list"))
|
|
|
|
extra = -1;
|
2005-10-12 06:22:48 +08:00
|
|
|
else if (!strcmp(arg, "--no-name"))
|
|
|
|
no_name = 1;
|
2006-01-11 16:16:42 +08:00
|
|
|
else if (!strcmp(arg, "--current"))
|
|
|
|
with_current_branch = 1;
|
2005-10-12 06:22:48 +08:00
|
|
|
else if (!strcmp(arg, "--sha1-name"))
|
|
|
|
sha1_name = 1;
|
2005-09-10 06:40:45 +08:00
|
|
|
else if (!strncmp(arg, "--more=", 7))
|
2005-08-21 17:51:10 +08:00
|
|
|
extra = atoi(arg + 7);
|
|
|
|
else if (!strcmp(arg, "--merge-base"))
|
|
|
|
merge_base = 1;
|
2005-09-10 06:40:45 +08:00
|
|
|
else if (!strcmp(arg, "--independent"))
|
|
|
|
independent = 1;
|
2005-11-11 07:47:58 +08:00
|
|
|
else if (!strcmp(arg, "--topo-order"))
|
2006-02-16 14:05:33 +08:00
|
|
|
lifo = 1;
|
2006-03-03 09:14:00 +08:00
|
|
|
else if (!strcmp(arg, "--topics"))
|
|
|
|
topics = 1;
|
2006-05-02 08:12:26 +08:00
|
|
|
else if (!strcmp(arg, "--sparse"))
|
|
|
|
dense = 0;
|
2006-02-16 14:05:33 +08:00
|
|
|
else if (!strcmp(arg, "--date-order"))
|
|
|
|
lifo = 0;
|
2005-08-21 17:51:10 +08:00
|
|
|
else
|
|
|
|
usage(show_branch_usage);
|
|
|
|
ac--; av++;
|
|
|
|
}
|
|
|
|
ac--; av++;
|
|
|
|
|
2005-09-10 06:40:45 +08:00
|
|
|
/* Only one of these is allowed */
|
|
|
|
if (1 < independent + merge_base + (extra != 0))
|
|
|
|
usage(show_branch_usage);
|
|
|
|
|
2005-12-24 04:47:18 +08:00
|
|
|
/* If nothing is specified, show all branches by default */
|
|
|
|
if (ac + all_heads + all_tags == 0)
|
|
|
|
all_heads = 1;
|
|
|
|
|
2005-08-21 17:51:10 +08:00
|
|
|
if (all_heads + all_tags)
|
|
|
|
snarf_refs(all_heads, all_tags);
|
2005-12-24 04:47:18 +08:00
|
|
|
while (0 < ac) {
|
|
|
|
append_one_rev(*av);
|
|
|
|
ac--; av++;
|
2005-12-05 07:58:50 +08:00
|
|
|
}
|
2005-12-24 04:47:18 +08:00
|
|
|
|
2006-01-11 16:16:42 +08:00
|
|
|
head_path_p = resolve_ref(git_path("HEAD"), head_sha1, 1);
|
|
|
|
if (head_path_p) {
|
|
|
|
head_path_len = strlen(head_path_p);
|
|
|
|
memcpy(head_path, head_path_p, head_path_len + 1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
head_path_len = 0;
|
|
|
|
head_path[0] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (with_current_branch && head_path_p) {
|
|
|
|
int has_head = 0;
|
|
|
|
for (i = 0; !has_head && i < ref_name_cnt; i++) {
|
|
|
|
/* We are only interested in adding the branch
|
|
|
|
* HEAD points at.
|
|
|
|
*/
|
|
|
|
if (rev_is_head(head_path,
|
|
|
|
head_path_len,
|
|
|
|
ref_name[i],
|
|
|
|
head_sha1, NULL))
|
|
|
|
has_head++;
|
|
|
|
}
|
|
|
|
if (!has_head) {
|
|
|
|
int pfxlen = strlen(git_path("refs/heads/"));
|
|
|
|
append_one_rev(head_path + pfxlen);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-12-05 07:58:50 +08:00
|
|
|
if (!ref_name_cnt) {
|
|
|
|
fprintf(stderr, "No revs to be shown.\n");
|
|
|
|
exit(0);
|
|
|
|
}
|
2005-08-21 17:51:10 +08:00
|
|
|
|
|
|
|
for (num_rev = 0; ref_name[num_rev]; num_rev++) {
|
|
|
|
unsigned char revkey[20];
|
2005-09-10 06:40:45 +08:00
|
|
|
unsigned int flag = 1u << (num_rev + REV_SHIFT);
|
2005-08-21 17:51:10 +08:00
|
|
|
|
|
|
|
if (MAX_REVS <= num_rev)
|
|
|
|
die("cannot handle more than %d revs.", MAX_REVS);
|
|
|
|
if (get_sha1(ref_name[num_rev], revkey))
|
2005-12-15 15:47:30 +08:00
|
|
|
die("'%s' is not a valid ref.", ref_name[num_rev]);
|
2005-08-21 17:51:10 +08:00
|
|
|
commit = lookup_commit_reference(revkey);
|
|
|
|
if (!commit)
|
|
|
|
die("cannot find commit %s (%s)",
|
|
|
|
ref_name[num_rev], revkey);
|
|
|
|
parse_commit(commit);
|
|
|
|
mark_seen(commit, &seen);
|
|
|
|
|
|
|
|
/* rev#0 uses bit REV_SHIFT, rev#1 uses bit REV_SHIFT+1,
|
|
|
|
* and so on. REV_SHIFT bits from bit 0 are used for
|
|
|
|
* internal bookkeeping.
|
|
|
|
*/
|
2005-09-10 06:40:45 +08:00
|
|
|
commit->object.flags |= flag;
|
|
|
|
if (commit->object.flags == flag)
|
|
|
|
insert_by_date(commit, &list);
|
2005-08-21 17:51:10 +08:00
|
|
|
rev[num_rev] = commit;
|
|
|
|
}
|
2005-09-10 06:40:45 +08:00
|
|
|
for (i = 0; i < num_rev; i++)
|
|
|
|
rev_mask[i] = rev[i]->object.flags;
|
|
|
|
|
|
|
|
if (0 <= extra)
|
|
|
|
join_revs(&list, &seen, num_rev, extra);
|
2005-08-21 17:51:10 +08:00
|
|
|
|
2006-07-16 15:00:09 +08:00
|
|
|
sort_by_date(&seen);
|
|
|
|
|
2005-08-21 17:51:10 +08:00
|
|
|
if (merge_base)
|
|
|
|
return show_merge_base(seen, num_rev);
|
|
|
|
|
2005-09-10 06:40:45 +08:00
|
|
|
if (independent)
|
|
|
|
return show_independent(rev, num_rev, ref_name, rev_mask);
|
|
|
|
|
|
|
|
/* Show list; --more=-1 means list-only */
|
2005-09-11 09:24:46 +08:00
|
|
|
if (1 < num_rev || extra < 0) {
|
2005-08-21 17:51:10 +08:00
|
|
|
for (i = 0; i < num_rev; i++) {
|
|
|
|
int j;
|
|
|
|
int is_head = rev_is_head(head_path,
|
|
|
|
head_path_len,
|
|
|
|
ref_name[i],
|
|
|
|
head_sha1,
|
|
|
|
rev[i]->object.sha1);
|
2005-09-10 06:40:45 +08:00
|
|
|
if (extra < 0)
|
|
|
|
printf("%c [%s] ",
|
|
|
|
is_head ? '*' : ' ', ref_name[i]);
|
|
|
|
else {
|
|
|
|
for (j = 0; j < i; j++)
|
|
|
|
putchar(' ');
|
|
|
|
printf("%c [%s] ",
|
|
|
|
is_head ? '*' : '!', ref_name[i]);
|
|
|
|
}
|
2005-10-12 06:22:48 +08:00
|
|
|
/* header lines never need name */
|
|
|
|
show_one_commit(rev[i], 1);
|
2006-01-12 06:02:38 +08:00
|
|
|
if (is_head)
|
|
|
|
head_at = i;
|
2005-08-21 17:51:10 +08:00
|
|
|
}
|
2005-09-10 06:40:45 +08:00
|
|
|
if (0 <= extra) {
|
|
|
|
for (i = 0; i < num_rev; i++)
|
|
|
|
putchar('-');
|
|
|
|
putchar('\n');
|
|
|
|
}
|
2005-08-23 14:16:46 +08:00
|
|
|
}
|
2005-09-10 06:40:45 +08:00
|
|
|
if (extra < 0)
|
|
|
|
exit(0);
|
2005-08-23 14:16:46 +08:00
|
|
|
|
2005-08-30 08:19:47 +08:00
|
|
|
/* Sort topologically */
|
2006-02-16 14:05:33 +08:00
|
|
|
sort_in_topological_order(&seen, lifo);
|
2005-08-30 08:19:47 +08:00
|
|
|
|
|
|
|
/* Give names to commits */
|
2005-10-12 06:22:48 +08:00
|
|
|
if (!sha1_name && !no_name)
|
|
|
|
name_commits(seen, rev, ref_name, num_rev);
|
2005-08-30 08:19:47 +08:00
|
|
|
|
|
|
|
all_mask = ((1u << (REV_SHIFT + num_rev)) - 1);
|
|
|
|
all_revs = all_mask & ~((1u << REV_SHIFT) - 1);
|
|
|
|
|
2005-08-21 17:51:10 +08:00
|
|
|
while (seen) {
|
|
|
|
struct commit *commit = pop_one_commit(&seen);
|
|
|
|
int this_flag = commit->object.flags;
|
2006-03-04 06:34:40 +08:00
|
|
|
int is_merge_point = ((this_flag & all_revs) == all_revs);
|
2005-08-23 14:16:46 +08:00
|
|
|
|
2006-03-04 06:34:40 +08:00
|
|
|
shown_merge_point |= is_merge_point;
|
2005-08-30 08:19:47 +08:00
|
|
|
|
2005-08-23 14:16:46 +08:00
|
|
|
if (1 < num_rev) {
|
2006-05-02 08:12:26 +08:00
|
|
|
int is_merge = !!(commit->parents &&
|
|
|
|
commit->parents->next);
|
2006-03-04 06:34:40 +08:00
|
|
|
if (topics &&
|
|
|
|
!is_merge_point &&
|
|
|
|
(this_flag & (1u << REV_SHIFT)))
|
|
|
|
continue;
|
2006-05-02 08:12:26 +08:00
|
|
|
if (dense && is_merge &&
|
|
|
|
omit_in_dense(commit, rev, num_rev))
|
|
|
|
continue;
|
2006-01-12 06:02:38 +08:00
|
|
|
for (i = 0; i < num_rev; i++) {
|
|
|
|
int mark;
|
|
|
|
if (!(this_flag & (1u << (i + REV_SHIFT))))
|
|
|
|
mark = ' ';
|
|
|
|
else if (is_merge)
|
|
|
|
mark = '-';
|
|
|
|
else if (i == head_at)
|
|
|
|
mark = '*';
|
|
|
|
else
|
|
|
|
mark = '+';
|
|
|
|
putchar(mark);
|
|
|
|
}
|
2005-08-23 14:16:46 +08:00
|
|
|
putchar(' ');
|
|
|
|
}
|
2005-10-12 06:22:48 +08:00
|
|
|
show_one_commit(commit, no_name);
|
2005-11-11 07:47:58 +08:00
|
|
|
|
|
|
|
if (shown_merge_point && --extra < 0)
|
|
|
|
break;
|
2005-08-21 17:51:10 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|