1
0
Fork 0
mirror of https://github.com/git/git.git synced 2024-06-22 08:19:22 +02:00

Merge three separate "fetch refs" functions

It really just boils down to one "get_remote_heads()" function, and a
common "struct ref" structure definition.
This commit is contained in:
Linus Torvalds 2005-07-16 13:55:50 -07:00
parent d089391c00
commit d1c133f5d4
5 changed files with 67 additions and 109 deletions

View File

@ -285,10 +285,18 @@ struct pack_entry {
struct packed_git *p; struct packed_git *p;
}; };
struct ref {
struct ref *next;
unsigned char old_sha1[20];
unsigned char new_sha1[20];
char name[0];
};
extern int git_connect(int fd[2], char *url, const char *prog); extern int git_connect(int fd[2], char *url, const char *prog);
extern int finish_connect(pid_t pid); extern int finish_connect(pid_t pid);
extern int path_match(const char *path, int nr, char **match); extern int path_match(const char *path, int nr, char **match);
extern int get_ack(int fd, unsigned char *result_sha1); extern int get_ack(int fd, unsigned char *result_sha1);
extern struct ref **get_remote_heads(int in, struct ref **list, int nr_match, char **match);
extern void prepare_packed_git(void); extern void prepare_packed_git(void);
extern int use_packed_git(struct packed_git *); extern int use_packed_git(struct packed_git *);

View File

@ -7,50 +7,12 @@ static int quiet;
static const char clone_pack_usage[] = "git-clone-pack [-q] [--exec=<git-upload-pack>] [<host>:]<directory> [<heads>]*"; static const char clone_pack_usage[] = "git-clone-pack [-q] [--exec=<git-upload-pack>] [<host>:]<directory> [<heads>]*";
static const char *exec = "git-upload-pack"; static const char *exec = "git-upload-pack";
struct ref {
struct ref *next;
unsigned char sha1[20];
char name[0];
};
static struct ref *get_remote_refs(int fd, int nr_match, char **match)
{
struct ref *ref_list = NULL, **next_ref = &ref_list;
for (;;) {
static char line[1000];
unsigned char sha1[20];
struct ref *ref;
char *refname;
int len;
len = packet_read_line(fd, line, sizeof(line));
if (!len)
break;
if (line[len-1] == '\n')
line[--len] = 0;
if (len < 42 || get_sha1_hex(line, sha1))
die("git-clone-pack: protocol error - expected ref descriptor, got '%s'", line);
refname = line+41;
len = len-40;
if (nr_match && !path_match(refname, nr_match, match))
continue;
ref = xmalloc(sizeof(struct ref) + len);
ref->next = NULL;
memcpy(ref->sha1, sha1, 20);
memcpy(ref->name, refname, len);
*next_ref = ref;
next_ref = &ref->next;
}
return ref_list;
}
static void clone_handshake(int fd[2], struct ref *ref) static void clone_handshake(int fd[2], struct ref *ref)
{ {
unsigned char sha1[20]; unsigned char sha1[20];
while (ref) { while (ref) {
packet_write(fd[1], "want %s\n", sha1_to_hex(ref->sha1)); packet_write(fd[1], "want %s\n", sha1_to_hex(ref->old_sha1));
ref = ref->next; ref = ref->next;
} }
packet_flush(fd[1]); packet_flush(fd[1]);
@ -77,7 +39,7 @@ static void write_one_ref(struct ref *ref)
fd = open(path, O_CREAT | O_EXCL | O_WRONLY, 0666); fd = open(path, O_CREAT | O_EXCL | O_WRONLY, 0666);
if (fd < 0) if (fd < 0)
die("unable to create ref %s", ref->name); die("unable to create ref %s", ref->name);
hex = sha1_to_hex(ref->sha1); hex = sha1_to_hex(ref->old_sha1);
hex[40] = '\n'; hex[40] = '\n';
if (write(fd, hex, 41) != 41) if (write(fd, hex, 41) != 41)
die("unable to write ref %s", ref->name); die("unable to write ref %s", ref->name);
@ -98,7 +60,7 @@ static void write_refs(struct ref *ref)
while (ref) { while (ref) {
if (is_master(ref)) if (is_master(ref))
master_ref = ref; master_ref = ref;
if (head && !memcmp(ref->sha1, head->sha1, 20)) { if (head && !memcmp(ref->old_sha1, head->old_sha1, 20)) {
if (!head_ptr || ref == master_ref) if (!head_ptr || ref == master_ref)
head_ptr = ref; head_ptr = ref;
} }
@ -142,7 +104,7 @@ static int clone_pack(int fd[2], int nr_match, char **match)
int status; int status;
pid_t pid; pid_t pid;
refs = get_remote_refs(fd[0], nr_match, match); get_remote_heads(fd[0], &refs, nr_match, match);
if (!refs) { if (!refs) {
packet_flush(fd[1]); packet_flush(fd[1]);
die("no matching remote head"); die("no matching remote head");

View File

@ -7,6 +7,41 @@
#include <arpa/inet.h> #include <arpa/inet.h>
#include <netdb.h> #include <netdb.h>
/*
* Read all the refs from the other end
*/
struct ref **get_remote_heads(int in, struct ref **list, int nr_match, char **match)
{
*list = NULL;
for (;;) {
struct ref *ref;
unsigned char old_sha1[20];
static char buffer[1000];
char *name;
int len;
len = packet_read_line(in, buffer, sizeof(buffer));
if (!len)
break;
if (buffer[len-1] == '\n')
buffer[--len] = 0;
if (len < 42 || get_sha1_hex(buffer, old_sha1) || buffer[40] != ' ')
die("protocol error: expected sha/ref, got '%s'", buffer);
name = buffer + 41;
if (nr_match && !path_match(name, nr_match, match))
continue;
ref = xmalloc(sizeof(*ref) + len - 40);
memcpy(ref->old_sha1, old_sha1, 20);
memset(ref->new_sha1, 0, 20);
memcpy(ref->name, buffer + 41, len - 40);
ref->next = NULL;
*list = ref;
list = &ref->next;
}
return list;
}
int get_ack(int fd, unsigned char *result_sha1) int get_ack(int fd, unsigned char *result_sha1)
{ {
static char line[1000]; static char line[1000];

View File

@ -53,44 +53,28 @@ static int find_common(int fd[2], unsigned char *result_sha1, unsigned char *rem
return retval; return retval;
} }
static int get_remote_heads(int fd, int nr_match, char **match, unsigned char *result) /*
{ * Eventually we'll want to be able to fetch multiple heads.
int count = 0; *
* Right now we'll just require a single match.
for (;;) { */
static char line[1000];
unsigned char sha1[20];
char *refname;
int len;
len = packet_read_line(fd, line, sizeof(line));
if (!len)
break;
if (line[len-1] == '\n')
line[--len] = 0;
if (len < 42 || get_sha1_hex(line, sha1))
die("git-fetch-pack: protocol error - expected ref descriptor, got '%s'", line);
refname = line+41;
if (nr_match && !path_match(refname, nr_match, match))
continue;
count++;
memcpy(result, sha1, 20);
}
return count;
}
static int fetch_pack(int fd[2], int nr_match, char **match) static int fetch_pack(int fd[2], int nr_match, char **match)
{ {
unsigned char sha1[20], remote[20]; struct ref *ref;
int heads, status; unsigned char sha1[20];
int status;
pid_t pid; pid_t pid;
heads = get_remote_heads(fd[0], nr_match, match, remote); get_remote_heads(fd[0], &ref, nr_match, match);
if (heads != 1) { if (!ref) {
packet_flush(fd[1]); packet_flush(fd[1]);
die(heads ? "multiple remote heads" : "no matching remote head"); die("no matching remote head");
} }
if (find_common(fd, sha1, remote) < 0) if (ref->next) {
packet_flush(fd[1]);
die("multiple remote heads");
}
if (find_common(fd, sha1, ref->old_sha1) < 0)
die("git-fetch-pack: no common commits"); die("git-fetch-pack: no common commits");
pid = fork(); pid = fork();
if (pid < 0) if (pid < 0)
@ -113,7 +97,7 @@ static int fetch_pack(int fd[2], int nr_match, char **match)
int code = WEXITSTATUS(status); int code = WEXITSTATUS(status);
if (code) if (code)
die("git-unpack-objects died with error code %d", code); die("git-unpack-objects died with error code %d", code);
puts(sha1_to_hex(remote)); puts(sha1_to_hex(ref->old_sha1));
return 0; return 0;
} }
if (WIFSIGNALED(status)) { if (WIFSIGNALED(status)) {

View File

@ -7,13 +7,6 @@ static const char send_pack_usage[] =
static const char *exec = "git-receive-pack"; static const char *exec = "git-receive-pack";
static int send_all = 0; static int send_all = 0;
struct ref {
struct ref *next;
unsigned char old_sha1[20];
unsigned char new_sha1[20];
char name[0];
};
static int is_zero_sha1(const unsigned char *sha1) static int is_zero_sha1(const unsigned char *sha1)
{ {
int i; int i;
@ -170,36 +163,12 @@ static int try_to_match(const char *refname, const unsigned char *sha1)
static int send_pack(int in, int out, int nr_match, char **match) static int send_pack(int in, int out, int nr_match, char **match)
{ {
struct ref *ref_list = NULL, **last_ref = &ref_list; struct ref *ref_list, **last_ref;
struct ref *ref; struct ref *ref;
int new_refs; int new_refs;
/* /* First we get all heads, whether matching or not.. */
* Read all the refs from the other end last_ref = get_remote_heads(in, &ref_list, 0, NULL);
*/
for (;;) {
unsigned char old_sha1[20];
static char buffer[1000];
char *name;
int len;
len = packet_read_line(in, buffer, sizeof(buffer));
if (!len)
break;
if (buffer[len-1] == '\n')
buffer[--len] = 0;
if (len < 42 || get_sha1_hex(buffer, old_sha1) || buffer[40] != ' ')
die("protocol error: expected sha/ref, got '%s'", buffer);
name = buffer + 41;
ref = xmalloc(sizeof(*ref) + len - 40);
memcpy(ref->old_sha1, old_sha1, 20);
memset(ref->new_sha1, 0, 20);
memcpy(ref->name, buffer + 41, len - 40);
ref->next = NULL;
*last_ref = ref;
last_ref = &ref->next;
}
/* /*
* Go through the refs, see if we want to update * Go through the refs, see if we want to update