#include "cache.h"
#include "transport.h"
#include "run-command.h"
#ifndef NO_CURL
#include "http.h"
#endif
#include "pkt-line.h"
#include "fetch-pack.h"
#include "send-pack.h"
#include "walker.h"
#include "bundle.h"
#include "dir.h"
#include "refs.h"
/* rsync support */
/*
* We copy packed-refs and refs/ into a temporary file, then read the
* loose refs recursively (sorting whenever possible), and then inserting
* those packed refs that are not yet in the list (not validating, but
* assuming that the file is sorted).
*
* Appears refactoring this from refs.c is too cumbersome.
*/
static int str_cmp(const void *a, const void *b)
{
const char *s1 = a;
const char *s2 = b;
return strcmp(s1, s2);
}
/* path->buf + name_offset is expected to point to "refs/" */
static int read_loose_refs(struct strbuf *path, int name_offset,
struct ref **tail)
{
DIR *dir = opendir(path->buf);
struct dirent *de;
struct {
char **entries;
int nr, alloc;
} list;
int i, pathlen;
if (!dir)
return -1;
memset (&list, 0, sizeof(list));
while ((de = readdir(dir))) {
if (is_dot_or_dotdot(de->d_name))
continue;
ALLOC_GROW(list.entries, list.nr + 1, list.alloc);
list.entries[list.nr++] = xstrdup(de->d_name);
}
closedir(dir);
/* sort the list */
qsort(list.entries, list.nr, sizeof(char *), str_cmp);
pathlen = path->len;
strbuf_addch(path, '/');
for (i = 0; i < list.nr; i++, strbuf_setlen(path, pathlen + 1)) {
strbuf_addstr(path, list.entries[i]);
if (read_loose_refs(path, name_offset, tail)) {
int fd = open(path->buf, O_RDONLY);
char buffer[40];
struct ref *next;
if (fd < 0)
continue;
next = alloc_ref(path->buf + name_offset);
if (read_in_full(fd, buffer, 40) != 40 ||
get_sha1_hex(buffer, next->old_sha1)) {
close(fd);
free(next);
continue;
}
close(fd);
(*tail)->next = next;
*tail = next;
}
}
strbuf_setlen(path, pathlen);
for (i = 0; i < list.nr; i++)
free(list.entries[i]);
free(list.entries);
return 0;
}
/* insert the packed refs for which no loose refs were found */
static void insert_packed_refs(const char *packed_refs, struct ref **list)
{
FILE *f = fopen(packed_refs, "r");
static char buffer[PATH_MAX];
if (!f)
return;
for (;;) {
int cmp = cmp, len;
if (!fgets(buffer, sizeof(buffer), f)) {
fclose(f);
return;
}
if (hexval(buffer[0]) > 0xf)
continue;
len = strlen(buffer);
if (len && buffer[len - 1] == '\n')
buffer[--len] = '\0';
if (len < 41)
continue;
while ((*list)->next &&
(cmp = strcmp(buffer + 41,
(*list)->next->name)) > 0)
list = &(*list)->next;
if (!(*list)->next || cmp < 0) {
struct ref *next = alloc_ref(buffer + 41);
buffer[40] = '\0';
if (get_sha1_hex(buffer, next->old_sha1)) {
warning ("invalid SHA-1: %s", buffer);
free(next);
continue;
}
next->next = (*list)->next;
(*list)->next = next;
list = &(*list)->next;
}
}
}
static const char *rsync_url(const char *url)
{
return prefixcmp(url, "rsync://") ? skip_prefix(url, "rsync:") : url;
}
static struct ref *get_refs_via_rsync(struct transport *transport, int for_push)
{
struct strbuf buf = STRBUF_INIT, temp_dir = STRBUF_INIT;
struct ref dummy, *tail = &dummy;
struct child_process rsync;
const char *args[5];
int temp_dir_len;
if (for_push)
return NULL;
/* copy the refs to the temporary directory */
strbuf_addstr(&temp_dir, git_path("rsync-refs-XXXXXX"));
if (!mkdtemp(temp_dir.buf))
die_errno ("Could not make temporary directory");
temp_dir_len = temp_dir.len;
strbuf_addstr(&buf, rsync_url(transport->url));
strbuf_addstr(&buf, "/refs");
memset(&rsync, 0, sizeof(rsync));
rsync.argv = args;
rsync.stdout_to_stderr = 1;
args[0] = "rsync";
args[1] = (transport->verbose > 0) ? "-rv" : "-r";
args[2] = buf.buf;
args[3] = temp_dir.buf;
args[4] = NULL;
if (run_command(&rsync))
|