Commit Diff


commit - 9b058f456d15d60a89334ce3e7f0a7c22e182c55
commit + e6e73e5504113b6c75f1b2e3dec3e22af8f41996
blob - 2dceeed3a7a54bee61a491f92527df8295059f95
blob + fcf17481c5332a3d73388d4c75c513aea5c28d7e
--- got/got.c
+++ got/got.c
@@ -5360,12 +5360,13 @@ cmd_blame(int argc, char *argv[])
 	struct got_blob_object *blob = NULL;
 	char *commit_id_str = NULL;
 	struct blame_cb_args bca;
-	int ch, obj_type, i, fd = -1, fd1 = -1;
+	int ch, obj_type, i, fd1 = -1, fd2 = -1, fd3 = -1;
 	off_t filesize;
 	int *pack_fds = NULL;
+	FILE *f1 = NULL, *f2 = NULL;
 
-	fd = got_opentempfd();
-	if (fd == -1)
+	fd1 = got_opentempfd();
+	if (fd1 == -1)
 		return got_error_from_errno("got_opentempfd");
 
 	memset(&bca, 0, sizeof(bca));
@@ -5518,7 +5519,7 @@ cmd_blame(int argc, char *argv[])
 		goto done;
 	}
 
-	error = got_object_open_as_blob(&blob, repo, obj_id, 8192, fd);
+	error = got_object_open_as_blob(&blob, repo, obj_id, 8192, fd1);
 	if (error)
 		goto done;
 	bca.f = got_opentemp();
@@ -5549,13 +5550,28 @@ cmd_blame(int argc, char *argv[])
 	}
 	bca.repo = repo;
 
-	fd1 = got_opentempfd();
-	if (fd1 == -1) {
+	fd2 = got_opentempfd();
+	if (fd2 == -1) {
 		error = got_error_from_errno("got_opentempfd");
+		goto done;
+	}
+	fd3 = got_opentempfd();
+	if (fd3 == -1) {
+		error = got_error_from_errno("got_opentempfd");
+		goto done;
+	}
+	f1 = got_opentemp();
+	if (f1 == NULL) {
+		error = got_error_from_errno("got_opentemp");
+		goto done;
+	}
+	f2 = got_opentemp();
+	if (f2 == NULL) {
+		error = got_error_from_errno("got_opentemp");
 		goto done;
 	}
 	error = got_blame(link_target ? link_target : in_repo_path, commit_id,
-	    repo, blame_cb, &bca, check_cancelled, NULL, fd1);
+	    repo, blame_cb, &bca, check_cancelled, NULL, fd2, fd3, f1, f2);
 done:
 	free(in_repo_path);
 	free(link_target);
@@ -5565,10 +5581,18 @@ done:
 	free(obj_id);
 	if (commit)
 		got_object_commit_close(commit);
-	if (fd != -1 && close(fd) == -1 && error == NULL)
-		error = got_error_from_errno("close");
+
 	if (fd1 != -1 && close(fd1) == -1 && error == NULL)
 		error = got_error_from_errno("close");
+	if (fd2 != -1 && close(fd2) == -1 && error == NULL)
+		error = got_error_from_errno("close");
+	if (fd3 != -1 && close(fd3) == -1 && error == NULL)
+		error = got_error_from_errno("close");
+	if (f1 && fclose(f1) == EOF && error == NULL)
+		error = got_error_from_errno("fclose");
+	if (f2 && fclose(f2) == EOF && error == NULL)
+		error = got_error_from_errno("fclose");
+
 	if (blob)
 		got_object_blob_close(blob);
 	if (worktree)
blob - eb1c8f8f3ddec6a14375a8af819bc150fecf1abc
blob + 80bd351ec9bb5cf498aee6074d91b588a3566650
--- gotweb/gotweb.c
+++ gotweb/gotweb.c
@@ -4083,12 +4083,23 @@ gw_output_file_blame(struct gw_trans *gw_trans, struct
 	struct got_blob_object *blob = NULL;
 	char *path = NULL, *in_repo_path = NULL;
 	struct gw_blame_cb_args bca;
-	int i, obj_type, fd = -1, fd1 = -1;
+	int i, obj_type, fd1 = -1, fd2 = -1, fd3 = -1;
 	off_t filesize;
+	FILE *f1 = NULL, *f2 = NULL;
 
-	fd = got_opentempfd();
-	if (fd == -1)
+	fd1 = got_opentempfd();
+	if (fd1 == -1)
 		return got_error_from_errno("got_opentempfd");
+	fd2 = got_opentempfd();
+	if (fd2 == -1) {
+		error = got_error_from_errno("got_opentempfd");
+		goto done;
+	}
+	fd3 = got_opentempfd();
+	if (fd3 == -1) {
+		error = got_error_from_errno("got_opentempfd");
+		goto done;
+	}
 
 	memset(&bca, 0, sizeof(bca));
 
@@ -4133,7 +4144,7 @@ gw_output_file_blame(struct gw_trans *gw_trans, struct
 	}
 
 	error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192,
-	    fd);
+	    fd1);
 	if (error)
 		goto done;
 
@@ -4168,22 +4179,39 @@ gw_output_file_blame(struct gw_trans *gw_trans, struct
 
 	fd1 = got_opentempfd();
 	if (fd1 == -1) {
+		error = got_error_from_errno("got_opentempfd");
+		goto done;
+	}
+
+	f1 = got_opentemp();
+	if (f1 == NULL) {
 		error = got_error_from_errno("got_opentempfd");
 		goto done;
 	}
+	f2 = got_opentemp();
+	if (f2 == NULL) {
+		error = got_error_from_errno("got_opentempfd");
+		goto done;
+	}
 
 	error = got_blame(in_repo_path, commit_id, gw_trans->repo, gw_blame_cb,
-	    &bca, NULL, NULL, fd1);
+	    &bca, NULL, NULL, fd2, fd3, f1, f2);
 done:
 	free(in_repo_path);
 	free(commit_id);
 	free(obj_id);
 	free(path);
 
-	if (fd != -1 && close(fd) == -1 && error == NULL)
-		error = got_error_from_errno("close");
 	if (fd1 != -1 && close(fd1) == -1 && error == NULL)
+		error = got_error_from_errno("close");
+	if (fd2 != -1 && close(fd2) == -1 && error == NULL)
+		error = got_error_from_errno("close");
+	if (fd3 != -1 && close(fd3) == -1 && error == NULL)
 		error = got_error_from_errno("close");
+	if (f1 && fclose(f1) == EOF && error == NULL)
+		error = got_error_from_errno("fclose");
+	if (f2 && fclose(f2) == EOF && error == NULL)
+		error = got_error_from_errno("fclose");
 
 	if (blob) {
 		free(bca.line_offsets);
blob - 56e2d804dc17e77e5b5660bcd248453a942fcbb4
blob + 5d01c0034c2587d4dffbfd407a94631f1cfe7e7c
--- include/got_blame.h
+++ include/got_blame.h
@@ -36,4 +36,4 @@ typedef const struct got_error *(*got_blame_cb)(void *
  */
 const struct got_error *got_blame(const char *,
     struct got_object_id *, struct got_repository *,
-    got_blame_cb, void *, got_cancel_cb, void *, int);
+    got_blame_cb, void *, got_cancel_cb, void *, int, int, FILE *, FILE *);
blob - 68e7392e8e977dcd4362e7961374a990f1dba55c
blob + f08e9f18773211db7d75c2ee3de198ad23a08e01
--- lib/blame.c
+++ lib/blame.c
@@ -65,6 +65,7 @@ struct got_blame {
 	 */
 	FILE *f1; /* older version from commit N-1. */
 	FILE *f2; /* newer version from commit N. */
+	int fd;
 	unsigned char *map1;
 	unsigned char *map2;
 	off_t size1;
@@ -204,7 +205,6 @@ blame_commit(struct got_blame *blame, struct got_objec
 	struct got_object_id *pblob_id = NULL;
 	struct got_blob_object *pblob = NULL;
 	struct diff_result *diff_result = NULL;
-	int fd = -1;
 
 	err = got_object_open_as_commit(&commit, repo, id);
 	if (err)
@@ -216,12 +216,6 @@ blame_commit(struct got_blame *blame, struct got_objec
 		return NULL;
 	}
 
-	fd = got_opentempfd();
-	if (fd == -1) {
-		err = got_error_from_errno("got_opentempfd");
-		goto done;
-	}
-
 	err = got_object_open_as_commit(&pcommit, repo, &pid->id);
 	if (err)
 		goto done;
@@ -233,16 +227,10 @@ blame_commit(struct got_blame *blame, struct got_objec
 		goto done;
 	}
 
-	err = got_object_open_as_blob(&pblob, repo, pblob_id, 8192, fd);
+	err = got_object_open_as_blob(&pblob, repo, pblob_id, 8192, blame->fd);
 	if (err)
 		goto done;
 
-	blame->f1 = got_opentemp();
-	if (blame->f1 == NULL) {
-		err = got_error_from_errno("got_opentemp");
-		goto done;
-	}
-
 	err = blame_prepare_file(blame->f1, &blame->map1, &blame->size1,
 	    &blame->nlines1, &blame->line_offsets1, blame->data1,
 	    blame->cfg, pblob);
@@ -280,8 +268,6 @@ done:
 	if (pcommit)
 		got_object_commit_close(pcommit);
 	free(pblob_id);
-	if (fd != -1 && close(fd) == -1 && err == NULL)
-		err = got_error_from_errno("close");
 	if (pblob)
 		got_object_blob_close(pblob);
 	return err;
@@ -304,10 +290,6 @@ blame_close(struct got_blame *blame)
 		if (munmap(blame->map2, blame->size2) == -1 && err == NULL)
 			err = got_error_from_errno("munmap");
 	}
-	if (blame->f1 && fclose(blame->f1) == EOF && err == NULL)
-		err = got_error_from_errno("fclose");
-	if (blame->f2 && fclose(blame->f2) == EOF && err == NULL)
-		err = got_error_from_errno("fclose");
 	free(blame->lines);
 	free(blame->line_offsets1);
 	free(blame->line_offsets2);
@@ -466,9 +448,11 @@ blame_atomize_file(void *arg, struct diff_data *d)
 }
 
 static const struct got_error *
-close_file2_and_reuse_file1(struct got_blame *blame)
+flip_files(struct got_blame *blame)
 {
+	const struct got_error *err = NULL;
 	struct diff_data *d;
+	FILE *tmp;
 
 	free(blame->line_offsets2);
 	blame->line_offsets2 = blame->line_offsets1;
@@ -483,15 +467,16 @@ close_file2_and_reuse_file1(struct got_blame *blame)
 			return got_error_from_errno("munmap");
 		blame->map2 = blame->map1;
 		blame->map1 = NULL;
-
 	}
 	blame->size2 = blame->size1;
-	blame->size1 = 0;
 
-	if (fclose(blame->f2) == EOF)
-		return got_error_from_errno("fclose");
+	err = got_opentemp_truncate(blame->f2);
+	if (err)
+		return err; 
+	tmp = blame->f2;
 	blame->f2 = blame->f1;
-	blame->f1 = NULL;
+	blame->f1 = tmp;
+	blame->size1 = 0;
 
 	blame->nlines2 = blame->nlines1;
 	blame->nlines1 = 0;
@@ -509,7 +494,7 @@ static const struct got_error *
 blame_open(struct got_blame **blamep, const char *path,
     struct got_object_id *start_commit_id, struct got_repository *repo,
     got_blame_cb cb, void *arg, got_cancel_cb cancel_cb, void *cancel_arg,
-    int fd)
+    int fd1, int fd2, FILE *f1, FILE *f2)
 {
 	const struct got_error *err = NULL;
 	struct got_commit_object *start_commit = NULL, *last_commit = NULL;
@@ -530,7 +515,7 @@ blame_open(struct got_blame **blamep, const char *path
 	if (err)
 		goto done;
 
-	err = got_object_open_as_blob(&blob, repo, obj_id, 8192, fd);
+	err = got_object_open_as_blob(&blob, repo, obj_id, 8192, fd1);
 	if (err)
 		goto done;
 
@@ -551,11 +536,10 @@ blame_open(struct got_blame **blamep, const char *path
 		goto done;
 	}
 
-	blame->f2 = got_opentemp();
-	if (blame->f2 == NULL) {
-		err = got_error_from_errno("got_opentemp");
-		goto done;
-	}
+	blame->f1 = f1;
+	blame->f2 = f2;
+	blame->fd = fd2;
+
 	err = got_diff_get_config(&blame->cfg, GOT_DIFF_ALGORITHM_PATIENCE,
 	    blame_atomize_file, blame);
 	if (err)
@@ -619,7 +603,7 @@ blame_open(struct got_blame **blamep, const char *path
 			if (blame->nannotated == blame->nlines)
 				break;
 
-			err = close_file2_and_reuse_file1(blame);
+			err = flip_files(blame);
 			if (err)
 				goto done;
 		}
@@ -660,7 +644,8 @@ done:
 const struct got_error *
 got_blame(const char *path, struct got_object_id *commit_id,
     struct got_repository *repo, got_blame_cb cb, void *arg,
-    got_cancel_cb cancel_cb, void* cancel_arg, int fd)
+    got_cancel_cb cancel_cb, void* cancel_arg, int fd1, int fd2, FILE *f1,
+    FILE *f2)
 {
 	const struct got_error *err = NULL, *close_err = NULL;
 	struct got_blame *blame;
@@ -670,7 +655,7 @@ got_blame(const char *path, struct got_object_id *comm
 		return got_error_from_errno2("asprintf", path);
 
 	err = blame_open(&blame, abspath, commit_id, repo, cb, arg,
-	    cancel_cb, cancel_arg, fd);
+	    cancel_cb, cancel_arg, fd1, fd2, f1, f2);
 	free(abspath);
 	if (blame)
 		close_err = blame_close(blame);
blob - 6a0c6dbff39ee1a8db173cbc3fa1fbf0918eecab
blob + b458f29d2fc7c248f6f3bb6668d560ec46596515
--- tog/tog.c
+++ tog/tog.c
@@ -4907,25 +4907,45 @@ blame_thread(void *arg)
 	const struct got_error *err, *close_err;
 	struct tog_blame_thread_args *ta = arg;
 	struct tog_blame_cb_args *a = ta->cb_args;
-	int errcode, fd = -1;
+	int errcode, fd1 = -1, fd2 = -1;
+	FILE *f1 = NULL, *f2 = NULL;
 
-	fd = got_opentempfd();
-	if (fd == -1)
+	fd1 = got_opentempfd();
+	if (fd1 == -1)
 		return (void *)got_error_from_errno("got_opentempfd");
+
+	fd2 = got_opentempfd();
+	if (fd2 == -1) {
+		err = got_error_from_errno("got_opentempfd");
+		goto done;
+	}
 
+	f1 = got_opentemp();
+	if (f1 == NULL) {
+		err = (void *)got_error_from_errno("got_opentemp");
+		goto done;
+	}
+	f2 = got_opentemp();
+	if (f2 == NULL) {
+		err = (void *)got_error_from_errno("got_opentemp");
+		goto done;
+	}
+
 	err = block_signals_used_by_main_thread();
 	if (err)
-		return (void *)err;
+		goto done;
 
 	err = got_blame(ta->path, a->commit_id, ta->repo,
-	    blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg, fd);
+	    blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1,
+	    f2);
 	if (err && err->code == GOT_ERR_CANCELLED)
 		err = NULL;
 
 	errcode = pthread_mutex_lock(&tog_mutex);
-	if (errcode)
-		return (void *)got_error_set_errno(errcode,
-		    "pthread_mutex_lock");
+	if (errcode) {
+		err = got_error_set_errno(errcode, "pthread_mutex_lock");
+		goto done;
+	}
 
 	close_err = got_repo_close(ta->repo);
 	if (err == NULL)
@@ -4937,8 +4957,15 @@ blame_thread(void *arg)
 	if (errcode && err == NULL)
 		err = got_error_set_errno(errcode, "pthread_mutex_unlock");
 
-	if (fd != -1 && close(fd) == -1 && err == NULL)
+done:
+	if (fd1 != -1 && close(fd1) == -1 && err == NULL)
 		err = got_error_from_errno("close");
+	if (fd2 != -1 && close(fd2) == -1 && err == NULL)
+		err = got_error_from_errno("close");
+	if (f1 && fclose(f1) == EOF && err == NULL)
+		err = got_error_from_errno("fclose");
+	if (f2 && fclose(f2) == EOF && err == NULL)
+		err = got_error_from_errno("fclose");
 
 	return (void *)err;
 }