summaryrefslogtreecommitdiff
path: root/sha1_file.c
diff options
context:
space:
mode:
Diffstat (limited to 'sha1_file.c')
-rw-r--r--sha1_file.c94
1 files changed, 52 insertions, 42 deletions
diff --git a/sha1_file.c b/sha1_file.c
index 36f85455e6..afe4b90f6e 100644
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -74,6 +74,18 @@ static struct cached_object *find_cached_object(const unsigned char *sha1)
return NULL;
}
+
+static enum safe_crlf get_safe_crlf(unsigned flags)
+{
+ if (flags & HASH_RENORMALIZE)
+ return SAFE_CRLF_RENORMALIZE;
+ else if (flags & HASH_WRITE_OBJECT)
+ return safe_crlf;
+ else
+ return SAFE_CRLF_FALSE;
+}
+
+
int mkdir_in_gitdir(const char *path)
{
if (mkdir(path, 0777)) {
@@ -459,19 +471,19 @@ struct alternate_object_database *alloc_alt_odb(const char *dir)
void add_to_alternates_file(const char *reference)
{
- struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
+ struct lock_file lock = LOCK_INIT;
char *alts = git_pathdup("objects/info/alternates");
FILE *in, *out;
+ int found = 0;
- hold_lock_file_for_update(lock, alts, LOCK_DIE_ON_ERROR);
- out = fdopen_lock_file(lock, "w");
+ hold_lock_file_for_update(&lock, alts, LOCK_DIE_ON_ERROR);
+ out = fdopen_lock_file(&lock, "w");
if (!out)
die_errno("unable to fdopen alternates lockfile");
in = fopen(alts, "r");
if (in) {
struct strbuf line = STRBUF_INIT;
- int found = 0;
while (strbuf_getline(&line, in) != EOF) {
if (!strcmp(reference, line.buf)) {
@@ -483,18 +495,15 @@ void add_to_alternates_file(const char *reference)
strbuf_release(&line);
fclose(in);
-
- if (found) {
- rollback_lock_file(lock);
- lock = NULL;
- }
}
else if (errno != ENOENT)
die_errno("unable to read alternates file");
- if (lock) {
+ if (found) {
+ rollback_lock_file(&lock);
+ } else {
fprintf_or_die(out, "%s\n", reference);
- if (commit_lock_file(lock))
+ if (commit_lock_file(&lock))
die_errno("unable to move new alternates file into place");
if (alt_odb_tail)
link_alt_odb_entries(reference, '\n', NULL, 0);
@@ -1155,6 +1164,9 @@ int sha1_object_info_extended(const unsigned char *sha1, struct object_info *oi,
lookup_replace_object(sha1) :
sha1;
+ if (is_null_sha1(real))
+ return -1;
+
if (!oi)
oi = &blank_oi;
@@ -1666,7 +1678,7 @@ static void check_tag(const void *buf, size_t size)
die("corrupt tag");
}
-static int index_mem(unsigned char *sha1, void *buf, size_t size,
+static int index_mem(struct object_id *oid, void *buf, size_t size,
enum object_type type,
const char *path, unsigned flags)
{
@@ -1682,7 +1694,7 @@ static int index_mem(unsigned char *sha1, void *buf, size_t size,
if ((type == OBJ_BLOB) && path) {
struct strbuf nbuf = STRBUF_INIT;
if (convert_to_git(&the_index, path, buf, size, &nbuf,
- write_object ? safe_crlf : SAFE_CRLF_FALSE)) {
+ get_safe_crlf(flags))) {
buf = strbuf_detach(&nbuf, &size);
re_allocated = 1;
}
@@ -1697,15 +1709,15 @@ static int index_mem(unsigned char *sha1, void *buf, size_t size,
}
if (write_object)
- ret = write_sha1_file(buf, size, typename(type), sha1);
+ ret = write_sha1_file(buf, size, typename(type), oid->hash);
else
- ret = hash_sha1_file(buf, size, typename(type), sha1);
+ ret = hash_sha1_file(buf, size, typename(type), oid->hash);
if (re_allocated)
free(buf);
return ret;
}
-static int index_stream_convert_blob(unsigned char *sha1, int fd,
+static int index_stream_convert_blob(struct object_id *oid, int fd,
const char *path, unsigned flags)
{
int ret;
@@ -1716,26 +1728,26 @@ static int index_stream_convert_blob(unsigned char *sha1, int fd,
assert(would_convert_to_git_filter_fd(path));
convert_to_git_filter_fd(&the_index, path, fd, &sbuf,
- write_object ? safe_crlf : SAFE_CRLF_FALSE);
+ get_safe_crlf(flags));
if (write_object)
ret = write_sha1_file(sbuf.buf, sbuf.len, typename(OBJ_BLOB),
- sha1);
+ oid->hash);
else
ret = hash_sha1_file(sbuf.buf, sbuf.len, typename(OBJ_BLOB),
- sha1);
+ oid->hash);
strbuf_release(&sbuf);
return ret;
}
-static int index_pipe(unsigned char *sha1, int fd, enum object_type type,
+static int index_pipe(struct object_id *oid, int fd, enum object_type type,
const char *path, unsigned flags)
{
struct strbuf sbuf = STRBUF_INIT;
int ret;
if (strbuf_read(&sbuf, fd, 4096) >= 0)
- ret = index_mem(sha1, sbuf.buf, sbuf.len, type, path, flags);
+ ret = index_mem(oid, sbuf.buf, sbuf.len, type, path, flags);
else
ret = -1;
strbuf_release(&sbuf);
@@ -1744,14 +1756,14 @@ static int index_pipe(unsigned char *sha1, int fd, enum object_type type,
#define SMALL_FILE_SIZE (32*1024)
-static int index_core(unsigned char *sha1, int fd, size_t size,
+static int index_core(struct object_id *oid, int fd, size_t size,
enum object_type type, const char *path,
unsigned flags)
{
int ret;
if (!size) {
- ret = index_mem(sha1, "", size, type, path, flags);
+ ret = index_mem(oid, "", size, type, path, flags);
} else if (size <= SMALL_FILE_SIZE) {
char *buf = xmalloc(size);
ssize_t read_result = read_in_full(fd, buf, size);
@@ -1762,11 +1774,11 @@ static int index_core(unsigned char *sha1, int fd, size_t size,
ret = error("short read while indexing %s",
path ? path : "<unknown>");
else
- ret = index_mem(sha1, buf, size, type, path, flags);
+ ret = index_mem(oid, buf, size, type, path, flags);
free(buf);
} else {
void *buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
- ret = index_mem(sha1, buf, size, type, path, flags);
+ ret = index_mem(oid, buf, size, type, path, flags);
munmap(buf, size);
}
return ret;
@@ -1804,12 +1816,12 @@ int index_fd(struct object_id *oid, int fd, struct stat *st,
* die() for large files.
*/
if (type == OBJ_BLOB && path && would_convert_to_git_filter_fd(path))
- ret = index_stream_convert_blob(oid->hash, fd, path, flags);
+ ret = index_stream_convert_blob(oid, fd, path, flags);
else if (!S_ISREG(st->st_mode))
- ret = index_pipe(oid->hash, fd, type, path, flags);
+ ret = index_pipe(oid, fd, type, path, flags);
else if (st->st_size <= big_file_threshold || type != OBJ_BLOB ||
(path && would_convert_to_git(&the_index, path)))
- ret = index_core(oid->hash, fd, xsize_t(st->st_size), type, path,
+ ret = index_core(oid, fd, xsize_t(st->st_size), type, path,
flags);
else
ret = index_stream(oid, fd, xsize_t(st->st_size), type, path,
@@ -1843,7 +1855,7 @@ int index_path(struct object_id *oid, const char *path, struct stat *st, unsigne
strbuf_release(&sb);
break;
case S_IFDIR:
- return resolve_gitlink_ref(path, "HEAD", oid->hash);
+ return resolve_gitlink_ref(path, "HEAD", oid);
default:
return error("%s: unsupported file type", path);
}
@@ -1886,6 +1898,7 @@ int for_each_file_in_obj_subdir(unsigned int subdir_nr,
DIR *dir;
struct dirent *de;
int r = 0;
+ struct object_id oid;
if (subdir_nr > 0xff)
BUG("invalid loose object subdirectory: %x", subdir_nr);
@@ -1903,6 +1916,8 @@ int for_each_file_in_obj_subdir(unsigned int subdir_nr,
return r;
}
+ oid.hash[0] = subdir_nr;
+
while ((de = readdir(dir))) {
if (is_dot_or_dotdot(de->d_name))
continue;
@@ -1910,20 +1925,15 @@ int for_each_file_in_obj_subdir(unsigned int subdir_nr,
strbuf_setlen(path, baselen);
strbuf_addf(path, "/%s", de->d_name);
- if (strlen(de->d_name) == GIT_SHA1_HEXSZ - 2) {
- char hex[GIT_MAX_HEXSZ+1];
- struct object_id oid;
-
- xsnprintf(hex, sizeof(hex), "%02x%s",
- subdir_nr, de->d_name);
- if (!get_oid_hex(hex, &oid)) {
- if (obj_cb) {
- r = obj_cb(&oid, path->buf, data);
- if (r)
- break;
- }
- continue;
+ if (strlen(de->d_name) == GIT_SHA1_HEXSZ - 2 &&
+ !hex_to_bytes(oid.hash + 1, de->d_name,
+ GIT_SHA1_RAWSZ - 1)) {
+ if (obj_cb) {
+ r = obj_cb(&oid, path->buf, data);
+ if (r)
+ break;
}
+ continue;
}
if (cruft_cb) {