Skip to content

Commit 284098f

Browse files
mhaggergitster
authored andcommitted
diff: use tempfile module
Also add some code comments explaining how the fields in "struct diff_tempfile" are used. Signed-off-by: Michael Haggerty <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6e122b4 commit 284098f

File tree

1 file changed

+23
-23
lines changed

1 file changed

+23
-23
lines changed

diff.c

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* Copyright (C) 2005 Junio C Hamano
33
*/
44
#include "cache.h"
5+
#include "tempfile.h"
56
#include "quote.h"
67
#include "diff.h"
78
#include "diffcore.h"
@@ -308,11 +309,26 @@ static const char *external_diff(void)
308309
return external_diff_cmd;
309310
}
310311

312+
/*
313+
* Keep track of files used for diffing. Sometimes such an entry
314+
* refers to a temporary file, sometimes to an existing file, and
315+
* sometimes to "/dev/null".
316+
*/
311317
static struct diff_tempfile {
312-
const char *name; /* filename external diff should read from */
318+
/*
319+
* filename external diff should read from, or NULL if this
320+
* entry is currently not in use:
321+
*/
322+
const char *name;
323+
313324
char hex[41];
314325
char mode[10];
315-
char tmp_path[PATH_MAX];
326+
327+
/*
328+
* If this diff_tempfile instance refers to a temporary file,
329+
* this tempfile object is used to manage its lifetime.
330+
*/
331+
struct tempfile tempfile;
316332
} diff_temp[2];
317333

318334
typedef unsigned long (*sane_truncate_fn)(char *line, unsigned long len);
@@ -564,25 +580,16 @@ static struct diff_tempfile *claim_diff_tempfile(void) {
564580
die("BUG: diff is failing to clean up its tempfiles");
565581
}
566582

567-
static int remove_tempfile_installed;
568-
569583
static void remove_tempfile(void)
570584
{
571585
int i;
572586
for (i = 0; i < ARRAY_SIZE(diff_temp); i++) {
573-
if (diff_temp[i].name == diff_temp[i].tmp_path)
574-
unlink_or_warn(diff_temp[i].name);
587+
if (is_tempfile_active(&diff_temp[i].tempfile))
588+
delete_tempfile(&diff_temp[i].tempfile);
575589
diff_temp[i].name = NULL;
576590
}
577591
}
578592

579-
static void remove_tempfile_on_signal(int signo)
580-
{
581-
remove_tempfile();
582-
sigchain_pop(signo);
583-
raise(signo);
584-
}
585-
586593
static void print_line_count(FILE *file, int count)
587594
{
588595
switch (count) {
@@ -2817,8 +2824,7 @@ static void prep_temp_blob(const char *path, struct diff_tempfile *temp,
28172824
strbuf_addstr(&template, "XXXXXX_");
28182825
strbuf_addstr(&template, base);
28192826

2820-
fd = git_mkstemps(temp->tmp_path, PATH_MAX, template.buf,
2821-
strlen(base) + 1);
2827+
fd = mks_tempfile_ts(&temp->tempfile, template.buf, strlen(base) + 1);
28222828
if (fd < 0)
28232829
die_errno("unable to create temp-file");
28242830
if (convert_to_working_tree(path,
@@ -2828,8 +2834,8 @@ static void prep_temp_blob(const char *path, struct diff_tempfile *temp,
28282834
}
28292835
if (write_in_full(fd, blob, size) != size)
28302836
die_errno("unable to write temp-file");
2831-
close(fd);
2832-
temp->name = temp->tmp_path;
2837+
close_tempfile(&temp->tempfile);
2838+
temp->name = get_tempfile_path(&temp->tempfile);
28332839
strcpy(temp->hex, sha1_to_hex(sha1));
28342840
temp->hex[40] = 0;
28352841
sprintf(temp->mode, "%06o", mode);
@@ -2854,12 +2860,6 @@ static struct diff_tempfile *prepare_temp_file(const char *name,
28542860
return temp;
28552861
}
28562862

2857-
if (!remove_tempfile_installed) {
2858-
atexit(remove_tempfile);
2859-
sigchain_push_common(remove_tempfile_on_signal);
2860-
remove_tempfile_installed = 1;
2861-
}
2862-
28632863
if (!S_ISGITLINK(one->mode) &&
28642864
(!one->sha1_valid ||
28652865
reuse_worktree_file(name, one->sha1, 1))) {

0 commit comments

Comments
 (0)