Andrew's git
/
gitweb.git
/ diff
summary
|
log
|
commit
| diff |
tree
commit
grep
author
committer
pickaxe
?
re
diff: use tempfile module
author
Michael Haggerty
<mhagger@alum.mit.edu>
Wed, 12 Aug 2015 17:12:01 +0000
(19:12 +0200)
committer
Junio C Hamano
<gitster@pobox.com>
Wed, 12 Aug 2015 21:49:43 +0000
(14:49 -0700)
Also add some code comments explaining how the fields in "struct
diff_tempfile" are used.
Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
diff.c
patch
|
blob
|
history
raw
|
patch
|
inline
| side by side (parent:
6e122b4
)
diff --git
a/diff.c
b/diff.c
index 7500c5509550ccd9a86d620e06c51a71d61f8b6c..528d25ca9c3ec8df316bf2cd331151d7a71d5dd3 100644
(file)
--- a/
diff.c
+++ b/
diff.c
@@
-2,6
+2,7
@@
* Copyright (C) 2005 Junio C Hamano
*/
#include "cache.h"
* Copyright (C) 2005 Junio C Hamano
*/
#include "cache.h"
+#include "tempfile.h"
#include "quote.h"
#include "diff.h"
#include "diffcore.h"
#include "quote.h"
#include "diff.h"
#include "diffcore.h"
@@
-308,11
+309,26
@@
static const char *external_diff(void)
return external_diff_cmd;
}
return external_diff_cmd;
}
+/*
+ * Keep track of files used for diffing. Sometimes such an entry
+ * refers to a temporary file, sometimes to an existing file, and
+ * sometimes to "/dev/null".
+ */
static struct diff_tempfile {
static struct diff_tempfile {
- const char *name; /* filename external diff should read from */
+ /*
+ * filename external diff should read from, or NULL if this
+ * entry is currently not in use:
+ */
+ const char *name;
+
char hex[41];
char mode[10];
char hex[41];
char mode[10];
- char tmp_path[PATH_MAX];
+
+ /*
+ * If this diff_tempfile instance refers to a temporary file,
+ * this tempfile object is used to manage its lifetime.
+ */
+ struct tempfile tempfile;
} diff_temp[2];
typedef unsigned long (*sane_truncate_fn)(char *line, unsigned long len);
} diff_temp[2];
typedef unsigned long (*sane_truncate_fn)(char *line, unsigned long len);
@@
-564,25
+580,16
@@
static struct diff_tempfile *claim_diff_tempfile(void) {
die("BUG: diff is failing to clean up its tempfiles");
}
die("BUG: diff is failing to clean up its tempfiles");
}
-static int remove_tempfile_installed;
-
static void remove_tempfile(void)
{
int i;
for (i = 0; i < ARRAY_SIZE(diff_temp); i++) {
static void remove_tempfile(void)
{
int i;
for (i = 0; i < ARRAY_SIZE(diff_temp); i++) {
- if (
diff_temp[i].name == diff_temp[i].tmp_path
)
-
unlink_or_warn(diff_temp[i].nam
e);
+ if (
is_tempfile_active(&diff_temp[i].tempfile)
)
+
delete_tempfile(&diff_temp[i].tempfil
e);
diff_temp[i].name = NULL;
}
}
diff_temp[i].name = NULL;
}
}
-static void remove_tempfile_on_signal(int signo)
-{
- remove_tempfile();
- sigchain_pop(signo);
- raise(signo);
-}
-
static void print_line_count(FILE *file, int count)
{
switch (count) {
static void print_line_count(FILE *file, int count)
{
switch (count) {
@@
-2817,8
+2824,7
@@
static void prep_temp_blob(const char *path, struct diff_tempfile *temp,
strbuf_addstr(&template, "XXXXXX_");
strbuf_addstr(&template, base);
strbuf_addstr(&template, "XXXXXX_");
strbuf_addstr(&template, base);
- fd = git_mkstemps(temp->tmp_path, PATH_MAX, template.buf,
- strlen(base) + 1);
+ fd = mks_tempfile_ts(&temp->tempfile, template.buf, strlen(base) + 1);
if (fd < 0)
die_errno("unable to create temp-file");
if (convert_to_working_tree(path,
if (fd < 0)
die_errno("unable to create temp-file");
if (convert_to_working_tree(path,
@@
-2828,8
+2834,8
@@
static void prep_temp_blob(const char *path, struct diff_tempfile *temp,
}
if (write_in_full(fd, blob, size) != size)
die_errno("unable to write temp-file");
}
if (write_in_full(fd, blob, size) != size)
die_errno("unable to write temp-file");
- close
(fd
);
- temp->name =
temp->tmp_path
;
+ close
_tempfile(&temp->tempfile
);
+ temp->name =
get_tempfile_path(&temp->tempfile)
;
strcpy(temp->hex, sha1_to_hex(sha1));
temp->hex[40] = 0;
sprintf(temp->mode, "%06o", mode);
strcpy(temp->hex, sha1_to_hex(sha1));
temp->hex[40] = 0;
sprintf(temp->mode, "%06o", mode);
@@
-2854,12
+2860,6
@@
static struct diff_tempfile *prepare_temp_file(const char *name,
return temp;
}
return temp;
}
- if (!remove_tempfile_installed) {
- atexit(remove_tempfile);
- sigchain_push_common(remove_tempfile_on_signal);
- remove_tempfile_installed = 1;
- }
-
if (!S_ISGITLINK(one->mode) &&
(!one->sha1_valid ||
reuse_worktree_file(name, one->sha1, 1))) {
if (!S_ISGITLINK(one->mode) &&
(!one->sha1_valid ||
reuse_worktree_file(name, one->sha1, 1))) {