Tell between packed, unpacked and symbolic refs.

This adds a "int *flag" parameter to resolve_ref() and makes
for_each_ref() family to call callback function with an extra
"int flag" parameter.  They are used to give two bits of
information (REF_ISSYMREF and REF_ISPACKED) about the ref.

Signed-off-by: Junio C Hamano <junkio@cox.net>
This commit is contained in:
Junio C Hamano 2006-09-20 22:02:01 -07:00
parent cb5d709ff8
commit 8da1977554
23 changed files with 75 additions and 49 deletions

View File

@ -277,7 +277,7 @@ int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix)
usage(fmt_merge_msg_usage);
/* get current branch */
current_branch = resolve_ref("HEAD", head_sha1, 1);
current_branch = resolve_ref("HEAD", head_sha1, 1, NULL);
if (!strncmp(current_branch, "refs/heads/", 11))
current_branch += 11;

View File

@ -75,7 +75,7 @@ copy_data:
}
}
static int name_ref(const char *path, const unsigned char *sha1, void *cb_data)
static int name_ref(const char *path, const unsigned char *sha1, int flags, void *cb_data)
{
struct object *o = parse_object(sha1);
int tags_only = *(int*)cb_data;

View File

@ -9,7 +9,8 @@ static void remove_lock_file(void)
unlink(lock_path);
}
static int handle_one_ref(const char *path, const unsigned char *sha1, void *cb_data)
static int handle_one_ref(const char *path, const unsigned char *sha1,
int flags, void *cb_data)
{
FILE *refs_file = cb_data;

View File

@ -174,7 +174,7 @@ static void walk_commit_list(struct rev_info *revs)
}
}
static int add_one_ref(const char *path, const unsigned char *sha1, void *cb_data)
static int add_one_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
{
struct object *object = parse_object(sha1);
if (!object)

View File

@ -27,7 +27,7 @@ static void add_refspec(const char *ref)
refspec_nr = nr;
}
static int expand_one_ref(const char *ref, const unsigned char *sha1, void *cb_data)
static int expand_one_ref(const char *ref, const unsigned char *sha1, int flag, void *cb_data)
{
/* Ignore the "refs/" at the beginning of the refname */
ref += 5;

View File

@ -137,7 +137,7 @@ static void show_default(void)
}
}
static int show_reference(const char *refname, const unsigned char *sha1, void *cb_data)
static int show_reference(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
{
show_rev(NORMAL, sha1, refname);
return 0;

View File

@ -346,7 +346,7 @@ static void sort_ref_range(int bottom, int top)
compare_ref_name);
}
static int append_ref(const char *refname, const unsigned char *sha1, void *cb_data)
static int append_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
{
struct commit *commit = lookup_commit_reference_gently(sha1, 1);
int i;
@ -369,7 +369,7 @@ static int append_ref(const char *refname, const unsigned char *sha1, void *cb_d
return 0;
}
static int append_head_ref(const char *refname, const unsigned char *sha1, void *cb_data)
static int append_head_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
{
unsigned char tmp[20];
int ofs = 11;
@ -380,14 +380,14 @@ static int append_head_ref(const char *refname, const unsigned char *sha1, void
*/
if (get_sha1(refname + ofs, tmp) || hashcmp(tmp, sha1))
ofs = 5;
return append_ref(refname + ofs, sha1, cb_data);
return append_ref(refname + ofs, sha1, flag, cb_data);
}
static int append_tag_ref(const char *refname, const unsigned char *sha1, void *cb_data)
static int append_tag_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
{
if (strncmp(refname, "refs/tags/", 10))
return 0;
return append_ref(refname + 5, sha1, cb_data);
return append_ref(refname + 5, sha1, flag, cb_data);
}
static const char *match_ref_pattern = NULL;
@ -401,7 +401,7 @@ static int count_slash(const char *s)
return cnt;
}
static int append_matching_ref(const char *refname, const unsigned char *sha1, void *cb_data)
static int append_matching_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
{
/* we want to allow pattern hold/<asterisk> to show all
* branches under refs/heads/hold/, and v0.99.9? to show
@ -417,10 +417,10 @@ static int append_matching_ref(const char *refname, const unsigned char *sha1, v
if (fnmatch(match_ref_pattern, tail, 0))
return 0;
if (!strncmp("refs/heads/", refname, 11))
return append_head_ref(refname, sha1, cb_data);
return append_head_ref(refname, sha1, flag, cb_data);
if (!strncmp("refs/tags/", refname, 10))
return append_tag_ref(refname, sha1, cb_data);
return append_ref(refname, sha1, cb_data);
return append_tag_ref(refname, sha1, flag, cb_data);
return append_ref(refname, sha1, flag, cb_data);
}
static void snarf_refs(int head, int tag)
@ -487,7 +487,7 @@ static void append_one_rev(const char *av)
{
unsigned char revkey[20];
if (!get_sha1(av, revkey)) {
append_ref(av, revkey, NULL);
append_ref(av, revkey, 0, NULL);
return;
}
if (strchr(av, '*') || strchr(av, '?') || strchr(av, '[')) {
@ -630,7 +630,7 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
ac--; av++;
}
head_p = resolve_ref("HEAD", head_sha1, 1);
head_p = resolve_ref("HEAD", head_sha1, 1, NULL);
if (head_p) {
head_len = strlen(head_p);
memcpy(head, head_p, head_len + 1);

View File

@ -1,5 +1,6 @@
#include "builtin.h"
#include "cache.h"
#include "refs.h"
static const char git_symbolic_ref_usage[] =
"git-symbolic-ref name [ref]";
@ -7,10 +8,13 @@ static const char git_symbolic_ref_usage[] =
static void check_symref(const char *HEAD)
{
unsigned char sha1[20];
const char *refs_heads_master = resolve_ref(HEAD, sha1, 0);
int flag;
const char *refs_heads_master = resolve_ref(HEAD, sha1, 0, &flag);
if (!refs_heads_master)
die("No such ref: %s", HEAD);
else if (!(flag & REF_ISSYMREF))
die("ref %s is not a symbolic ref", HEAD);
puts(refs_heads_master);
}

View File

@ -286,7 +286,7 @@ extern int get_sha1(const char *str, unsigned char *sha1);
extern int get_sha1_hex(const char *hex, unsigned char *sha1);
extern char *sha1_to_hex(const unsigned char *sha1); /* static buffer result! */
extern int read_ref(const char *filename, unsigned char *sha1);
extern const char *resolve_ref(const char *path, unsigned char *sha1, int);
extern const char *resolve_ref(const char *path, unsigned char *sha1, int, int *);
extern int create_symref(const char *ref, const char *refs_heads_master);
extern int validate_symref(const char *ref);

View File

@ -53,7 +53,7 @@ static void add_to_known_names(const char *path,
names = ++idx;
}
static int get_name(const char *path, const unsigned char *sha1, void *cb_data)
static int get_name(const char *path, const unsigned char *sha1, int flag, void *cb_data)
{
struct commit *commit = lookup_commit_reference_gently(sha1, 1);
struct object *object;

View File

@ -42,7 +42,7 @@ static void rev_list_push(struct commit *commit, int mark)
}
}
static int rev_list_insert_ref(const char *path, const unsigned char *sha1, void *cb_data)
static int rev_list_insert_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
{
struct object *o = deref_tag(parse_object(sha1), path, 0);
@ -253,7 +253,7 @@ done:
static struct commit_list *complete;
static int mark_complete(const char *path, const unsigned char *sha1, void *cb_data)
static int mark_complete(const char *path, const unsigned char *sha1, int flag, void *cb_data)
{
struct object *o = parse_object(sha1);

View File

@ -201,7 +201,7 @@ static int interpret_target(char *target, unsigned char *sha1)
return -1;
}
static int mark_complete(const char *path, const unsigned char *sha1, void *cb_data)
static int mark_complete(const char *path, const unsigned char *sha1, int flag, void *cb_data)
{
struct commit *commit = lookup_commit_reference_gently(sha1, 1);
if (commit) {

View File

@ -402,7 +402,7 @@ static void fsck_dir(int i, char *path)
static int default_refs;
static int fsck_handle_ref(const char *refname, const unsigned char *sha1, void *cb_data)
static int fsck_handle_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
{
struct object *obj;
@ -458,9 +458,10 @@ static void fsck_object_dir(const char *path)
static int fsck_head_link(void)
{
unsigned char sha1[20];
const char *head_points_at = resolve_ref("HEAD", sha1, 1);
int flag;
const char *head_points_at = resolve_ref("HEAD", sha1, 1, &flag);
if (!head_points_at)
if (!head_points_at || !(flag & REF_ISSYMREF))
return error("HEAD is not a symbolic ref");
if (strncmp(head_points_at, "refs/heads/", 11))
return error("HEAD points to something strange (%s)",

View File

@ -1864,7 +1864,7 @@ static int update_remote(unsigned char *sha1, struct remote_lock *lock)
static struct ref *local_refs, **local_tail;
static struct ref *remote_refs, **remote_tail;
static int one_local_ref(const char *refname, const unsigned char *sha1, void *cb_data)
static int one_local_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
{
struct ref *ref;
int len = strlen(refname) + 1;

View File

@ -12,7 +12,7 @@ static int report_status;
static char capabilities[] = "report-status";
static int capabilities_sent;
static int show_ref(const char *path, const unsigned char *sha1, void *cb_data)
static int show_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
{
if (capabilities_sent)
packet_write(1, "%s %s\n", sha1_to_hex(sha1), path);
@ -27,7 +27,7 @@ static void write_head_info(void)
{
for_each_ref(show_ref, NULL);
if (!capabilities_sent)
show_ref("capabilities^{}", null_sha1, NULL);
show_ref("capabilities^{}", null_sha1, 0, NULL);
}

44
refs.c
View File

@ -5,6 +5,7 @@
struct ref_list {
struct ref_list *next;
unsigned char flag; /* ISSYMREF? ISPACKED? */
unsigned char sha1[20];
char name[FLEX_ARRAY];
};
@ -36,7 +37,8 @@ static const char *parse_ref_line(char *line, unsigned char *sha1)
return line;
}
static struct ref_list *add_ref(const char *name, const unsigned char *sha1, struct ref_list *list)
static struct ref_list *add_ref(const char *name, const unsigned char *sha1,
int flag, struct ref_list *list)
{
int len;
struct ref_list **p = &list, *entry;
@ -58,6 +60,7 @@ static struct ref_list *add_ref(const char *name, const unsigned char *sha1, str
entry = xmalloc(sizeof(struct ref_list) + len);
hashcpy(entry->sha1, sha1);
memcpy(entry->name, name, len);
entry->flag = flag;
entry->next = *p;
*p = entry;
return list;
@ -78,7 +81,7 @@ static struct ref_list *get_packed_refs(void)
const char *name = parse_ref_line(refline, sha1);
if (!name)
continue;
list = add_ref(name, sha1, list);
list = add_ref(name, sha1, REF_ISPACKED, list);
}
fclose(f);
refs = list;
@ -104,6 +107,7 @@ static struct ref_list *get_ref_dir(const char *base, struct ref_list *list)
while ((de = readdir(dir)) != NULL) {
unsigned char sha1[20];
struct stat st;
int flag;
int namelen;
if (de->d_name[0] == '.')
@ -120,11 +124,11 @@ static struct ref_list *get_ref_dir(const char *base, struct ref_list *list)
list = get_ref_dir(ref, list);
continue;
}
if (read_ref(ref, sha1) < 0) {
if (!resolve_ref(ref, sha1, 1, &flag)) {
error("%s points nowhere!", ref);
continue;
}
list = add_ref(ref, sha1, list);
list = add_ref(ref, sha1, flag, list);
}
free(ref);
closedir(dir);
@ -147,12 +151,15 @@ static struct ref_list *get_loose_refs(void)
/* We allow "recursive" symbolic refs. Only within reason, though */
#define MAXDEPTH 5
const char *resolve_ref(const char *ref, unsigned char *sha1, int reading)
const char *resolve_ref(const char *ref, unsigned char *sha1, int reading, int *flag)
{
int depth = MAXDEPTH, len;
char buffer[256];
static char ref_buffer[256];
if (flag)
*flag = 0;
for (;;) {
const char *path = git_path("%s", ref);
struct stat st;
@ -174,6 +181,8 @@ const char *resolve_ref(const char *ref, unsigned char *sha1, int reading)
while (list) {
if (!strcmp(ref, list->name)) {
hashcpy(sha1, list->sha1);
if (flag)
*flag |= REF_ISPACKED;
return ref;
}
list = list->next;
@ -191,6 +200,8 @@ const char *resolve_ref(const char *ref, unsigned char *sha1, int reading)
buffer[len] = 0;
strcpy(ref_buffer, buffer);
ref = ref_buffer;
if (flag)
*flag |= REF_ISSYMREF;
continue;
}
}
@ -219,6 +230,8 @@ const char *resolve_ref(const char *ref, unsigned char *sha1, int reading)
buf[len] = 0;
memcpy(ref_buffer, buf, len + 1);
ref = ref_buffer;
if (flag)
*flag |= REF_ISSYMREF;
}
if (len < 40 || get_sha1_hex(buffer, sha1))
return NULL;
@ -270,12 +283,13 @@ int create_symref(const char *ref_target, const char *refs_heads_master)
int read_ref(const char *ref, unsigned char *sha1)
{
if (resolve_ref(ref, sha1, 1))
if (resolve_ref(ref, sha1, 1, NULL))
return 0;
return -1;
}
static int do_for_each_ref(const char *base, each_ref_fn fn, int trim, void *cb_data)
static int do_for_each_ref(const char *base, each_ref_fn fn, int trim,
void *cb_data)
{
int retval;
struct ref_list *packed = get_packed_refs();
@ -303,7 +317,8 @@ static int do_for_each_ref(const char *base, each_ref_fn fn, int trim, void *cb_
error("%s does not point to a valid object!", entry->name);
continue;
}
retval = fn(entry->name + trim, entry->sha1, cb_data);
retval = fn(entry->name + trim, entry->sha1,
entry->flag, cb_data);
if (retval)
return retval;
}
@ -311,7 +326,8 @@ static int do_for_each_ref(const char *base, each_ref_fn fn, int trim, void *cb_
packed = packed ? packed : loose;
while (packed) {
if (!strncmp(base, packed->name, trim)) {
retval = fn(packed->name + trim, packed->sha1, cb_data);
retval = fn(packed->name + trim, packed->sha1,
packed->flag, cb_data);
if (retval)
return retval;
}
@ -323,8 +339,10 @@ static int do_for_each_ref(const char *base, each_ref_fn fn, int trim, void *cb_
int head_ref(each_ref_fn fn, void *cb_data)
{
unsigned char sha1[20];
if (!read_ref("HEAD", sha1))
return fn("HEAD", sha1, cb_data);
int flag;
if (resolve_ref("HEAD", sha1, 1, &flag))
return fn("HEAD", sha1, flag, cb_data);
return 0;
}
@ -415,7 +433,7 @@ int check_ref_format(const char *ref)
static struct ref_lock *verify_lock(struct ref_lock *lock,
const unsigned char *old_sha1, int mustexist)
{
if (!resolve_ref(lock->ref_name, lock->old_sha1, mustexist)) {
if (!resolve_ref(lock->ref_name, lock->old_sha1, mustexist, NULL)) {
error("Can't verify ref %s", lock->ref_name);
unlock_ref(lock);
return NULL;
@ -441,7 +459,7 @@ static struct ref_lock *lock_ref_sha1_basic(const char *ref,
lock = xcalloc(1, sizeof(struct ref_lock));
lock->lock_fd = -1;
ref = resolve_ref(ref, lock->old_sha1, mustexist);
ref = resolve_ref(ref, lock->old_sha1, mustexist, NULL);
if (!ref) {
int last_errno = errno;
error("unable to resolve reference %s: %s",

4
refs.h
View File

@ -14,7 +14,9 @@ struct ref_lock {
* Calls the specified function for each ref file until it returns nonzero,
* and returns the value
*/
typedef int each_ref_fn(const char *refname, const unsigned char *sha1, void *cb_data);
#define REF_ISSYMREF 01
#define REF_ISPACKED 02
typedef int each_ref_fn(const char *refname, const unsigned char *sha1, int flags, void *cb_data);
extern int head_ref(each_ref_fn, void *);
extern int for_each_ref(each_ref_fn, void *);
extern int for_each_tag_ref(each_ref_fn, void *);

View File

@ -466,7 +466,7 @@ static void limit_list(struct rev_info *revs)
static int all_flags;
static struct rev_info *all_revs;
static int handle_one_ref(const char *path, const unsigned char *sha1, void *cb_data)
static int handle_one_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
{
struct object *object = get_reference(all_revs, path, sha1, all_flags);
add_pending_object(all_revs, object, "");

View File

@ -215,7 +215,7 @@ static int ref_newer(const unsigned char *new_sha1,
static struct ref *local_refs, **local_tail;
static struct ref *remote_refs, **remote_tail;
static int one_local_ref(const char *refname, const unsigned char *sha1, void *cb_data)
static int one_local_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
{
struct ref *ref;
int len = strlen(refname) + 1;

View File

@ -7,7 +7,7 @@
/* refs */
static FILE *info_ref_fp;
static int add_info_ref(const char *path, const unsigned char *sha1, void *cb_data)
static int add_info_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
{
struct object *o = parse_object(sha1);

View File

@ -276,7 +276,7 @@ static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
for (p = fmt; *p; p++) {
this_result = refs_found ? sha1_from_ref : sha1;
ref = resolve_ref(mkpath(*p, len, str), this_result, 1);
ref = resolve_ref(mkpath(*p, len, str), this_result, 1, NULL);
if (ref) {
if (!refs_found++)
real_ref = xstrdup(ref);

View File

@ -416,7 +416,7 @@ static void receive_needs(void)
}
}
static int send_ref(const char *refname, const unsigned char *sha1, void *cb_data)
static int send_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
{
static const char *capabilities = "multi_ack thin-pack side-band side-band-64k";
struct object *o = parse_object(sha1);

View File

@ -41,7 +41,7 @@ void wt_status_prepare(struct wt_status *s)
s->is_initial = get_sha1("HEAD", sha1) ? 1 : 0;
head = resolve_ref("HEAD", sha1, 0);
head = resolve_ref("HEAD", sha1, 0, NULL);
s->branch = head ? xstrdup(head) : NULL;
s->reference = "HEAD";