2005-04-20 16:10:46 +08:00
|
|
|
#include "cache.h"
|
2006-04-02 20:44:09 +08:00
|
|
|
#include "blob.h"
|
|
|
|
#include "commit.h"
|
|
|
|
#include "tree.h"
|
2005-04-20 16:10:46 +08:00
|
|
|
|
|
|
|
struct entry {
|
|
|
|
unsigned char old_sha1[20];
|
|
|
|
unsigned char new_sha1[20];
|
|
|
|
int converted;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define MAXOBJECTS (1000000)
|
|
|
|
|
|
|
|
static struct entry *convert[MAXOBJECTS];
|
|
|
|
static int nr_convert;
|
|
|
|
|
|
|
|
static struct entry * convert_entry(unsigned char *sha1);
|
|
|
|
|
|
|
|
static struct entry *insert_new(unsigned char *sha1, int pos)
|
|
|
|
{
|
2006-04-04 02:30:46 +08:00
|
|
|
struct entry *new = xcalloc(1, sizeof(struct entry));
|
2006-08-23 14:49:00 +08:00
|
|
|
hashcpy(new->old_sha1, sha1);
|
2005-04-20 16:10:46 +08:00
|
|
|
memmove(convert + pos + 1, convert + pos, (nr_convert - pos) * sizeof(struct entry *));
|
|
|
|
convert[pos] = new;
|
|
|
|
nr_convert++;
|
|
|
|
if (nr_convert == MAXOBJECTS)
|
|
|
|
die("you're kidding me - hit maximum object limit");
|
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct entry *lookup_entry(unsigned char *sha1)
|
|
|
|
{
|
|
|
|
int low = 0, high = nr_convert;
|
|
|
|
|
|
|
|
while (low < high) {
|
|
|
|
int next = (low + high) / 2;
|
|
|
|
struct entry *n = convert[next];
|
2006-08-18 02:54:57 +08:00
|
|
|
int cmp = hashcmp(sha1, n->old_sha1);
|
2005-04-20 16:10:46 +08:00
|
|
|
if (!cmp)
|
|
|
|
return n;
|
|
|
|
if (cmp < 0) {
|
|
|
|
high = next;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
low = next+1;
|
|
|
|
}
|
|
|
|
return insert_new(sha1, low);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void convert_binary_sha1(void *buffer)
|
|
|
|
{
|
|
|
|
struct entry *entry = convert_entry(buffer);
|
2006-08-23 14:49:00 +08:00
|
|
|
hashcpy(buffer, entry->new_sha1);
|
2005-04-20 16:10:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void convert_ascii_sha1(void *buffer)
|
|
|
|
{
|
|
|
|
unsigned char sha1[20];
|
|
|
|
struct entry *entry;
|
|
|
|
|
|
|
|
if (get_sha1_hex(buffer, sha1))
|
2005-08-10 12:25:46 +08:00
|
|
|
die("expected sha1, got '%s'", (char*) buffer);
|
2005-04-20 16:10:46 +08:00
|
|
|
entry = convert_entry(sha1);
|
|
|
|
memcpy(buffer, sha1_to_hex(entry->new_sha1), 40);
|
|
|
|
}
|
|
|
|
|
2005-07-28 06:29:38 +08:00
|
|
|
static unsigned int convert_mode(unsigned int mode)
|
|
|
|
{
|
|
|
|
unsigned int newmode;
|
|
|
|
|
|
|
|
newmode = mode & S_IFMT;
|
|
|
|
if (S_ISREG(mode))
|
|
|
|
newmode |= (mode & 0100) ? 0755 : 0644;
|
|
|
|
return newmode;
|
|
|
|
}
|
|
|
|
|
2005-04-24 07:37:31 +08:00
|
|
|
static int write_subdirectory(void *buffer, unsigned long size, const char *base, int baselen, unsigned char *result_sha1)
|
2005-04-20 16:10:46 +08:00
|
|
|
{
|
2005-04-27 03:00:58 +08:00
|
|
|
char *new = xmalloc(size);
|
2005-04-26 01:19:53 +08:00
|
|
|
unsigned long newlen = 0;
|
2005-04-24 07:37:31 +08:00
|
|
|
unsigned long used;
|
|
|
|
|
|
|
|
used = 0;
|
|
|
|
while (size) {
|
|
|
|
int len = 21 + strlen(buffer);
|
|
|
|
char *path = strchr(buffer, ' ');
|
|
|
|
unsigned char *sha1;
|
|
|
|
unsigned int mode;
|
|
|
|
char *slash, *origpath;
|
|
|
|
|
|
|
|
if (!path || sscanf(buffer, "%o", &mode) != 1)
|
|
|
|
die("bad tree conversion");
|
2005-07-28 06:29:38 +08:00
|
|
|
mode = convert_mode(mode);
|
2005-04-24 07:37:31 +08:00
|
|
|
path++;
|
|
|
|
if (memcmp(path, base, baselen))
|
|
|
|
break;
|
|
|
|
origpath = path;
|
|
|
|
path += baselen;
|
|
|
|
slash = strchr(path, '/');
|
|
|
|
if (!slash) {
|
|
|
|
newlen += sprintf(new + newlen, "%o %s", mode, path);
|
|
|
|
new[newlen++] = '\0';
|
2006-08-23 14:49:00 +08:00
|
|
|
hashcpy((unsigned char*)new + newlen, (unsigned char *) buffer + len - 20);
|
2005-04-24 07:37:31 +08:00
|
|
|
newlen += 20;
|
|
|
|
|
|
|
|
used += len;
|
|
|
|
size -= len;
|
2006-06-18 23:18:09 +08:00
|
|
|
buffer = (char *) buffer + len;
|
2005-04-24 07:37:31 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2005-05-03 01:57:02 +08:00
|
|
|
newlen += sprintf(new + newlen, "%o %.*s", S_IFDIR, (int)(slash - path), path);
|
2005-04-24 07:37:31 +08:00
|
|
|
new[newlen++] = 0;
|
|
|
|
sha1 = (unsigned char *)(new + newlen);
|
|
|
|
newlen += 20;
|
|
|
|
|
|
|
|
len = write_subdirectory(buffer, size, origpath, slash-origpath+1, sha1);
|
|
|
|
|
|
|
|
used += len;
|
|
|
|
size -= len;
|
2006-06-18 23:18:09 +08:00
|
|
|
buffer = (char *) buffer + len;
|
2005-04-24 07:37:31 +08:00
|
|
|
}
|
|
|
|
|
2006-04-02 20:44:09 +08:00
|
|
|
write_sha1_file(new, newlen, tree_type, result_sha1);
|
2005-04-24 07:37:31 +08:00
|
|
|
free(new);
|
|
|
|
return used;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void convert_tree(void *buffer, unsigned long size, unsigned char *result_sha1)
|
|
|
|
{
|
|
|
|
void *orig_buffer = buffer;
|
|
|
|
unsigned long orig_size = size;
|
|
|
|
|
2005-04-20 16:10:46 +08:00
|
|
|
while (size) {
|
2007-03-07 09:44:17 +08:00
|
|
|
size_t len = 1+strlen(buffer);
|
2005-04-20 16:10:46 +08:00
|
|
|
|
2006-06-18 23:18:09 +08:00
|
|
|
convert_binary_sha1((char *) buffer + len);
|
2005-04-20 16:10:46 +08:00
|
|
|
|
|
|
|
len += 20;
|
|
|
|
if (len > size)
|
|
|
|
die("corrupt tree object");
|
|
|
|
size -= len;
|
2006-06-18 23:18:09 +08:00
|
|
|
buffer = (char *) buffer + len;
|
2005-04-20 16:10:46 +08:00
|
|
|
}
|
2005-04-24 07:37:31 +08:00
|
|
|
|
|
|
|
write_subdirectory(orig_buffer, orig_size, "", 0, result_sha1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned long parse_oldstyle_date(const char *buf)
|
|
|
|
{
|
|
|
|
char c, *p;
|
|
|
|
char buffer[100];
|
|
|
|
struct tm tm;
|
|
|
|
const char *formats[] = {
|
|
|
|
"%c",
|
|
|
|
"%a %b %d %T",
|
|
|
|
"%Z",
|
|
|
|
"%Y",
|
|
|
|
" %Y",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
/* We only ever did two timezones in the bad old format .. */
|
|
|
|
const char *timezones[] = {
|
2005-04-25 06:49:09 +08:00
|
|
|
"PDT", "PST", "CEST", NULL
|
2005-04-24 07:37:31 +08:00
|
|
|
};
|
|
|
|
const char **fmt = formats;
|
|
|
|
|
|
|
|
p = buffer;
|
|
|
|
while (isspace(c = *buf))
|
|
|
|
buf++;
|
|
|
|
while ((c = *buf++) != '\n')
|
|
|
|
*p++ = c;
|
|
|
|
*p++ = 0;
|
|
|
|
buf = buffer;
|
|
|
|
memset(&tm, 0, sizeof(tm));
|
|
|
|
do {
|
|
|
|
const char *next = strptime(buf, *fmt, &tm);
|
|
|
|
if (next) {
|
|
|
|
if (!*next)
|
|
|
|
return mktime(&tm);
|
|
|
|
buf = next;
|
|
|
|
} else {
|
|
|
|
const char **p = timezones;
|
|
|
|
while (isspace(*buf))
|
|
|
|
buf++;
|
|
|
|
while (*p) {
|
|
|
|
if (!memcmp(buf, *p, strlen(*p))) {
|
|
|
|
buf += strlen(*p);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fmt++;
|
|
|
|
} while (*buf && *fmt);
|
|
|
|
printf("left: %s\n", buf);
|
|
|
|
return mktime(&tm);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int convert_date_line(char *dst, void **buf, unsigned long *sp)
|
|
|
|
{
|
|
|
|
unsigned long size = *sp;
|
|
|
|
char *line = *buf;
|
|
|
|
char *next = strchr(line, '\n');
|
|
|
|
char *date = strchr(line, '>');
|
|
|
|
int len;
|
|
|
|
|
|
|
|
if (!next || !date)
|
|
|
|
die("missing or bad author/committer line %s", line);
|
|
|
|
next++; date += 2;
|
|
|
|
|
|
|
|
*buf = next;
|
|
|
|
*sp = size - (next - line);
|
|
|
|
|
|
|
|
len = date - line;
|
|
|
|
memcpy(dst, line, len);
|
|
|
|
dst += len;
|
|
|
|
|
|
|
|
/* Is it already in new format? */
|
|
|
|
if (isdigit(*date)) {
|
|
|
|
int datelen = next - date;
|
|
|
|
memcpy(dst, date, datelen);
|
|
|
|
return len + datelen;
|
|
|
|
}
|
|
|
|
|
2005-04-24 07:48:32 +08:00
|
|
|
/*
|
|
|
|
* Hacky hacky: one of the sparse old-style commits does not have
|
|
|
|
* any date at all, but we can fake it by using the committer date.
|
|
|
|
*/
|
|
|
|
if (*date == '\n' && strchr(next, '>'))
|
|
|
|
date = strchr(next, '>')+2;
|
|
|
|
|
2005-04-24 07:37:31 +08:00
|
|
|
return len + sprintf(dst, "%lu -0700\n", parse_oldstyle_date(date));
|
2005-04-20 16:10:46 +08:00
|
|
|
}
|
|
|
|
|
2005-04-24 07:37:31 +08:00
|
|
|
static void convert_date(void *buffer, unsigned long size, unsigned char *result_sha1)
|
2005-04-20 16:10:46 +08:00
|
|
|
{
|
2005-04-27 03:00:58 +08:00
|
|
|
char *new = xmalloc(size + 100);
|
2005-04-26 01:19:53 +08:00
|
|
|
unsigned long newlen = 0;
|
2006-07-10 14:57:51 +08:00
|
|
|
|
|
|
|
/* "tree <sha1>\n" */
|
2005-04-24 07:37:31 +08:00
|
|
|
memcpy(new + newlen, buffer, 46);
|
|
|
|
newlen += 46;
|
2006-06-18 23:18:09 +08:00
|
|
|
buffer = (char *) buffer + 46;
|
2005-04-24 07:37:31 +08:00
|
|
|
size -= 46;
|
|
|
|
|
2006-07-10 14:57:51 +08:00
|
|
|
/* "parent <sha1>\n" */
|
2005-04-24 07:37:31 +08:00
|
|
|
while (!memcmp(buffer, "parent ", 7)) {
|
|
|
|
memcpy(new + newlen, buffer, 48);
|
|
|
|
newlen += 48;
|
2006-06-18 23:18:09 +08:00
|
|
|
buffer = (char *) buffer + 48;
|
2005-04-24 07:37:31 +08:00
|
|
|
size -= 48;
|
|
|
|
}
|
|
|
|
|
2006-07-10 14:57:51 +08:00
|
|
|
/* "author xyz <xyz> date" */
|
2005-04-24 07:37:31 +08:00
|
|
|
newlen += convert_date_line(new + newlen, &buffer, &size);
|
2006-07-10 14:57:51 +08:00
|
|
|
/* "committer xyz <xyz> date" */
|
2005-04-24 07:37:31 +08:00
|
|
|
newlen += convert_date_line(new + newlen, &buffer, &size);
|
|
|
|
|
2006-07-10 14:57:51 +08:00
|
|
|
/* Rest */
|
2005-04-24 07:37:31 +08:00
|
|
|
memcpy(new + newlen, buffer, size);
|
|
|
|
newlen += size;
|
|
|
|
|
2006-04-02 20:44:09 +08:00
|
|
|
write_sha1_file(new, newlen, commit_type, result_sha1);
|
|
|
|
free(new);
|
2005-04-24 07:37:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void convert_commit(void *buffer, unsigned long size, unsigned char *result_sha1)
|
|
|
|
{
|
|
|
|
void *orig_buffer = buffer;
|
|
|
|
unsigned long orig_size = size;
|
|
|
|
|
2005-07-28 06:29:38 +08:00
|
|
|
if (memcmp(buffer, "tree ", 5))
|
2005-08-10 12:25:46 +08:00
|
|
|
die("Bad commit '%s'", (char*) buffer);
|
2006-06-18 23:18:09 +08:00
|
|
|
convert_ascii_sha1((char *) buffer + 5);
|
|
|
|
buffer = (char *) buffer + 46; /* "tree " + "hex sha1" + "\n" */
|
2005-04-20 16:10:46 +08:00
|
|
|
while (!memcmp(buffer, "parent ", 7)) {
|
2006-06-18 23:18:09 +08:00
|
|
|
convert_ascii_sha1((char *) buffer + 7);
|
|
|
|
buffer = (char *) buffer + 48;
|
2005-04-20 16:10:46 +08:00
|
|
|
}
|
2005-04-24 07:37:31 +08:00
|
|
|
convert_date(orig_buffer, orig_size, result_sha1);
|
2005-04-20 16:10:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct entry * convert_entry(unsigned char *sha1)
|
|
|
|
{
|
|
|
|
struct entry *entry = lookup_entry(sha1);
|
2007-02-27 03:55:59 +08:00
|
|
|
enum object_type type;
|
2005-04-20 16:10:46 +08:00
|
|
|
void *buffer, *data;
|
2005-04-26 01:19:53 +08:00
|
|
|
unsigned long size;
|
2005-04-20 16:10:46 +08:00
|
|
|
|
|
|
|
if (entry->converted)
|
|
|
|
return entry;
|
2007-02-27 03:55:59 +08:00
|
|
|
data = read_sha1_file(sha1, &type, &size);
|
2005-04-20 16:10:46 +08:00
|
|
|
if (!data)
|
|
|
|
die("unable to read object %s", sha1_to_hex(sha1));
|
|
|
|
|
2005-04-27 03:00:58 +08:00
|
|
|
buffer = xmalloc(size);
|
2005-04-26 01:19:53 +08:00
|
|
|
memcpy(buffer, data, size);
|
2006-04-02 20:44:09 +08:00
|
|
|
|
2007-02-27 03:55:59 +08:00
|
|
|
if (type == OBJ_BLOB) {
|
2006-04-02 20:44:09 +08:00
|
|
|
write_sha1_file(buffer, size, blob_type, entry->new_sha1);
|
2007-02-27 03:55:59 +08:00
|
|
|
} else if (type == OBJ_TREE)
|
2005-04-26 01:19:53 +08:00
|
|
|
convert_tree(buffer, size, entry->new_sha1);
|
2007-02-27 03:55:59 +08:00
|
|
|
else if (type == OBJ_COMMIT)
|
2005-04-26 01:19:53 +08:00
|
|
|
convert_commit(buffer, size, entry->new_sha1);
|
2005-04-20 16:10:46 +08:00
|
|
|
else
|
2007-02-27 03:55:59 +08:00
|
|
|
die("unknown object type %d in %s", type, sha1_to_hex(sha1));
|
2005-04-20 16:10:46 +08:00
|
|
|
entry->converted = 1;
|
|
|
|
free(buffer);
|
2005-07-28 06:29:38 +08:00
|
|
|
free(data);
|
2005-04-20 16:10:46 +08:00
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
unsigned char sha1[20];
|
|
|
|
struct entry *entry;
|
|
|
|
|
2005-11-26 16:50:02 +08:00
|
|
|
setup_git_directory();
|
|
|
|
|
2006-05-09 05:43:38 +08:00
|
|
|
if (argc != 2)
|
2005-09-08 08:26:23 +08:00
|
|
|
usage("git-convert-objects <sha1>");
|
2006-05-09 05:43:38 +08:00
|
|
|
if (get_sha1(argv[1], sha1))
|
|
|
|
die("Not a valid object name %s", argv[1]);
|
2005-04-20 16:10:46 +08:00
|
|
|
|
|
|
|
entry = convert_entry(sha1);
|
|
|
|
printf("new sha1: %s\n", sha1_to_hex(entry->new_sha1));
|
|
|
|
return 0;
|
|
|
|
}
|