reuse a temporary file across commits during got log -p -S suggested by + ok op@
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
diff --git a/got/got.c b/got/got.c
index 9c71b5d..28ba9a4 100644
--- a/got/got.c
+++ b/got/got.c
@@ -3776,10 +3776,9 @@ match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
static const struct got_error *
match_patch(int *have_match, struct got_commit_object *commit,
struct got_object_id *id, const char *path, int diff_context,
- struct got_repository *repo, regex_t *regex)
+ struct got_repository *repo, regex_t *regex, FILE *f)
{
const struct got_error *err = NULL;
- FILE *f;
char *line = NULL;
size_t linesize = 0;
ssize_t linelen;
@@ -3787,9 +3786,9 @@ match_patch(int *have_match, struct got_commit_object *commit,
*have_match = 0;
- f = got_opentemp();
- if (f == NULL)
- return got_error_from_errno("got_opentemp");
+ err = got_opentemp_truncate(f);
+ if (err)
+ return err;
err = print_patch(commit, id, path, diff_context, repo, f);
if (err)
@@ -3808,8 +3807,6 @@ match_patch(int *have_match, struct got_commit_object *commit,
}
done:
free(line);
- if (fclose(f) == EOF && err == NULL)
- err = got_error_from_errno("fclose");
return err;
}
@@ -4016,7 +4013,8 @@ print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
struct got_repository *repo, const char *path, int show_changed_paths,
int show_patch, const char *search_pattern, int diff_context, int limit,
int log_branches, int reverse_display_order,
- struct got_reflist_object_id_map *refs_idmap, int one_line)
+ struct got_reflist_object_id_map *refs_idmap, int one_line,
+ FILE *tmpfile)
{
const struct got_error *err;
struct got_commit_graph *graph;
@@ -4079,7 +4077,8 @@ print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
&changed_paths, ®ex);
if (have_match == 0 && show_patch) {
err = match_patch(&have_match, commit, id,
- path, diff_context, repo, ®ex);
+ path, diff_context, repo, ®ex,
+ tmpfile);
if (err)
break;
}
@@ -4207,6 +4206,7 @@ cmd_log(int argc, char *argv[])
const char *errstr;
struct got_reflist_head refs;
struct got_reflist_object_id_map *refs_idmap = NULL;
+ FILE *tmpfile = NULL;
TAILQ_INIT(&refs);
@@ -4398,9 +4398,18 @@ cmd_log(int argc, char *argv[])
worktree = NULL;
}
+ if (search_pattern && show_patch) {
+ tmpfile = got_opentemp();
+ if (tmpfile == NULL) {
+ error = got_error_from_errno("got_opentemp");
+ goto done;
+ }
+ }
+
error = print_commits(start_id, end_id, repo, path ? path : "",
show_changed_paths, show_patch, search_pattern, diff_context,
- limit, log_branches, reverse_display_order, refs_idmap, one_line);
+ limit, log_branches, reverse_display_order, refs_idmap, one_line,
+ tmpfile);
done:
free(path);
free(repo_path);
@@ -4414,6 +4423,8 @@ done:
}
if (refs_idmap)
got_reflist_object_id_map_free(refs_idmap);
+ if (tmpfile && fclose(tmpfile) == EOF && error == NULL)
+ error = got_error_from_errno("fclose");
got_ref_list_free(&refs);
return error;
}
diff --git a/include/got_opentemp.h b/include/got_opentemp.h
index 82d4fbd..5df60c3 100644
--- a/include/got_opentemp.h
+++ b/include/got_opentemp.h
@@ -37,3 +37,6 @@ const struct got_error *got_opentemp_named(char **, FILE **, const char *);
/* Like got_opentemp_named() but returns a file descriptor instead of a FILE. */
const struct got_error *got_opentemp_named_fd(char **, int *, const char *);
+
+/* Truncate a file. This is useful for re-using open temporary files. */
+const struct got_error *got_opentemp_truncate(FILE *);
diff --git a/lib/diff.c b/lib/diff.c
index 1c12d84..6319482 100644
--- a/lib/diff.c
+++ b/lib/diff.c
@@ -31,6 +31,7 @@
#include "got_path.h"
#include "got_cancel.h"
#include "got_worktree.h"
+#include "got_opentemp.h"
#include "got_lib_diff.h"
#include "got_lib_delta.h"
@@ -52,18 +53,6 @@ add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
}
static const struct got_error *
-reset_file(FILE *f)
-{
- if (fpurge(f) == EOF)
- return got_error_from_errno("fpurge");
- if (ftruncate(fileno(f), 0L) == -1)
- return got_error_from_errno("ftruncate");
- if (fseeko(f, 0L, SEEK_SET) == -1)
- return got_error_from_errno("fseeko");
- return NULL;
-}
-
-static const struct got_error *
diff_blobs(off_t **line_offsets, size_t *nlines,
struct got_diffreg_result **resultp, struct got_blob_object *blob1,
struct got_blob_object *blob2, FILE *f1, FILE *f2,
@@ -91,12 +80,12 @@ diff_blobs(off_t **line_offsets, size_t *nlines,
*resultp = NULL;
if (f1) {
- err = reset_file(f1);
+ err = got_opentemp_truncate(f1);
if (err)
goto done;
}
if (f2) {
- err = reset_file(f2);
+ err = got_opentemp_truncate(f2);
if (err)
goto done;
}
diff --git a/lib/opentemp.c b/lib/opentemp.c
index e998f5b..4c4e3bb 100644
--- a/lib/opentemp.c
+++ b/lib/opentemp.c
@@ -113,3 +113,15 @@ got_opentemp_named_fd(char **path, int *outfd, const char *basepath)
*outfd = fd;
return err;
}
+
+const struct got_error *
+got_opentemp_truncate(FILE *f)
+{
+ if (fpurge(f) == EOF)
+ return got_error_from_errno("fpurge");
+ if (ftruncate(fileno(f), 0L) == -1)
+ return got_error_from_errno("ftruncate");
+ if (fseeko(f, 0L, SEEK_SET) == -1)
+ return got_error_from_errno("fseeko");
+ return NULL;
+}