{
struct object *obj = lookup_object(sha1);
if (!obj) {
- struct commit *ret = xcalloc(1, sizeof(struct commit));
+ struct commit *ret = alloc_commit_node();
created_object(sha1, &ret->object);
ret->object.type = TYPE_COMMIT;
return ret;
return 0;
}
+void free_commit_grafts(void)
+{
+ int pos = commit_graft_nr;
+ while (pos >= 0)
+ free(commit_graft[pos--]);
+ commit_graft_nr = 0;
+}
+
struct commit_graft *read_graft_line(char *buf, int len)
{
/* The format is just "Commit Parent1 Parent2 ...\n" */
static void prepare_commit_graft(void)
{
static int commit_graft_prepared;
- char *graft_file;
+ static char *last_graft_file;
+ char *graft_file = get_graft_file();
+
+ if (last_graft_file) {
+ if (!strcmp(graft_file, last_graft_file))
+ return;
+ free_commit_grafts();
+ }
+ if (last_graft_file)
+ free(last_graft_file);
+ last_graft_file = strdup(graft_file);
- if (commit_graft_prepared)
- return;
- graft_file = get_graft_file();
read_graft_file(graft_file);
commit_graft_prepared = 1;
}
int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size)
{
+ char *tail = buffer;
char *bufptr = buffer;
unsigned char parent[20];
struct commit_list **pptr;
if (item->object.parsed)
return 0;
item->object.parsed = 1;
- if (memcmp(bufptr, "tree ", 5))
+ tail += size;
+ if (tail <= bufptr + 5 || memcmp(bufptr, "tree ", 5))
return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
- if (get_sha1_hex(bufptr + 5, parent) < 0)
+ if (tail <= bufptr + 45 || get_sha1_hex(bufptr + 5, parent) < 0)
return error("bad tree pointer in commit %s",
sha1_to_hex(item->object.sha1));
item->tree = lookup_tree(parent);
pptr = &item->parents;
graft = lookup_commit_graft(item->object.sha1);
- while (!memcmp(bufptr, "parent ", 7)) {
+ while (bufptr + 48 < tail && !memcmp(bufptr, "parent ", 7)) {
struct commit *new_parent;
- if (get_sha1_hex(bufptr + 7, parent) || bufptr[47] != '\n')
+ if (tail <= bufptr + 48 ||
+ get_sha1_hex(bufptr + 7, parent) ||
+ bufptr[47] != '\n')
return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
bufptr += 48;
if (graft)
memcpy(bp, q_utf8, sizeof(q_utf8)-1);
bp += sizeof(q_utf8)-1;
for (i = 0; i < len; i++) {
- unsigned ch = line[i];
+ unsigned ch = line[i] & 0xFF;
if (is_rfc2047_special(ch)) {
sprintf(bp, "=%02X", ch);
bp += 3;
const char *hex = abbrev
? find_unique_abbrev(p->object.sha1, abbrev)
: sha1_to_hex(p->object.sha1);
- char *dots = (abbrev && strlen(hex) != 40) ? "..." : "";
+ const char *dots = (abbrev && strlen(hex) != 40) ? "..." : "";
parent = parent->next;
offset += sprintf(buf + offset, " %s%s", hex, dots);
* to say this is not a 7-bit ASCII.
*/
if (fmt == CMIT_FMT_EMAIL && !after_subject) {
- int i;
- for (i = 0; !plain_non_ascii && msg[i] && i < len; i++)
- if (msg[i] & 0x80)
+ int i, ch, in_body;
+
+ for (in_body = i = 0; (ch = msg[i]) && i < len; i++) {
+ if (!in_body) {
+ /* author could be non 7-bit ASCII but
+ * the log may so; skip over the
+ * header part first.
+ */
+ if (ch == '\n' &&
+ i + 1 < len && msg[i+1] == '\n')
+ in_body = 1;
+ }
+ else if (ch & 0x80) {
plain_non_ascii = 1;
+ break;
+ }
+ }
}
for (;;) {