Diff clean-up.

This is a long overdue clean-up to the code for parsing and passing
diff options.  It also tightens some constness issues.

Signed-off-by: Junio C Hamano <junkio@cox.net>
This commit is contained in:
Junio C Hamano 2005-09-21 00:00:47 -07:00
parent dbc3743868
commit 6b5ee137e5
11 changed files with 254 additions and 368 deletions

View File

@ -143,9 +143,9 @@ extern char *get_graft_file(void);
#define ALTERNATE_DB_ENVIRONMENT "GIT_ALTERNATE_OBJECT_DIRECTORIES"
extern const char **get_pathspec(const char *prefix, char **pathspec);
extern const char **get_pathspec(const char *prefix, const char **pathspec);
extern const char *setup_git_directory(void);
extern char *prefix_path(const char *prefix, int len, char *path);
extern const char *prefix_path(const char *prefix, int len, const char *path);
#define alloc_nr(x) (((x)+16)*3/2)
@ -158,7 +158,7 @@ extern int cache_name_pos(const char *name, int namelen);
#define ADD_CACHE_SKIP_DFCHECK 4 /* Ok to skip DF conflict checks */
extern int add_cache_entry(struct cache_entry *ce, int option);
extern int remove_cache_entry_at(int pos);
extern int remove_file_from_cache(char *path);
extern int remove_file_from_cache(const char *path);
extern int ce_same_name(struct cache_entry *a, struct cache_entry *b);
extern int ce_match_stat(struct cache_entry *ce, struct stat *st);
extern int ce_modified(struct cache_entry *ce, struct stat *st);

View File

@ -11,87 +11,56 @@ static const char diff_files_usage[] =
"[<common diff options>] [<path>...]"
COMMON_DIFF_OPTIONS_HELP;
static int diff_output_format = DIFF_FORMAT_RAW;
static int diff_line_termination = '\n';
static int detect_rename = 0;
static int find_copies_harder = 0;
static int diff_setup_opt = 0;
static int diff_score_opt = 0;
static const char *pickaxe = NULL;
static int pickaxe_opts = 0;
static int diff_break_opt = -1;
static const char *orderfile = NULL;
static const char *diff_filter = NULL;
static struct diff_options diff_options;
static int silent = 0;
static void show_unmerge(const char *path)
{
diff_unmerge(path);
diff_unmerge(&diff_options, path);
}
static void show_file(int pfx, struct cache_entry *ce)
{
diff_addremove(pfx, ntohl(ce->ce_mode), ce->sha1, ce->name, NULL);
diff_addremove(&diff_options, pfx, ntohl(ce->ce_mode),
ce->sha1, ce->name, NULL);
}
static void show_modified(int oldmode, int mode,
const unsigned char *old_sha1, const unsigned char *sha1,
char *path)
{
diff_change(oldmode, mode, old_sha1, sha1, path, NULL);
diff_change(&diff_options, oldmode, mode, old_sha1, sha1, path, NULL);
}
int main(int argc, char **argv)
int main(int argc, const char **argv)
{
static const unsigned char null_sha1[20] = { 0, };
const char **pathspec;
const char *prefix = setup_git_directory();
int entries, i;
diff_setup(&diff_options);
while (1 < argc && argv[1][0] == '-') {
if (!strcmp(argv[1], "-p") || !strcmp(argv[1], "-u"))
diff_output_format = DIFF_FORMAT_PATCH;
else if (!strcmp(argv[1], "-q"))
if (!strcmp(argv[1], "-q"))
silent = 1;
else if (!strcmp(argv[1], "-r"))
; /* no-op */
else if (!strcmp(argv[1], "-s"))
; /* no-op */
else if (!strcmp(argv[1], "-z"))
diff_line_termination = 0;
else if (!strcmp(argv[1], "--name-only"))
diff_output_format = DIFF_FORMAT_NAME;
else if (!strcmp(argv[1], "-R"))
diff_setup_opt |= DIFF_SETUP_REVERSE;
else if (!strncmp(argv[1], "-S", 2))
pickaxe = argv[1] + 2;
else if (!strncmp(argv[1], "-O", 2))
orderfile = argv[1] + 2;
else if (!strncmp(argv[1], "--diff-filter=", 14))
diff_filter = argv[1] + 14;
else if (!strcmp(argv[1], "--pickaxe-all"))
pickaxe_opts = DIFF_PICKAXE_ALL;
else if (!strncmp(argv[1], "-B", 2)) {
if ((diff_break_opt =
diff_scoreopt_parse(argv[1])) == -1)
else {
int diff_opt_cnt;
diff_opt_cnt = diff_opt_parse(&diff_options,
argv+1, argc-1);
if (diff_opt_cnt < 0)
usage(diff_files_usage);
else if (diff_opt_cnt) {
argv += diff_opt_cnt;
argc -= diff_opt_cnt;
continue;
}
else
usage(diff_files_usage);
}
else if (!strncmp(argv[1], "-M", 2)) {
if ((diff_score_opt =
diff_scoreopt_parse(argv[1])) == -1)
usage(diff_files_usage);
detect_rename = DIFF_DETECT_RENAME;
}
else if (!strncmp(argv[1], "-C", 2)) {
if ((diff_score_opt =
diff_scoreopt_parse(argv[1])) == -1)
usage(diff_files_usage);
detect_rename = DIFF_DETECT_COPY;
}
else if (!strcmp(argv[1], "--find-copies-harder"))
find_copies_harder = 1;
else
usage(diff_files_usage);
argv++; argc--;
}
@ -99,7 +68,7 @@ int main(int argc, char **argv)
pathspec = get_pathspec(prefix, argv + 1);
entries = read_cache();
if (find_copies_harder && detect_rename != DIFF_DETECT_COPY)
if (diff_setup_done(&diff_options) < 0)
usage(diff_files_usage);
/* At this point, if argc == 1, then we are doing everything.
@ -110,8 +79,6 @@ int main(int argc, char **argv)
exit(1);
}
diff_setup(diff_setup_opt);
for (i = 0; i < entries; i++) {
struct stat st;
unsigned int oldmode;
@ -141,18 +108,14 @@ int main(int argc, char **argv)
continue;
}
changed = ce_match_stat(ce, &st);
if (!changed && !find_copies_harder)
if (!changed && !diff_options.find_copies_harder)
continue;
oldmode = ntohl(ce->ce_mode);
show_modified(oldmode, DIFF_FILE_CANON_MODE(st.st_mode),
ce->sha1, (changed ? null_sha1 : ce->sha1),
ce->name);
}
diffcore_std(pathspec,
detect_rename, diff_score_opt,
pickaxe, pickaxe_opts,
diff_break_opt,
orderfile, diff_filter);
diff_flush(diff_output_format, diff_line_termination);
diffcore_std(&diff_options);
diff_flush(&diff_options);
return 0;
}

View File

@ -2,26 +2,20 @@
#include "diff.h"
static int cached_only = 0;
static int diff_output_format = DIFF_FORMAT_RAW;
static int diff_line_termination = '\n';
static int match_nonexisting = 0;
static int detect_rename = 0;
static int find_copies_harder = 0;
static int diff_setup_opt = 0;
static int diff_score_opt = 0;
static const char *pickaxe = NULL;
static int pickaxe_opts = 0;
static int diff_break_opt = -1;
static const char *orderfile = NULL;
static const char *diff_filter = NULL;
static struct diff_options diff_options;
/* A file entry went away or appeared */
static void show_file(const char *prefix, struct cache_entry *ce, unsigned char *sha1, unsigned int mode)
static void show_file(const char *prefix,
struct cache_entry *ce,
unsigned char *sha1, unsigned int mode)
{
diff_addremove(prefix[0], ntohl(mode), sha1, ce->name, NULL);
diff_addremove(&diff_options, prefix[0], ntohl(mode),
sha1, ce->name, NULL);
}
static int get_stat_data(struct cache_entry *ce, unsigned char **sha1p, unsigned int *modep)
static int get_stat_data(struct cache_entry *ce,
unsigned char **sha1p, unsigned int *modep)
{
unsigned char *sha1 = ce->sha1;
unsigned int mode = ce->ce_mode;
@ -77,13 +71,13 @@ static int show_modified(struct cache_entry *old,
oldmode = old->ce_mode;
if (mode == oldmode && !memcmp(sha1, old->sha1, 20) &&
!find_copies_harder)
!diff_options.find_copies_harder)
return 0;
mode = ntohl(mode);
oldmode = ntohl(oldmode);
diff_change(oldmode, mode,
diff_change(&diff_options, oldmode, mode,
old->sha1, sha1, old->name, NULL);
return 0;
}
@ -127,7 +121,7 @@ static int diff_cache(struct cache_entry **ac, int entries, const char **pathspe
break;
/* fallthru */
case 3:
diff_unmerge(ce->name);
diff_unmerge(&diff_options, ce->name);
break;
default:
@ -168,7 +162,7 @@ static const char diff_cache_usage[] =
"[<common diff options>] <tree-ish> [<path>...]"
COMMON_DIFF_OPTIONS_HELP;
int main(int argc, char **argv)
int main(int argc, const char **argv)
{
const char *tree_name = NULL;
unsigned char sha1[20];
@ -180,8 +174,10 @@ int main(int argc, char **argv)
int allow_options = 1;
int i;
diff_setup(&diff_options);
for (i = 1; i < argc; i++) {
const char *arg = argv[i];
int diff_opt_cnt;
if (!allow_options || *arg != '-') {
if (tree_name)
@ -198,60 +194,15 @@ int main(int argc, char **argv)
/* We accept the -r flag just to look like git-diff-tree */
continue;
}
/* We accept the -u flag as a synonym for "-p" */
if (!strcmp(arg, "-p") || !strcmp(arg, "-u")) {
diff_output_format = DIFF_FORMAT_PATCH;
continue;
}
if (!strncmp(arg, "-B", 2)) {
if ((diff_break_opt = diff_scoreopt_parse(arg)) == -1)
usage(diff_cache_usage);
continue;
}
if (!strncmp(arg, "-M", 2)) {
detect_rename = DIFF_DETECT_RENAME;
if ((diff_score_opt = diff_scoreopt_parse(arg)) == -1)
usage(diff_cache_usage);
continue;
}
if (!strncmp(arg, "-C", 2)) {
detect_rename = DIFF_DETECT_COPY;
if ((diff_score_opt = diff_scoreopt_parse(arg)) == -1)
usage(diff_cache_usage);
continue;
}
if (!strcmp(arg, "--find-copies-harder")) {
find_copies_harder = 1;
continue;
}
if (!strcmp(arg, "-z")) {
diff_line_termination = 0;
continue;
}
if (!strcmp(arg, "--name-only")) {
diff_output_format = DIFF_FORMAT_NAME;
continue;
}
if (!strcmp(arg, "-R")) {
diff_setup_opt |= DIFF_SETUP_REVERSE;
continue;
}
if (!strncmp(arg, "-S", 2)) {
pickaxe = arg + 2;
continue;
}
if (!strncmp(arg, "--diff-filter=", 14)) {
diff_filter = arg + 14;
continue;
}
if (!strncmp(arg, "-O", 2)) {
orderfile = arg + 2;
continue;
}
if (!strcmp(arg, "--pickaxe-all")) {
pickaxe_opts = DIFF_PICKAXE_ALL;
diff_opt_cnt = diff_opt_parse(&diff_options, argv + i,
argc - i);
if (diff_opt_cnt < 0)
usage(diff_cache_usage);
else if (diff_opt_cnt) {
i += diff_opt_cnt - 1;
continue;
}
if (!strcmp(arg, "-m")) {
match_nonexisting = 1;
continue;
@ -265,7 +216,7 @@ int main(int argc, char **argv)
pathspec = get_pathspec(prefix, argv + i);
if (find_copies_harder && detect_rename != DIFF_DETECT_COPY)
if (diff_setup_done(&diff_options) < 0)
usage(diff_cache_usage);
if (!tree_name || get_sha1(tree_name, sha1))
@ -273,9 +224,6 @@ int main(int argc, char **argv)
read_cache();
/* The rest is for paths restriction. */
diff_setup(diff_setup_opt);
mark_merge_entries();
tree = read_object_with_reference(sha1, "tree", &size, NULL);
@ -286,11 +234,7 @@ int main(int argc, char **argv)
ret = diff_cache(active_cache, active_nr, pathspec);
diffcore_std(pathspec,
detect_rename, diff_score_opt,
pickaxe, pickaxe_opts,
diff_break_opt,
orderfile, diff_filter);
diff_flush(diff_output_format, diff_line_termination);
diffcore_std(&diff_options);
diff_flush(&diff_options);
return ret;
}

View File

@ -5,17 +5,7 @@
#include "cache.h"
#include "diff.h"
static int diff_output_format = DIFF_FORMAT_RAW;
static int diff_line_termination = '\n';
static int detect_rename = 0;
static int find_copies_harder = 0;
static int diff_setup_opt = 0;
static int diff_score_opt = 0;
static const char *pickaxe = NULL;
static int pickaxe_opts = 0;
static int diff_break_opt = -1;
static const char *orderfile = NULL;
static const char *diff_filter = NULL;
static struct diff_options diff_options;
static const char diff_stages_usage[] =
"git-diff-stages [<common diff options>] <stage1> <stage2> [<path>...]"
@ -47,15 +37,16 @@ static void diff_stages(int stage1, int stage2)
if (!one && !two)
continue;
if (!one)
diff_addremove('+', ntohl(two->ce_mode),
diff_addremove(&diff_options, '+', ntohl(two->ce_mode),
two->sha1, name, NULL);
else if (!two)
diff_addremove('-', ntohl(one->ce_mode),
diff_addremove(&diff_options, '-', ntohl(one->ce_mode),
one->sha1, name, NULL);
else if (memcmp(one->sha1, two->sha1, 20) ||
(one->ce_mode != two->ce_mode) ||
find_copies_harder)
diff_change(ntohl(one->ce_mode), ntohl(two->ce_mode),
diff_options.find_copies_harder)
diff_change(&diff_options,
ntohl(one->ce_mode), ntohl(two->ce_mode),
one->sha1, two->sha1, name, NULL);
}
}
@ -65,44 +56,25 @@ int main(int ac, const char **av)
int stage1, stage2;
read_cache();
diff_setup(&diff_options);
while (1 < ac && av[1][0] == '-') {
const char *arg = av[1];
if (!strcmp(arg, "-r"))
; /* as usual */
else if (!strcmp(arg, "-p") || !strcmp(arg, "-u"))
diff_output_format = DIFF_FORMAT_PATCH;
else if (!strncmp(arg, "-B", 2)) {
if ((diff_break_opt = diff_scoreopt_parse(arg)) == -1)
else {
int diff_opt_cnt;
diff_opt_cnt = diff_opt_parse(&diff_options,
av+1, ac-1);
if (diff_opt_cnt < 0)
usage(diff_stages_usage);
else if (diff_opt_cnt) {
av += diff_opt_cnt;
ac -= diff_opt_cnt;
continue;
}
else
usage(diff_stages_usage);
}
else if (!strncmp(arg, "-M", 2)) {
detect_rename = DIFF_DETECT_RENAME;
if ((diff_score_opt = diff_scoreopt_parse(arg)) == -1)
usage(diff_stages_usage);
}
else if (!strncmp(arg, "-C", 2)) {
detect_rename = DIFF_DETECT_COPY;
if ((diff_score_opt = diff_scoreopt_parse(arg)) == -1)
usage(diff_stages_usage);
}
else if (!strcmp(arg, "--find-copies-harder"))
find_copies_harder = 1;
else if (!strcmp(arg, "-z"))
diff_line_termination = 0;
else if (!strcmp(arg, "--name-only"))
diff_output_format = DIFF_FORMAT_NAME;
else if (!strcmp(arg, "-R"))
diff_setup_opt |= DIFF_SETUP_REVERSE;
else if (!strncmp(arg, "-S", 2))
pickaxe = arg + 2;
else if (!strncmp(arg, "-O", 2))
orderfile = arg + 2;
else if (!strncmp(arg, "--diff-filter=", 14))
diff_filter = arg + 14;
else if (!strcmp(arg, "--pickaxe-all"))
pickaxe_opts = DIFF_PICKAXE_ALL;
else
usage(diff_stages_usage);
ac--; av++;
}
@ -110,21 +82,17 @@ int main(int ac, const char **av)
sscanf(av[1], "%d", &stage1) != 1 ||
! (0 <= stage1 && stage1 <= 3) ||
sscanf(av[2], "%d", &stage2) != 1 ||
! (0 <= stage2 && stage2 <= 3) ||
(find_copies_harder && detect_rename != DIFF_DETECT_COPY))
! (0 <= stage2 && stage2 <= 3))
usage(diff_stages_usage);
av += 3; /* The rest from av[0] are for paths restriction. */
diff_setup(diff_setup_opt);
diff_options.paths = av;
if (diff_setup_done(&diff_options) < 0)
usage(diff_stages_usage);
diff_stages(stage1, stage2);
diffcore_std(av,
detect_rename, diff_score_opt,
pickaxe, pickaxe_opts,
diff_break_opt,
orderfile,
diff_filter);
diff_flush(diff_output_format, diff_line_termination);
diffcore_std(&diff_options);
diff_flush(&diff_options);
return 0;
}

View File

@ -9,17 +9,9 @@ static int ignore_merges = 1;
static int recursive = 0;
static int show_tree_entry_in_recursive = 0;
static int read_stdin = 0;
static int diff_output_format = DIFF_FORMAT_RAW;
static int diff_line_termination = '\n';
static int detect_rename = 0;
static int find_copies_harder = 0;
static int diff_setup_opt = 0;
static int diff_score_opt = 0;
static const char *pickaxe = NULL;
static int pickaxe_opts = 0;
static int diff_break_opt = -1;
static const char *orderfile = NULL;
static const char *diff_filter = NULL;
static struct diff_options diff_options;
static const char *header = NULL;
static const char *header_prefix = "";
static enum cmit_fmt commit_format = CMIT_FMT_RAW;
@ -94,7 +86,7 @@ static void show_file(const char *prefix, void *tree, unsigned long size, const
return;
}
diff_addremove(prefix[0], mode, sha1, base, path);
diff_addremove(&diff_options, prefix[0], mode, sha1, base, path);
}
static int compare_tree_entry(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
@ -118,7 +110,8 @@ static int compare_tree_entry(void *tree1, unsigned long size1, void *tree2, uns
show_file("+", tree2, size2, base);
return 1;
}
if (!find_copies_harder && !memcmp(sha1, sha2, 20) && mode1 == mode2)
if (!diff_options.find_copies_harder &&
!memcmp(sha1, sha2, 20) && mode1 == mode2)
return 0;
/*
@ -135,13 +128,14 @@ static int compare_tree_entry(void *tree1, unsigned long size1, void *tree2, uns
int retval;
char *newbase = malloc_base(base, path1, pathlen1);
if (show_tree_entry_in_recursive)
diff_change(mode1, mode2, sha1, sha2, base, path1);
diff_change(&diff_options, mode1, mode2,
sha1, sha2, base, path1);
retval = diff_tree_sha1(sha1, sha2, newbase);
free(newbase);
return retval;
}
diff_change(mode1, mode2, sha1, sha2, base, path1);
diff_change(&diff_options, mode1, mode2, sha1, sha2, base, path1);
return 0;
}
@ -263,28 +257,26 @@ static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, co
return retval;
}
static void call_diff_setup(void)
static void call_diff_setup_done(void)
{
diff_setup(diff_setup_opt);
diff_setup_done(&diff_options);
}
static int call_diff_flush(void)
{
diffcore_std(NULL,
detect_rename, diff_score_opt,
pickaxe, pickaxe_opts,
diff_break_opt,
orderfile,
diff_filter);
diffcore_std(&diff_options);
if (diff_queue_is_empty()) {
diff_flush(DIFF_FORMAT_NO_OUTPUT, diff_line_termination);
int saved_fmt = diff_options.output_format;
diff_options.output_format = DIFF_FORMAT_NO_OUTPUT;
diff_flush(&diff_options);
diff_options.output_format = saved_fmt;
return 0;
}
if (header) {
printf("%s%c", header, diff_line_termination);
printf("%s%c", header, diff_options.line_termination);
header = NULL;
}
diff_flush(diff_output_format, diff_line_termination);
diff_flush(&diff_options);
return 1;
}
@ -293,7 +285,7 @@ static int diff_tree_sha1_top(const unsigned char *old,
{
int ret;
call_diff_setup();
call_diff_setup_done();
ret = diff_tree_sha1(old, new, base);
call_diff_flush();
return ret;
@ -305,7 +297,7 @@ static int diff_root_tree(const unsigned char *new, const char *base)
void *tree;
unsigned long size;
call_diff_setup();
call_diff_setup_done();
tree = read_object_with_reference(new, "tree", &size, NULL);
if (!tree)
die("unable to read root tree (%s)", sha1_to_hex(new));
@ -409,7 +401,7 @@ static const char diff_tree_usage[] =
"[<common diff options>] <tree-ish> <tree-ish>"
COMMON_DIFF_OPTIONS_HELP;
int main(int argc, char **argv)
int main(int argc, const char **argv)
{
int nr_sha1;
char line[1000];
@ -417,7 +409,10 @@ int main(int argc, char **argv)
const char *prefix = setup_git_directory();
nr_sha1 = 0;
diff_setup(&diff_options);
for (;;) {
int diff_opt_cnt;
const char *arg;
argv++;
@ -434,6 +429,16 @@ int main(int argc, char **argv)
break;
}
diff_opt_cnt = diff_opt_parse(&diff_options, argv, argc);
if (diff_opt_cnt < 0)
usage(diff_tree_usage);
else if (diff_opt_cnt) {
argv += diff_opt_cnt - 1;
argc -= diff_opt_cnt - 1;
continue;
}
if (!strcmp(arg, "--")) {
argv++;
argc--;
@ -447,68 +452,10 @@ int main(int argc, char **argv)
recursive = show_tree_entry_in_recursive = 1;
continue;
}
if (!strcmp(arg, "-R")) {
diff_setup_opt |= DIFF_SETUP_REVERSE;
continue;
}
if (!strcmp(arg, "-p") || !strcmp(arg, "-u")) {
diff_output_format = DIFF_FORMAT_PATCH;
recursive = 1;
continue;
}
if (!strncmp(arg, "-S", 2)) {
pickaxe = arg + 2;
continue;
}
if (!strncmp(arg, "-O", 2)) {
orderfile = arg + 2;
continue;
}
if (!strncmp(arg, "--diff-filter=", 14)) {
diff_filter = arg + 14;
continue;
}
if (!strcmp(arg, "--pickaxe-all")) {
pickaxe_opts = DIFF_PICKAXE_ALL;
continue;
}
if (!strncmp(arg, "-M", 2)) {
detect_rename = DIFF_DETECT_RENAME;
if ((diff_score_opt = diff_scoreopt_parse(arg)) == -1)
usage(diff_tree_usage);
continue;
}
if (!strncmp(arg, "-C", 2)) {
detect_rename = DIFF_DETECT_COPY;
if ((diff_score_opt = diff_scoreopt_parse(arg)) == -1)
usage(diff_tree_usage);
continue;
}
if (!strncmp(arg, "-B", 2)) {
if ((diff_break_opt = diff_scoreopt_parse(arg)) == -1)
usage(diff_tree_usage);
continue;
}
if (!strcmp(arg, "--find-copies-harder")) {
find_copies_harder = 1;
continue;
}
if (!strcmp(arg, "--name-only")) {
diff_output_format = DIFF_FORMAT_NAME;
continue;
}
if (!strcmp(arg, "-z")) {
diff_line_termination = 0;
continue;
}
if (!strcmp(arg, "-m")) {
ignore_merges = 0;
continue;
}
if (!strcmp(arg, "-s")) {
diff_output_format = DIFF_FORMAT_NO_OUTPUT;
continue;
}
if (!strcmp(arg, "-v")) {
verbose_header = 1;
header_prefix = "diff-tree ";
@ -530,8 +477,8 @@ int main(int argc, char **argv)
}
usage(diff_tree_usage);
}
if (find_copies_harder && detect_rename != DIFF_DETECT_COPY)
usage(diff_tree_usage);
if (diff_options.output_format == DIFF_FORMAT_PATCH)
recursive = 1;
paths = get_pathspec(prefix, argv);
if (paths) {
@ -559,9 +506,9 @@ int main(int argc, char **argv)
if (!read_stdin)
return 0;
if (detect_rename)
diff_setup_opt |= (DIFF_SETUP_USE_SIZE_CACHE |
DIFF_SETUP_USE_CACHE);
if (diff_options.detect_rename)
diff_options.setup |= (DIFF_SETUP_USE_SIZE_CACHE |
DIFF_SETUP_USE_CACHE);
while (fgets(line, sizeof(line), stdin))
diff_tree_stdin(line);

137
diff.c
View File

@ -12,7 +12,6 @@
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)
@ -669,11 +668,20 @@ static void run_diff(struct diff_filepair *p)
complete_rewrite);
}
void diff_setup(int flags)
void diff_setup(struct diff_options *options)
{
if (flags & DIFF_SETUP_REVERSE)
reverse_diff = 1;
if (flags & DIFF_SETUP_USE_CACHE) {
memset(options, 0, sizeof(*options));
options->output_format = DIFF_FORMAT_RAW;
options->line_termination = '\n';
options->break_opt = -1;
}
int diff_setup_done(struct diff_options *options)
{
if (options->find_copies_harder &&
options->detect_rename != DIFF_DETECT_COPY)
return -1;
if (options->setup & DIFF_SETUP_USE_CACHE) {
if (!active_cache)
/* read-cache does not die even when it fails
* so it is safe for us to do this here. Also
@ -683,9 +691,55 @@ void diff_setup(int flags)
*/
read_cache();
}
if (flags & DIFF_SETUP_USE_SIZE_CACHE)
if (options->setup & DIFF_SETUP_USE_SIZE_CACHE)
use_size_cache = 1;
return 0;
}
int diff_opt_parse(struct diff_options *options, const char **av, int ac)
{
const char *arg = av[0];
if (!strcmp(arg, "-p") || !strcmp(arg, "-u"))
options->output_format = DIFF_FORMAT_PATCH;
else if (!strcmp(arg, "-z"))
options->line_termination = 0;
else if (!strcmp(arg, "--name-only"))
options->output_format = DIFF_FORMAT_NAME;
else if (!strcmp(arg, "-R"))
options->reverse_diff = 1;
else if (!strncmp(arg, "-S", 2))
options->pickaxe = arg + 2;
else if (!strcmp(arg, "-s"))
options->output_format = DIFF_FORMAT_NO_OUTPUT;
else if (!strncmp(arg, "-O", 2))
options->orderfile = arg + 2;
else if (!strncmp(arg, "--diff-filter=", 14))
options->filter = arg + 14;
else if (!strcmp(arg, "--pickaxe-all"))
options->pickaxe_opts = DIFF_PICKAXE_ALL;
else if (!strncmp(arg, "-B", 2)) {
if ((options->break_opt =
diff_scoreopt_parse(arg)) == -1)
return -1;
}
else if (!strncmp(arg, "-M", 2)) {
if ((options->rename_score =
diff_scoreopt_parse(arg)) == -1)
return -1;
options->detect_rename = DIFF_DETECT_RENAME;
}
else if (!strncmp(arg, "-C", 2)) {
if ((options->rename_score =
diff_scoreopt_parse(arg)) == -1)
return -1;
options->detect_rename = DIFF_DETECT_COPY;
}
else if (!strcmp(arg, "--find-copies-harder"))
options->find_copies_harder = 1;
else
return 0;
return 1;
}
static int parse_num(const char **cp_p)
@ -987,23 +1041,25 @@ static void diff_resolve_rename_copy(void)
diff_debug_queue("resolve-rename-copy done", q);
}
void diff_flush(int diff_output_style, int line_termination)
void diff_flush(struct diff_options *options)
{
struct diff_queue_struct *q = &diff_queued_diff;
int i;
int inter_name_termination = '\t';
int diff_output_format = options->output_format;
int line_termination = options->line_termination;
if (!line_termination)
inter_name_termination = 0;
for (i = 0; i < q->nr; i++) {
struct diff_filepair *p = q->queue[i];
if ((diff_output_style == DIFF_FORMAT_NO_OUTPUT) ||
if ((diff_output_format == DIFF_FORMAT_NO_OUTPUT) ||
(p->status == DIFF_STATUS_UNKNOWN))
continue;
if (p->status == 0)
die("internal error in diff-resolve-rename-copy");
switch (diff_output_style) {
switch (diff_output_format) {
case DIFF_FORMAT_PATCH:
diff_flush_patch(p);
break;
@ -1078,45 +1134,36 @@ static void diffcore_apply_filter(const char *filter)
*q = outq;
}
void diffcore_std(const char **paths,
int detect_rename, int rename_score,
const char *pickaxe, int pickaxe_opts,
int break_opt,
const char *orderfile,
const char *filter)
void diffcore_std(struct diff_options *options)
{
if (paths && paths[0])
diffcore_pathspec(paths);
if (break_opt != -1)
diffcore_break(break_opt);
if (detect_rename)
diffcore_rename(detect_rename, rename_score);
if (break_opt != -1)
if (options->paths && options->paths[0])
diffcore_pathspec(options->paths);
if (options->break_opt != -1)
diffcore_break(options->break_opt);
if (options->detect_rename)
diffcore_rename(options->detect_rename, options->rename_score);
if (options->break_opt != -1)
diffcore_merge_broken();
if (pickaxe)
diffcore_pickaxe(pickaxe, pickaxe_opts);
if (orderfile)
diffcore_order(orderfile);
if (options->pickaxe)
diffcore_pickaxe(options->pickaxe, options->pickaxe_opts);
if (options->orderfile)
diffcore_order(options->orderfile);
diff_resolve_rename_copy();
diffcore_apply_filter(filter);
diffcore_apply_filter(options->filter);
}
void diffcore_std_no_resolve(const char **paths,
const char *pickaxe, int pickaxe_opts,
const char *orderfile,
const char *filter)
void diffcore_std_no_resolve(struct diff_options *options)
{
if (paths && paths[0])
diffcore_pathspec(paths);
if (pickaxe)
diffcore_pickaxe(pickaxe, pickaxe_opts);
if (orderfile)
diffcore_order(orderfile);
diffcore_apply_filter(filter);
if (options->pickaxe)
diffcore_pickaxe(options->pickaxe, options->pickaxe_opts);
if (options->orderfile)
diffcore_order(options->orderfile);
diffcore_apply_filter(options->filter);
}
void diff_addremove(int addremove, unsigned mode,
void diff_addremove(struct diff_options *options,
int addremove, unsigned mode,
const unsigned char *sha1,
const char *base, const char *path)
{
@ -1135,7 +1182,7 @@ void diff_addremove(int addremove, unsigned mode,
* Before the final output happens, they are pruned after
* merged into rename/copy pairs as appropriate.
*/
if (reverse_diff)
if (options->reverse_diff)
addremove = (addremove == '+' ? '-' :
addremove == '-' ? '+' : addremove);
@ -1152,7 +1199,8 @@ void diff_addremove(int addremove, unsigned mode,
diff_queue(&diff_queued_diff, one, two);
}
void diff_change(unsigned old_mode, unsigned new_mode,
void diff_change(struct diff_options *options,
unsigned old_mode, unsigned new_mode,
const unsigned char *old_sha1,
const unsigned char *new_sha1,
const char *base, const char *path)
@ -1160,7 +1208,7 @@ void diff_change(unsigned old_mode, unsigned new_mode,
char concatpath[PATH_MAX];
struct diff_filespec *one, *two;
if (reverse_diff) {
if (options->reverse_diff) {
unsigned tmp;
const unsigned char *tmp_c;
tmp = old_mode; old_mode = new_mode; new_mode = tmp;
@ -1176,7 +1224,8 @@ void diff_change(unsigned old_mode, unsigned new_mode,
diff_queue(&diff_queued_diff, one, two);
}
void diff_unmerge(const char *path)
void diff_unmerge(struct diff_options *options,
const char *path)
{
struct diff_filespec *one, *two;
one = alloc_filespec(path);

47
diff.h
View File

@ -8,18 +8,37 @@
(S_ISREG(mode) ? (S_IFREG | ce_permissions(mode)) : \
S_ISLNK(mode) ? S_IFLNK : S_IFDIR)
extern void diff_addremove(int addremove,
struct diff_options {
const char **paths;
const char *filter;
const char *orderfile;
const char *pickaxe;
int break_opt;
int detect_rename;
int find_copies_harder;
int line_termination;
int output_format;
int pickaxe_opts;
int rename_score;
int reverse_diff;
int setup;
};
extern void diff_addremove(struct diff_options *,
int addremove,
unsigned mode,
const unsigned char *sha1,
const char *base,
const char *path);
extern void diff_change(unsigned mode1, unsigned mode2,
const unsigned char *sha1,
const unsigned char *sha2,
const char *base, const char *path);
extern void diff_change(struct diff_options *,
unsigned mode1, unsigned mode2,
const unsigned char *sha1,
const unsigned char *sha2,
const char *base, const char *path);
extern void diff_unmerge(const char *path);
extern void diff_unmerge(struct diff_options *,
const char *path);
extern int diff_scoreopt_parse(const char *opt);
@ -27,22 +46,18 @@ extern int diff_scoreopt_parse(const char *opt);
#define DIFF_SETUP_USE_CACHE 2
#define DIFF_SETUP_USE_SIZE_CACHE 4
extern void diff_setup(int flags);
extern void diff_setup(struct diff_options *);
extern int diff_opt_parse(struct diff_options *, const char **, int);
extern int diff_setup_done(struct diff_options *);
#define DIFF_DETECT_RENAME 1
#define DIFF_DETECT_COPY 2
#define DIFF_PICKAXE_ALL 1
extern void diffcore_std(const char **paths,
int detect_rename, int rename_score,
const char *pickaxe, int pickaxe_opts,
int break_opt,
const char *orderfile, const char *filter);
extern void diffcore_std(struct diff_options *);
extern void diffcore_std_no_resolve(const char **paths,
const char *pickaxe, int pickaxe_opts,
const char *orderfile, const char *filter);
extern void diffcore_std_no_resolve(struct diff_options *);
#define COMMON_DIFF_OPTIONS_HELP \
"\ncommon diff options:\n" \
@ -71,7 +86,7 @@ extern int diff_queue_is_empty(void);
#define DIFF_FORMAT_NO_OUTPUT 3
#define DIFF_FORMAT_NAME 4
extern void diff_flush(int output_style, int line_terminator);
extern void diff_flush(struct diff_options*);
/* diff-raw status letters */
#define DIFF_STATUS_ADDED 'A'

View File

@ -31,7 +31,7 @@ static const char *tag_other = "";
static const char *tag_killed = "";
static const char *tag_modified = "";
static char *exclude_per_dir = NULL;
static const char *exclude_per_dir = NULL;
/* We maintain three exclude pattern lists:
* EXC_CMDL lists patterns explicitly given on the command line.
@ -532,7 +532,7 @@ static const char ls_files_usage[] =
"[ --ignored ] [--exclude=<pattern>] [--exclude-from=<file>] "
"[ --exclude-per-directory=<filename> ]";
int main(int argc, char **argv)
int main(int argc, const char **argv)
{
int i;
int exc_given = 0;
@ -542,7 +542,7 @@ int main(int argc, char **argv)
prefix_offset = strlen(prefix);
for (i = 1; i < argc; i++) {
char *arg = argv[i];
const char *arg = argv[i];
if (!strcmp(arg, "-z")) {
line_terminator = 0;

View File

@ -232,7 +232,7 @@ int remove_cache_entry_at(int pos)
return 1;
}
int remove_file_from_cache(char *path)
int remove_file_from_cache(const char *path)
{
int pos = cache_name_pos(path, strlen(path));
if (pos < 0)

10
setup.c
View File

@ -1,8 +1,8 @@
#include "cache.h"
char *prefix_path(const char *prefix, int len, char *path)
const char *prefix_path(const char *prefix, int len, const char *path)
{
char *orig = path;
const char *orig = path;
for (;;) {
char c;
if (*path != '.')
@ -47,10 +47,10 @@ char *prefix_path(const char *prefix, int len, char *path)
return path;
}
const char **get_pathspec(const char *prefix, char **pathspec)
const char **get_pathspec(const char *prefix, const char **pathspec)
{
char *entry = *pathspec;
char **p;
const char *entry = *pathspec;
const char **p;
int prefixlen;
if (!prefix && !entry)

View File

@ -31,7 +31,7 @@ static inline long IS_ERR(const void *ptr)
return (unsigned long)ptr > (unsigned long)-1000L;
}
static int add_file_to_cache(char *path)
static int add_file_to_cache(const char *path)
{
int size, namelen, option, status;
struct cache_entry *ce;
@ -221,7 +221,7 @@ static int verify_dotfile(const char *rest)
return 1;
}
static int verify_path(char *path)
static int verify_path(const char *path)
{
char c;
@ -247,7 +247,7 @@ inside:
}
}
static int add_cacheinfo(char *arg1, char *arg2, char *arg3)
static int add_cacheinfo(const char *arg1, const char *arg2, const char *arg3)
{
int size, len, option;
unsigned int mode;
@ -277,7 +277,7 @@ static int add_cacheinfo(char *arg1, char *arg2, char *arg3)
static struct cache_file cache_file;
int main(int argc, char **argv)
int main(int argc, const char **argv)
{
int i, newfd, entries, has_errors = 0;
int allow_options = 1;
@ -292,7 +292,7 @@ int main(int argc, char **argv)
die("cache corrupted");
for (i = 1 ; i < argc; i++) {
char *path = argv[i];
const char *path = argv[i];
if (allow_options && *path == '-') {
if (!strcmp(path, "--")) {