- struct patch* ret = xmalloc(sizeof(struct patch));
- ret->chunks = NULL;
- ret->num = 0;
-
- struct util_info* info_c = (struct util_info*) commit->object.util;
- struct util_info* info_o = (struct util_info*) other->object.util;
-
- if(!memcmp(info_c->sha1, info_o->sha1, 20))
- return ret;
-
- get_blob(commit);
- get_blob(other);
-
- FILE* fout = fopen("/tmp/git-blame-tmp1", "w");
- if(!fout)
- die("fopen tmp1 failed: %s", strerror(errno));
-
- if(fwrite(info_c->buf, info_c->size, 1, fout) != 1)
- die("fwrite 1 failed: %s", strerror(errno));
- fclose(fout);
-
- fout = fopen("/tmp/git-blame-tmp2", "w");
- if(!fout)
- die("fopen tmp2 failed: %s", strerror(errno));
-
- if(fwrite(info_o->buf, info_o->size, 1, fout) != 1)
- die("fwrite 2 failed: %s", strerror(errno));
- fclose(fout);
-
- FILE* fin = popen("diff -u0 /tmp/git-blame-tmp1 /tmp/git-blame-tmp2", "r");
- if(!fin)
- die("popen failed: %s", strerror(errno));
-
- char buf[1024];
- while(fgets(buf, sizeof(buf), fin)) {
- if(buf[0] != '@' || buf[1] != '@')
- continue;
-
- if(DEBUG)
- printf("chunk line: %s", buf);
- ret->num++;
- ret->chunks = xrealloc(ret->chunks, sizeof(struct chunk)*ret->num);
- struct chunk* chunk = &ret->chunks[ret->num-1];
-
- assert(!strncmp(buf, "@@ -", 4));
-
- char* start = buf+4;
- char* sp = index(start, ' ');
- *sp = '\0';
- if(index(start, ',')) {
- int ret = sscanf(start, "%d,%d", &chunk->off1, &chunk->len1);
- assert(ret == 2);
- } else {
- int ret = sscanf(start, "%d", &chunk->off1);
- assert(ret == 1);
- chunk->len1 = 1;
- }
- *sp = ' ';
-
- start = sp+1;
- sp = index(start, ' ');
- *sp = '\0';
- if(index(start, ',')) {
- int ret = sscanf(start, "%d,%d", &chunk->off2, &chunk->len2);
- assert(ret == 2);
- } else {
- int ret = sscanf(start, "%d", &chunk->off2);
- assert(ret == 1);
- chunk->len2 = 1;
- }
- *sp = ' ';
-
- if(chunk->off1 > 0)
- chunk->off1 -= 1;
- if(chunk->off2 > 0)
- chunk->off2 -= 1;
-
- assert(chunk->off1 >= 0);
- assert(chunk->off2 >= 0);
- }
- fclose(fin);
-
- num_get_patch++;
- return ret;
+ struct patch *ret;
+ struct util_info *info_c = (struct util_info *)commit->object.util;
+ struct util_info *info_o = (struct util_info *)other->object.util;
+ char tmp_path1[TEMPFILE_PATH_LEN], tmp_path2[TEMPFILE_PATH_LEN];
+ char diff_cmd[TEMPFILE_PATH_LEN*2 + 20];
+ struct timeval tv_start, tv_end;
+ int fd;
+ FILE *fin;
+ char buf[1024];
+
+ ret = xmalloc(sizeof(struct patch));
+ ret->chunks = NULL;
+ ret->num = 0;
+
+ get_blob(commit);
+ get_blob(other);
+
+ gettimeofday(&tv_start, NULL);
+
+ fd = git_mkstemp(tmp_path1, TEMPFILE_PATH_LEN, "git-blame-XXXXXX");
+ if (fd < 0)
+ die("unable to create temp-file: %s", strerror(errno));
+
+ if (xwrite(fd, info_c->buf, info_c->size) != info_c->size)
+ die("write failed: %s", strerror(errno));
+ close(fd);
+
+ fd = git_mkstemp(tmp_path2, TEMPFILE_PATH_LEN, "git-blame-XXXXXX");
+ if (fd < 0)
+ die("unable to create temp-file: %s", strerror(errno));
+
+ if (xwrite(fd, info_o->buf, info_o->size) != info_o->size)
+ die("write failed: %s", strerror(errno));
+ close(fd);
+
+ sprintf(diff_cmd, "diff -u0 %s %s", tmp_path1, tmp_path2);
+ fin = popen(diff_cmd, "r");
+ if (!fin)
+ die("popen failed: %s", strerror(errno));
+
+ while (fgets(buf, sizeof(buf), fin)) {
+ struct chunk *chunk;
+ char *start, *sp;
+
+ if (buf[0] != '@' || buf[1] != '@')
+ continue;
+
+ if (DEBUG)
+ printf("chunk line: %s", buf);
+ ret->num++;
+ ret->chunks = xrealloc(ret->chunks,
+ sizeof(struct chunk) * ret->num);
+ chunk = &ret->chunks[ret->num - 1];
+
+ assert(!strncmp(buf, "@@ -", 4));
+
+ start = buf + 4;
+ sp = index(start, ' ');
+ *sp = '\0';
+ if (index(start, ',')) {
+ int ret =
+ sscanf(start, "%d,%d", &chunk->off1, &chunk->len1);
+ assert(ret == 2);
+ } else {
+ int ret = sscanf(start, "%d", &chunk->off1);
+ assert(ret == 1);
+ chunk->len1 = 1;
+ }
+ *sp = ' ';
+
+ start = sp + 1;
+ sp = index(start, ' ');
+ *sp = '\0';
+ if (index(start, ',')) {
+ int ret =
+ sscanf(start, "%d,%d", &chunk->off2, &chunk->len2);
+ assert(ret == 2);
+ } else {
+ int ret = sscanf(start, "%d", &chunk->off2);
+ assert(ret == 1);
+ chunk->len2 = 1;
+ }
+ *sp = ' ';
+
+ if (chunk->len1 == 0)
+ chunk->off1++;
+ if (chunk->len2 == 0)
+ chunk->off2++;
+
+ if (chunk->off1 > 0)
+ chunk->off1--;
+ if (chunk->off2 > 0)
+ chunk->off2--;
+
+ assert(chunk->off1 >= 0);
+ assert(chunk->off2 >= 0);
+ }
+ pclose(fin);
+ unlink(tmp_path1);
+ unlink(tmp_path2);
+
+ gettimeofday(&tv_end, NULL);
+ patch_time += 1000000 * (tv_end.tv_sec - tv_start.tv_sec) +
+ tv_end.tv_usec - tv_start.tv_usec;
+
+ num_get_patch++;
+ return ret;