/*
* Copyright (C) 2005 Junio C Hamano
*/
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include "cache.h"
#include "quote.h"
#include "diff.h"
#include "diffcore.h"
static const char *diff_opts = "-pu";
static unsigned char null_sha1[20] = { 0, };
static int reverse_diff;
static int use_size_cache;
static const char *external_diff(void)
{
static const char *external_diff_cmd = NULL;
static int done_preparing = 0;
const char *env_diff_opts;
if (done_preparing)
return external_diff_cmd;
/*
* Default values above are meant to match the
* Linux kernel development style. Examples of
* alternative styles you can specify via environment
* variables are:
*
* GIT_DIFF_OPTS="-c";
*/
external_diff_cmd = getenv("GIT_EXTERNAL_DIFF");
/* In case external diff fails... */
env_diff_opts = getenv("GIT_DIFF_OPTS");
if (env_diff_opts) diff_opts = env_diff_opts;
done_preparing = 1;
return external_diff_cmd;
}
#define TEMPFILE_PATH_LEN 50
static struct diff_tempfile {
const char *name; /* filename external diff should read from */
char hex[41];
char mode[10];
char tmp_path[TEMPFILE_PATH_LEN];
} diff_temp[2];
static int count_lines(const char *filename)
{
FILE *in;
int count, ch, completely_empty = 1, nl_just_seen = 0;
in = fopen(filename, "r");
count = 0;
while ((ch = fgetc(in)) != EOF)
if (ch == '\n') {
count++;
nl_just_seen = 1;
completely_empty = 0;
}
else {
nl_just_seen = 0;
completely_empty = 0;
}
fclose(in);
if (completely_empty)
return 0;
if (!nl_just_seen)
count++; /* no trailing newline */
return count;
}
static void print_line_count(int count)
{
switch (count) {
case 0:
printf("0,0");
break;
case 1:
printf("1");
break;
default:
printf("1,%d", count);
break;
}
}
static void copy_file(int prefix, const char *filename)
{
FILE *in;
int ch, nl_just_seen = 1;
in = fopen(filename, "r");
while ((ch = fgetc(in)) != EOF) {
if (nl_just_seen)
putchar(prefix);
putchar(ch);
if (ch == '\n')
nl_just_seen = 1;
else
nl_just_seen = 0;
}
fclose(in);
if (!nl_just_seen)
printf("\n\\ No newline at end of file\n");
}
static void emit_rewrite_diff(const char *name_a,
const char *name_b,
struct diff_tempfile *temp)
{
/* Use temp[i].name as input, name_a and name_b as labels */
int lc_a, lc_b;
lc_a = count_lines(temp[0].name);
lc_b = count_lines(temp[1].name);
printf("--- %s\n+++ %s\n@@ -", name_a, name_b);
print_line_count(lc_a);
printf(" +");
print_line_count(lc_b);
printf(" @@\n");
if (lc_a)
copy_file('-', temp[0].name);
if (lc_b)
copy_file('+', temp[1].name);
}
static void builtin_diff(const char *name_a,
const char *name_b,
struct diff_tempfile *temp,
const char *xfrm_msg,
int complete_rewrite)
{
int i, next_at, cmd_size;
const char *const diff_cmd = "diff -L%s%s -L%s%s";
const char *const diff_arg = "%s %s||:"; /* "||:" is to return 0 */
const char *input_name_sq[2];
const char *path0[2];
const char *path1[2];
const char *name_sq[2
|