#include "cache.h"
#include "tag.h"
#include "commit.h"
#include "tree.h"
#include "blob.h"
#include "tree-walk.h"
#include "refs.h"
#include "remote.h"
static int get_sha1_oneline(const char *, unsigned char *, struct commit_list *);
static int find_short_object_filename(int len, const char *name, unsigned char *sha1)
{
struct alternate_object_database *alt;
char hex[40];
int found = 0;
static struct alternate_object_database *fakeent;
if (!fakeent) {
const char *objdir = get_object_directory();
int objdir_len = strlen(objdir);
int entlen = objdir_len + 43;
fakeent = xmalloc(sizeof(*fakeent) + entlen);
memcpy(fakeent->base, objdir, objdir_len);
fakeent->name = fakeent->base + objdir_len + 1;
fakeent->name[-1] = '/';
}
fakeent->next = alt_odb_list;
sprintf(hex, "%.2s", name);
for (alt = fakeent; alt && found < 2; alt = alt->next) {
struct dirent *de;
DIR *dir;
sprintf(alt->name, "%.2s/", name);
dir = opendir(alt->base);
if (!dir)
continue;
while ((de = readdir(dir)) != NULL) {
if (strlen(de->d_name) != 38)
continue;
if (memcmp(de->d_name, name + 2, len - 2))
continue;
if (!found) {
memcpy(hex + 2, de->d_name, 38);
found++;
}
else if (memcmp(hex + 2, de->d_name, 38)) {
found = 2;
break;
}
}
closedir(dir);
}
if (found == 1)
return get_sha1_hex(hex, sha1) == 0;
return found;
}
static int match_sha(unsigned len, const unsigned char *a, const unsigned char *b)
{
do {
if (*a != *b)
return 0;
a++;
b++;
len -= 2;
} while (len > 1);
if (len)
if ((*a ^ *b) & 0xf0)
return 0;
return 1;
}
static int find_short_packed_object(int len, const unsigned char *match, unsigned char *sha1)
{
struct packed_git *p;
const unsigned char *found_sha1 = NULL;
int found = 0;
prepare_packed_git();
for (p = packed_git; p && found < 2; p = p->next) {
uint32_t num, last;
uint32_t first = 0;
open_pack_index(p);
num = p->num_objects;
last = num;
while (first < last) {
uint32_t mid = (first + last) / 2;
const unsigned char *now;
int cmp;
now = nth_packed_object_sha1(p, mid);
cmp = hashcmp(match, now);
if (!cmp) {
first = mid;
break;
}
if (cmp > 0) {
first = mid+1;
continue;
}
last = mid;
}
if (first < num) {
const unsigned char *now, *next;
now = nth_packed_object_sha1(p, first);
if (match_sha(len, match, now)) {
next = nth_packed_object_sha1(p, first+1);
if (!next|| !match_sha(len, match, next)) {
/* unique within this pack */
if (!found) {
found_sha1 = now;
found++;
}
else if (hashcmp(found_sha1, now)) {
found = 2;
break;
}
}
else {
/* not even unique within this pack */
found = 2;
break;
}
}
}
}
if (found == 1)
hashcpy(sha1, found_sha1);
return found;
}
#define SHORT_NAME_NOT_FOUND (-1)
#define SHORT_NAME_AMBIGUOUS (-2)
static int find_unique_short_object(int len, char *canonical,
unsigned char *res, unsigned char *sha1)
{
int has_unpacked, has_packed;
unsigned char unpacked_sha1[20], packed_sha1[20];
prepare_alt_odb();
has_unpacked = find_short_object_filename(len, canonical, unpacked_sha1);
has_packed = find_short_packed_object(len, res, packed_sha1);
if (!has_unpacked && !has_packed)
return SHORT_NAME_NOT_FOUND;
if (1 < has_unpacked || 1 < has_packed)
return SHORT_NAME_AMBIGUOUS;
if (has_unpacked != has_packed) {
hashcpy(sha1, (has_packed ? packed_sha1 : unpacked_sha1));
return 0;
}
/* Both have unique ones -- do they match? */
if (hashcmp(packed_sha1, unpacked_sha1))
return SHORT_NAME_AMBIGUOUS;
hashcpy(sha1, packed_sha1);
return 0;
}
static int get_short_sha1(const char *name, int len, unsigned char *sha1,
int quietly)
{
int i, status;
|