aboutsummaryrefslogtreecommitdiff
path: root/diff.c
diff options
context:
space:
mode:
authorJunio C Hamano <junkio@cox.net>2005-04-28 10:13:01 -0700
committerLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-28 10:13:01 -0700
commit532149d72735b4c5035f73abd06f3995c6303acf (patch)
treeef9b496c135a89bde7214517a8b5a02b3e796662 /diff.c
parent91a6eaa05da5627b1238b7168be1d8ae69516383 (diff)
downloadgit-532149d72735b4c5035f73abd06f3995c6303acf.tar.gz
git-532149d72735b4c5035f73abd06f3995c6303acf.tar.xz
[PATCH] diff.c: clean temporary files
When diff-cache -p and friends are interrupted, they can leave their temporary files behind. Also when the external diff program is killed instead of exiting (this usually happens when piping the output to a pager, which can cause SIGPIPE when the user quits viewing the diff early), they incorrectly died without cleaning their temporary file. This fixes these problems. Signed-off-by: Junio C Hamano <junkio@cox.net> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
Diffstat (limited to 'diff.c')
-rw-r--r--diff.c29
1 files changed, 24 insertions, 5 deletions
diff --git a/diff.c b/diff.c
index c064e1058..8659e4b04 100644
--- a/diff.c
+++ b/diff.c
@@ -3,6 +3,7 @@
*/
#include <sys/types.h>
#include <sys/wait.h>
+#include <signal.h>
#include "cache.h"
#include "diff.h"
@@ -135,6 +136,9 @@ static void prepare_temp_file(const char *name,
if (!one->file_valid) {
not_a_valid_file:
+ /* A '-' entry produces this for file-2, and
+ * a '+' entry produces this for file-1.
+ */
temp->name = "/dev/null";
strcpy(temp->hex, ".");
strcpy(temp->mode, ".");
@@ -155,7 +159,7 @@ static void prepare_temp_file(const char *name,
goto not_a_valid_file;
die("stat(%s): %s", temp->name, strerror(errno));
}
- strcpy(temp->hex, ".");
+ strcpy(temp->hex, sha1_to_hex(null_sha1));
sprintf(temp->mode, "%06o",
S_IFREG |ce_permissions(st.st_mode));
}
@@ -196,6 +200,11 @@ static void remove_tempfile(void)
}
}
+static void remove_tempfile_on_signal(int signo)
+{
+ remove_tempfile();
+}
+
/* An external diff command takes:
*
* diff-cmd name infile1 infile1-sha1 infile1-mode \
@@ -207,7 +216,8 @@ void run_external_diff(const char *name,
struct diff_spec *two)
{
struct diff_tempfile *temp = diff_temp;
- int pid, status;
+ pid_t pid;
+ int status;
static int atexit_asked = 0;
if (one && two) {
@@ -219,6 +229,7 @@ void run_external_diff(const char *name,
atexit_asked = 1;
atexit(remove_tempfile);
}
+ signal(SIGINT, remove_tempfile_on_signal);
}
fflush(NULL);
@@ -246,9 +257,17 @@ void run_external_diff(const char *name,
printf("* Unmerged path %s\n", name);
exit(0);
}
- if (waitpid(pid, &status, 0) < 0 || !WIFEXITED(status))
- die("diff program failed");
-
+ if (waitpid(pid, &status, 0) < 0 || !WIFEXITED(status)) {
+ /* We do not check the exit status because typically
+ * diff exits non-zero if files are different, and
+ * we are not interested in knowing that. We *knew*
+ * they are different and that's why we ran diff
+ * in the first place! However if it dies by a signal,
+ * we stop processing immediately.
+ */
+ remove_tempfile();
+ die("external diff died unexpectedly.\n");
+ }
remove_tempfile();
}