You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
306 lines
7.1 KiB
306 lines
7.1 KiB
19 years ago
|
#include "builtin.h"
|
||
19 years ago
|
#include "cache.h"
|
||
|
#include "commit.h"
|
||
|
#include "tag.h"
|
||
|
#include "refs.h"
|
||
17 years ago
|
#include "parse-options.h"
|
||
19 years ago
|
|
||
18 years ago
|
#define CUTOFF_DATE_SLOP 86400 /* one day */
|
||
|
|
||
19 years ago
|
typedef struct rev_name {
|
||
|
const char *tip_name;
|
||
|
int generation;
|
||
18 years ago
|
int distance;
|
||
19 years ago
|
} rev_name;
|
||
|
|
||
|
static long cutoff = LONG_MAX;
|
||
|
|
||
18 years ago
|
/* How many generations are maximally preferred over _one_ merge traversal? */
|
||
|
#define MERGE_TRAVERSAL_WEIGHT 65535
|
||
|
|
||
19 years ago
|
static void name_rev(struct commit *commit,
|
||
18 years ago
|
const char *tip_name, int generation, int distance,
|
||
19 years ago
|
int deref)
|
||
|
{
|
||
19 years ago
|
struct rev_name *name = (struct rev_name *)commit->util;
|
||
19 years ago
|
struct commit_list *parents;
|
||
19 years ago
|
int parent_number = 1;
|
||
19 years ago
|
|
||
|
if (!commit->object.parsed)
|
||
|
parse_commit(commit);
|
||
|
|
||
|
if (commit->date < cutoff)
|
||
|
return;
|
||
|
|
||
|
if (deref) {
|
||
|
char *new_name = xmalloc(strlen(tip_name)+3);
|
||
|
strcpy(new_name, tip_name);
|
||
|
strcat(new_name, "^0");
|
||
|
tip_name = new_name;
|
||
|
|
||
|
if (generation)
|
||
|
die("generation: %d, but deref?", generation);
|
||
|
}
|
||
|
|
||
|
if (name == NULL) {
|
||
|
name = xmalloc(sizeof(rev_name));
|
||
19 years ago
|
commit->util = name;
|
||
19 years ago
|
goto copy_data;
|
||
18 years ago
|
} else if (name->distance > distance) {
|
||
19 years ago
|
copy_data:
|
||
|
name->tip_name = tip_name;
|
||
|
name->generation = generation;
|
||
18 years ago
|
name->distance = distance;
|
||
19 years ago
|
} else
|
||
|
return;
|
||
|
|
||
|
for (parents = commit->parents;
|
||
|
parents;
|
||
|
parents = parents->next, parent_number++) {
|
||
19 years ago
|
if (parent_number > 1) {
|
||
18 years ago
|
int len = strlen(tip_name);
|
||
18 years ago
|
char *new_name = xmalloc(len +
|
||
|
1 + decimal_length(generation) + /* ~<n> */
|
||
|
1 + 2 + /* ^NN */
|
||
|
1);
|
||
19 years ago
|
|
||
18 years ago
|
if (len > 2 && !strcmp(tip_name + len - 2, "^0"))
|
||
|
len -= 2;
|
||
19 years ago
|
if (generation > 0)
|
||
18 years ago
|
sprintf(new_name, "%.*s~%d^%d", len, tip_name,
|
||
19 years ago
|
generation, parent_number);
|
||
|
else
|
||
18 years ago
|
sprintf(new_name, "%.*s^%d", len, tip_name,
|
||
|
parent_number);
|
||
19 years ago
|
|
||
18 years ago
|
name_rev(parents->item, new_name, 0,
|
||
|
distance + MERGE_TRAVERSAL_WEIGHT, 0);
|
||
19 years ago
|
} else {
|
||
18 years ago
|
name_rev(parents->item, tip_name, generation + 1,
|
||
|
distance + 1, 0);
|
||
19 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
18 years ago
|
struct name_ref_data {
|
||
|
int tags_only;
|
||
18 years ago
|
int name_only;
|
||
18 years ago
|
const char *ref_filter;
|
||
|
};
|
||
|
|
||
19 years ago
|
static int name_ref(const char *path, const unsigned char *sha1, int flags, void *cb_data)
|
||
19 years ago
|
{
|
||
|
struct object *o = parse_object(sha1);
|
||
18 years ago
|
struct name_ref_data *data = cb_data;
|
||
19 years ago
|
int deref = 0;
|
||
|
|
||
18 years ago
|
if (data->tags_only && prefixcmp(path, "refs/tags/"))
|
||
18 years ago
|
return 0;
|
||
|
|
||
|
if (data->ref_filter && fnmatch(data->ref_filter, path, 0))
|
||
19 years ago
|
return 0;
|
||
|
|
||
19 years ago
|
while (o && o->type == OBJ_TAG) {
|
||
19 years ago
|
struct tag *t = (struct tag *) o;
|
||
|
if (!t->tagged)
|
||
|
break; /* broken repository */
|
||
|
o = parse_object(t->tagged->sha1);
|
||
|
deref = 1;
|
||
|
}
|
||
19 years ago
|
if (o && o->type == OBJ_COMMIT) {
|
||
19 years ago
|
struct commit *commit = (struct commit *)o;
|
||
|
|
||
18 years ago
|
if (!prefixcmp(path, "refs/heads/"))
|
||
19 years ago
|
path = path + 11;
|
||
18 years ago
|
else if (data->tags_only
|
||
|
&& data->name_only
|
||
|
&& !prefixcmp(path, "refs/tags/"))
|
||
|
path = path + 10;
|
||
18 years ago
|
else if (!prefixcmp(path, "refs/"))
|
||
19 years ago
|
path = path + 5;
|
||
19 years ago
|
|
||
19 years ago
|
name_rev(commit, xstrdup(path), 0, 0, deref);
|
||
19 years ago
|
}
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
/* returns a static buffer */
|
||
17 years ago
|
static const char *get_rev_name(const struct object *o)
|
||
19 years ago
|
{
|
||
|
static char buffer[1024];
|
||
19 years ago
|
struct rev_name *n;
|
||
|
struct commit *c;
|
||
|
|
||
19 years ago
|
if (o->type != OBJ_COMMIT)
|
||
17 years ago
|
return NULL;
|
||
19 years ago
|
c = (struct commit *) o;
|
||
|
n = c->util;
|
||
19 years ago
|
if (!n)
|
||
17 years ago
|
return NULL;
|
||
19 years ago
|
|
||
|
if (!n->generation)
|
||
|
return n->tip_name;
|
||
18 years ago
|
else {
|
||
|
int len = strlen(n->tip_name);
|
||
|
if (len > 2 && !strcmp(n->tip_name + len - 2, "^0"))
|
||
|
len -= 2;
|
||
|
snprintf(buffer, sizeof(buffer), "%.*s~%d", len, n->tip_name,
|
||
|
n->generation);
|
||
|
|
||
|
return buffer;
|
||
|
}
|
||
19 years ago
|
}
|
||
19 years ago
|
|
||
17 years ago
|
static void show_name(const struct object *obj,
|
||
|
const char *caller_name,
|
||
|
int always, int allow_undefined, int name_only)
|
||
|
{
|
||
|
const char *name;
|
||
|
const unsigned char *sha1 = obj->sha1;
|
||
|
|
||
|
if (!name_only)
|
||
|
printf("%s ", caller_name ? caller_name : sha1_to_hex(sha1));
|
||
|
name = get_rev_name(obj);
|
||
|
if (name)
|
||
|
printf("%s\n", name);
|
||
|
else if (allow_undefined)
|
||
|
printf("undefined\n");
|
||
|
else if (always)
|
||
|
printf("%s\n", find_unique_abbrev(sha1, DEFAULT_ABBREV));
|
||
|
else
|
||
|
die("cannot describe '%s'", sha1_to_hex(sha1));
|
||
|
}
|
||
|
|
||
17 years ago
|
static char const * const name_rev_usage[] = {
|
||
17 years ago
|
"git name-rev [options] ( --all | --stdin | <commit>... )",
|
||
17 years ago
|
NULL
|
||
|
};
|
||
|
|
||
17 years ago
|
static void name_rev_line(char *p, struct name_ref_data *data)
|
||
|
{
|
||
|
int forty = 0;
|
||
|
char *p_start;
|
||
|
for (p_start = p; *p; p++) {
|
||
|
#define ishex(x) (isdigit((x)) || ((x) >= 'a' && (x) <= 'f'))
|
||
|
if (!ishex(*p))
|
||
|
forty = 0;
|
||
|
else if (++forty == 40 &&
|
||
|
!ishex(*(p+1))) {
|
||
|
unsigned char sha1[40];
|
||
|
const char *name = NULL;
|
||
|
char c = *(p+1);
|
||
17 years ago
|
int p_len = p - p_start + 1;
|
||
17 years ago
|
|
||
|
forty = 0;
|
||
|
|
||
|
*(p+1) = 0;
|
||
|
if (!get_sha1(p - 39, sha1)) {
|
||
|
struct object *o =
|
||
|
lookup_object(sha1);
|
||
|
if (o)
|
||
|
name = get_rev_name(o);
|
||
|
}
|
||
|
*(p+1) = c;
|
||
|
|
||
|
if (!name)
|
||
|
continue;
|
||
|
|
||
17 years ago
|
if (data->name_only)
|
||
|
printf("%.*s%s", p_len - 40, p_start, name);
|
||
|
else
|
||
|
printf("%.*s (%s)", p_len, p_start, name);
|
||
17 years ago
|
p_start = p + 1;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/* flush */
|
||
|
if (p_start != p)
|
||
|
fwrite(p_start, p - p_start, 1, stdout);
|
||
|
}
|
||
|
|
||
19 years ago
|
int cmd_name_rev(int argc, const char **argv, const char *prefix)
|
||
19 years ago
|
{
|
||
19 years ago
|
struct object_array revs = { 0, 0, NULL };
|
||
17 years ago
|
int all = 0, transform_stdin = 0, allow_undefined = 1, always = 0;
|
||
18 years ago
|
struct name_ref_data data = { 0, 0, NULL };
|
||
17 years ago
|
struct option opts[] = {
|
||
|
OPT_BOOLEAN(0, "name-only", &data.name_only, "print only names (no SHA-1)"),
|
||
|
OPT_BOOLEAN(0, "tags", &data.tags_only, "only use tags to name the commits"),
|
||
|
OPT_STRING(0, "refs", &data.ref_filter, "pattern",
|
||
|
"only use refs matching <pattern>"),
|
||
|
OPT_GROUP(""),
|
||
|
OPT_BOOLEAN(0, "all", &all, "list all commits reachable from all refs"),
|
||
|
OPT_BOOLEAN(0, "stdin", &transform_stdin, "read from stdin"),
|
||
17 years ago
|
OPT_BOOLEAN(0, "undefined", &allow_undefined, "allow to print `undefined` names"),
|
||
17 years ago
|
OPT_BOOLEAN(0, "always", &always,
|
||
|
"show abbreviated commit object as fallback"),
|
||
17 years ago
|
OPT_END(),
|
||
|
};
|
||
19 years ago
|
|
||
17 years ago
|
git_config(git_default_config, NULL);
|
||
16 years ago
|
argc = parse_options(argc, argv, prefix, opts, name_rev_usage, 0);
|
||
17 years ago
|
if (!!all + !!transform_stdin + !!argc > 1) {
|
||
|
error("Specify either a list, or --all, not both!");
|
||
|
usage_with_options(name_rev_usage, opts);
|
||
|
}
|
||
|
if (all || transform_stdin)
|
||
|
cutoff = 0;
|
||
19 years ago
|
|
||
17 years ago
|
for (; argc; argc--, argv++) {
|
||
19 years ago
|
unsigned char sha1[20];
|
||
|
struct object *o;
|
||
|
struct commit *commit;
|
||
|
|
||
|
if (get_sha1(*argv, sha1)) {
|
||
|
fprintf(stderr, "Could not get sha1 for %s. Skipping.\n",
|
||
|
*argv);
|
||
|
continue;
|
||
|
}
|
||
|
|
||
19 years ago
|
o = deref_tag(parse_object(sha1), *argv, 0);
|
||
19 years ago
|
if (!o || o->type != OBJ_COMMIT) {
|
||
19 years ago
|
fprintf(stderr, "Could not get commit for %s. Skipping.\n",
|
||
|
*argv);
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
commit = (struct commit *)o;
|
||
|
if (cutoff > commit->date)
|
||
|
cutoff = commit->date;
|
||
19 years ago
|
add_object_array((struct object *)commit, *argv, &revs);
|
||
19 years ago
|
}
|
||
|
|
||
18 years ago
|
if (cutoff)
|
||
|
cutoff = cutoff - CUTOFF_DATE_SLOP;
|
||
18 years ago
|
for_each_ref(name_ref, &data);
|
||
19 years ago
|
|
||
|
if (transform_stdin) {
|
||
|
char buffer[2048];
|
||
|
|
||
|
while (!feof(stdin)) {
|
||
17 years ago
|
char *p = fgets(buffer, sizeof(buffer), stdin);
|
||
19 years ago
|
if (!p)
|
||
|
break;
|
||
17 years ago
|
name_rev_line(p, &data);
|
||
19 years ago
|
}
|
||
|
} else if (all) {
|
||
19 years ago
|
int i, max;
|
||
19 years ago
|
|
||
19 years ago
|
max = get_max_object_index();
|
||
17 years ago
|
for (i = 0; i < max; i++) {
|
||
|
struct object *obj = get_indexed_object(i);
|
||
|
if (!obj)
|
||
|
continue;
|
||
|
show_name(obj, NULL,
|
||
17 years ago
|
always, allow_undefined, data.name_only);
|
||
17 years ago
|
}
|
||
19 years ago
|
} else {
|
||
|
int i;
|
||
17 years ago
|
for (i = 0; i < revs.nr; i++)
|
||
|
show_name(revs.objects[i].item, revs.objects[i].name,
|
||
|
always, allow_undefined, data.name_only);
|
||
19 years ago
|
}
|
||
19 years ago
|
|
||
|
return 0;
|
||
|
}
|