Skip to content

Commit

Permalink
Merge branch 'js/diff-ni' (early part)
Browse files Browse the repository at this point in the history
* 'js/diff-ni' (early part):
  diff --no-index: also imitate the exit status of diff(1)
  Fix typo: do not show name1 when name2 fails
  Teach git-diff-files the new option `--no-index`
  run_diff_{files,index}(): update calling convention.
  update-index: do not die too early in a read-only repository.
  git-status: do not be totally useless in a read-only repository.
  • Loading branch information
Junio C Hamano committed Feb 28, 2007
2 parents c4f8f82 + 34a5e1a commit 25a0b20
Show file tree
Hide file tree
Showing 12 changed files with 285 additions and 86 deletions.
5 changes: 4 additions & 1 deletion Documentation/git-diff-files.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ git-diff-files - Compares files in the working tree and the index

SYNOPSIS
--------
'git-diff-files' [-q] [-0|-1|-2|-3|-c|--cc] [<common diff options>] [<path>...]
'git-diff-files' [-q] [-0|-1|-2|-3|-c|--cc|-n|--no-index] [<common diff options>] [<path>...]

DESCRIPTION
-----------
Expand Down Expand Up @@ -36,6 +36,9 @@ omit diff output for unmerged entries and just show "Unmerged".
diff, similar to the way 'diff-tree' shows a merge
commit with these flags.

\-n,\--no-index::
Compare the two given files / directories.

-q::
Remain silent even on nonexistent files

Expand Down
4 changes: 4 additions & 0 deletions Documentation/git-diff.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ tree and the index file, or the index file and the working tree.
further add to the index but you still haven't. You can
stage these changes by using gitlink:git-add[1].

If exactly two paths are given, and at least one is untracked,
compare the two files / directories. This behavior can be
forced by --no-index.

'git-diff' [--options] --cached [<commit>] [--] [<path>...]::

This form is to view the changes you staged for the next
Expand Down
29 changes: 4 additions & 25 deletions builtin-diff-files.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,42 +10,21 @@
#include "builtin.h"

static const char diff_files_usage[] =
"git-diff-files [-q] [-0/-1/2/3 |-c|--cc] [<common diff options>] [<path>...]"
"git-diff-files [-q] [-0/-1/2/3 |-c|--cc|-n|--no-index] [<common diff options>] [<path>...]"
COMMON_DIFF_OPTIONS_HELP;

int cmd_diff_files(int argc, const char **argv, const char *prefix)
{
struct rev_info rev;
int silent = 0;
int nongit = 0;

prefix = setup_git_directory_gently(&nongit);
init_revisions(&rev, prefix);
git_config(git_default_config); /* no "diff" UI options */
rev.abbrev = 0;

argc = setup_revisions(argc, argv, &rev, NULL);
while (1 < argc && argv[1][0] == '-') {
if (!strcmp(argv[1], "--base"))
rev.max_count = 1;
else if (!strcmp(argv[1], "--ours"))
rev.max_count = 2;
else if (!strcmp(argv[1], "--theirs"))
rev.max_count = 3;
else if (!strcmp(argv[1], "-q"))
silent = 1;
else
usage(diff_files_usage);
argv++; argc--;
}
if (!rev.diffopt.output_format)
rev.diffopt.output_format = DIFF_FORMAT_RAW;

/*
* Make sure there are NO revision (i.e. pending object) parameter,
* rev.max_count is reasonable (0 <= n <= 3),
* there is no other revision filtering parameters.
*/
if (rev.pending.nr ||
rev.min_age != -1 || rev.max_age != -1)
usage(diff_files_usage);
return run_diff_files(&rev, silent);
return run_diff_files_cmd(&rev, argc, argv);
}
4 changes: 4 additions & 0 deletions builtin-diff-index.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,9 @@ int cmd_diff_index(int argc, const char **argv, const char *prefix)
if (rev.pending.nr != 1 ||
rev.max_count != -1 || rev.min_age != -1 || rev.max_age != -1)
usage(diff_cache_usage);
if (read_cache() < 0) {
perror("read_cache");
return -1;
}
return run_diff_index(&rev, cached);
}
42 changes: 7 additions & 35 deletions builtin-diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,40 +25,6 @@ struct blobinfo {
static const char builtin_diff_usage[] =
"git-diff <options> <rev>{0,2} -- <path>*";

static int builtin_diff_files(struct rev_info *revs,
int argc, const char **argv)
{
int silent = 0;
while (1 < argc) {
const char *arg = argv[1];
if (!strcmp(arg, "--base"))
revs->max_count = 1;
else if (!strcmp(arg, "--ours"))
revs->max_count = 2;
else if (!strcmp(arg, "--theirs"))
revs->max_count = 3;
else if (!strcmp(arg, "-q"))
silent = 1;
else
usage(builtin_diff_usage);
argv++; argc--;
}
/*
* Make sure there are NO revision (i.e. pending object) parameter,
* specified rev.max_count is reasonable (0 <= n <= 3), and
* there is no other revision filtering parameter.
*/
if (revs->pending.nr ||
revs->min_age != -1 ||
revs->max_age != -1 ||
3 < revs->max_count)
usage(builtin_diff_usage);
if (revs->max_count < 0 &&
(revs->diffopt.output_format & DIFF_FORMAT_PATCH))
revs->combine_merges = revs->dense_combined_merges = 1;
return run_diff_files(revs, silent);
}

static void stuff_change(struct diff_options *opt,
unsigned old_mode, unsigned new_mode,
const unsigned char *old_sha1,
Expand Down Expand Up @@ -151,6 +117,10 @@ static int builtin_diff_index(struct rev_info *revs,
revs->max_count != -1 || revs->min_age != -1 ||
revs->max_age != -1)
usage(builtin_diff_usage);
if (read_cache() < 0) {
perror("read_cache");
return -1;
}
return run_diff_index(revs, cached);
}

Expand Down Expand Up @@ -219,6 +189,7 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
int ents = 0, blobs = 0, paths = 0;
const char *path = NULL;
struct blobinfo blob[2];
int nongit = 0;

/*
* We could get N tree-ish in the rev.pending_objects list.
Expand All @@ -240,6 +211,7 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
* Other cases are errors.
*/

prefix = setup_git_directory_gently(&nongit);
git_config(git_diff_ui_config);
init_revisions(&rev, prefix);

Expand Down Expand Up @@ -317,7 +289,7 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
if (!ents) {
switch (blobs) {
case 0:
return builtin_diff_files(&rev, argc, argv);
return run_diff_files_cmd(&rev, argc, argv);
break;
case 1:
if (paths != 1)
Expand Down
11 changes: 10 additions & 1 deletion builtin-update-index.c
Original file line number Diff line number Diff line change
Expand Up @@ -487,14 +487,17 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
int prefix_length = prefix ? strlen(prefix) : 0;
char set_executable_bit = 0;
unsigned int refresh_flags = 0;
int lock_error = 0;
struct lock_file *lock_file;

git_config(git_default_config);

/* We can't free this memory, it becomes part of a linked list parsed atexit() */
lock_file = xcalloc(1, sizeof(struct lock_file));

newfd = hold_lock_file_for_update(lock_file, get_index_file(), 1);
newfd = hold_lock_file_for_update(lock_file, get_index_file(), 0);
if (newfd < 0)
lock_error = errno;

entries = read_cache();
if (entries < 0)
Expand Down Expand Up @@ -651,6 +654,12 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)

finish:
if (active_cache_changed) {
if (newfd < 0) {
if (refresh_flags & REFRESH_QUIET)
exit(128);
die("unable to create '%s.lock': %s",
get_index_file(), strerror(lock_error));
}
if (write_cache(newfd, active_cache, active_nr) ||
close(newfd) || commit_lock_file(lock_file))
die("Unable to write new index file");
Expand Down
Loading

0 comments on commit 25a0b20

Please sign in to comment.