Browse Source

[PATCH] Operations on refs

This patch adds code to read a hash out of a specified file under
{GIT_DIR}/refs/, and to write such files atomically and optionally with an
compare and lock.

Signed-off-by: Daniel Barkalow <barkalow@iabervon.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
maint
Daniel Barkalow 20 years ago committed by Linus Torvalds
parent
commit
95fc75129a
  1. 3
      Makefile
  2. 1
      cache.h
  3. 173
      refs.c
  4. 21
      refs.h
  5. 11
      sha1_file.c

3
Makefile

@ -40,7 +40,8 @@ install: $(PROG) $(SCRIPTS) @@ -40,7 +40,8 @@ install: $(PROG) $(SCRIPTS)
$(INSTALL) $(PROG) $(SCRIPTS) $(dest)$(bin)

LIB_OBJS=read-cache.o sha1_file.o usage.o object.o commit.o tree.o blob.o \
tag.o delta.o date.o index.o diff-delta.o patch-delta.o entry.o epoch.o
tag.o delta.o date.o index.o diff-delta.o patch-delta.o entry.o \
epoch.o refs.o
LIB_FILE=libgit.a
LIB_H=cache.h object.h blob.h tree.h commit.h tag.h delta.h epoch.h


1
cache.h

@ -117,6 +117,7 @@ extern unsigned int active_nr, active_alloc, active_cache_changed; @@ -117,6 +117,7 @@ extern unsigned int active_nr, active_alloc, active_cache_changed;
#define INDEX_ENVIRONMENT "GIT_INDEX_FILE"

extern char *get_object_directory(void);
extern char *get_refs_directory(void);
extern char *get_index_file(void);

#define ALTERNATE_DB_ENVIRONMENT "GIT_ALTERNATE_OBJECT_DIRECTORIES"

173
refs.c

@ -0,0 +1,173 @@ @@ -0,0 +1,173 @@
#include "refs.h"
#include "cache.h"

#include <errno.h>

static char *ref_file_name(const char *ref)
{
char *base = get_refs_directory();
int baselen = strlen(base);
int reflen = strlen(ref);
char *ret = xmalloc(baselen + 2 + reflen);
sprintf(ret, "%s/%s", base, ref);
return ret;
}

static char *ref_lock_file_name(const char *ref)
{
char *base = get_refs_directory();
int baselen = strlen(base);
int reflen = strlen(ref);
char *ret = xmalloc(baselen + 7 + reflen);
sprintf(ret, "%s/%s.lock", base, ref);
return ret;
}

static int read_ref_file(const char *filename, unsigned char *sha1) {
int fd = open(filename, O_RDONLY);
char hex[41];
if (fd < 0) {
return error("Couldn't open %s\n", filename);
}
if ((read(fd, hex, 41) < 41) ||
(hex[40] != '\n') ||
get_sha1_hex(hex, sha1)) {
error("Couldn't read a hash from %s\n", filename);
close(fd);
return -1;
}
close(fd);
return 0;
}

int get_ref_sha1(const char *ref, unsigned char *sha1)
{
char *filename;
int retval;
if (check_ref_format(ref))
return -1;
filename = ref_file_name(ref);
retval = read_ref_file(filename, sha1);
free(filename);
return retval;
}

static int lock_ref_file(const char *filename, const char *lock_filename,
const unsigned char *old_sha1)
{
int fd = open(lock_filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
unsigned char current_sha1[20];
int retval;
if (fd < 0) {
return error("Couldn't open lock file for %s: %s",
filename, strerror(errno));
}
retval = read_ref_file(filename, current_sha1);
if (old_sha1) {
if (retval) {
close(fd);
unlink(lock_filename);
return error("Could not read the current value of %s",
filename);
}
if (memcmp(current_sha1, old_sha1, 20)) {
close(fd);
unlink(lock_filename);
error("The current value of %s is %s",
filename, sha1_to_hex(current_sha1));
return error("Expected %s",
sha1_to_hex(old_sha1));
}
} else {
if (!retval) {
close(fd);
unlink(lock_filename);
return error("Unexpectedly found a value of %s for %s",
sha1_to_hex(current_sha1), filename);
}
}
return fd;
}

int lock_ref_sha1(const char *ref, const unsigned char *old_sha1)
{
char *filename;
char *lock_filename;
int retval;
if (check_ref_format(ref))
return -1;
filename = ref_file_name(ref);
lock_filename = ref_lock_file_name(ref);
retval = lock_ref_file(filename, lock_filename, old_sha1);
free(filename);
free(lock_filename);
return retval;
}

static int write_ref_file(const char *filename,
const char *lock_filename, int fd,
const unsigned char *sha1)
{
char *hex = sha1_to_hex(sha1);
char term = '\n';
if (write(fd, hex, 40) < 40 ||
write(fd, &term, 1) < 1) {
error("Couldn't write %s\n", filename);
close(fd);
return -1;
}
close(fd);
rename(lock_filename, filename);
return 0;
}

int write_ref_sha1(const char *ref, int fd, const unsigned char *sha1)
{
char *filename;
char *lock_filename;
int retval;
if (fd < 0)
return -1;
if (check_ref_format(ref))
return -1;
filename = ref_file_name(ref);
lock_filename = ref_lock_file_name(ref);
retval = write_ref_file(filename, lock_filename, fd, sha1);
free(filename);
free(lock_filename);
return retval;
}

int check_ref_format(const char *ref)
{
char *middle;
if (ref[0] == '.' || ref[0] == '/')
return -1;
middle = strchr(ref, '/');
if (!middle || !middle[1])
return -1;
if (strchr(middle + 1, '/'))
return -1;
return 0;
}

int write_ref_sha1_unlocked(const char *ref, const unsigned char *sha1)
{
char *filename;
char *lock_filename;
int fd;
int retval;
if (check_ref_format(ref))
return -1;
filename = ref_file_name(ref);
lock_filename = ref_lock_file_name(ref);
fd = open(lock_filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
if (fd < 0) {
error("Writing %s", lock_filename);
perror("Open");
}
retval = write_ref_file(filename, lock_filename, fd, sha1);
free(filename);
free(lock_filename);
return retval;
}

21
refs.h

@ -0,0 +1,21 @@ @@ -0,0 +1,21 @@
#ifndef REFS_H
#define REFS_H

/** Reads the refs file specified into sha1 **/
extern int get_ref_sha1(const char *ref, unsigned char *sha1);

/** Locks ref and returns the fd to give to write_ref_sha1() if the ref
* has the given value currently; otherwise, returns -1.
**/
extern int lock_ref_sha1(const char *ref, const unsigned char *old_sha1);

/** Writes sha1 into the refs file specified, locked with the given fd. **/
extern int write_ref_sha1(const char *ref, int fd, const unsigned char *sha1);

/** Writes sha1 into the refs file specified. **/
extern int write_ref_sha1_unlocked(const char *ref, const unsigned char *sha1);

/** Returns 0 if target has the right format for a ref. **/
extern int check_ref_format(const char *target);

#endif /* REFS_H */

11
sha1_file.c

@ -58,7 +58,7 @@ static int get_sha1_file(const char *path, unsigned char *result) @@ -58,7 +58,7 @@ static int get_sha1_file(const char *path, unsigned char *result)
return get_sha1_hex(buffer, result);
}

static char *git_dir, *git_object_dir, *git_index_file;
static char *git_dir, *git_object_dir, *git_index_file, *git_refs_dir;
static void setup_git_env(void)
{
git_dir = gitenv(GIT_DIR_ENVIRONMENT);
@ -69,6 +69,8 @@ static void setup_git_env(void) @@ -69,6 +69,8 @@ static void setup_git_env(void)
git_object_dir = xmalloc(strlen(git_dir) + 9);
sprintf(git_object_dir, "%s/objects", git_dir);
}
git_refs_dir = xmalloc(strlen(git_dir) + 6);
sprintf(git_refs_dir, "%s/refs", git_dir);
git_index_file = gitenv(INDEX_ENVIRONMENT);
if (!git_index_file) {
git_index_file = xmalloc(strlen(git_dir) + 7);
@ -83,6 +85,13 @@ char *get_object_directory(void) @@ -83,6 +85,13 @@ char *get_object_directory(void)
return git_object_dir;
}

char *get_refs_directory(void)
{
if (!git_refs_dir)
setup_git_env();
return git_refs_dir;
}

char *get_index_file(void)
{
if (!git_index_file)

Loading…
Cancel
Save