Skip to content

Commit

Permalink
Switch from diff-index to diff
Browse files Browse the repository at this point in the history
`diff-index`:

    Compare the content and mode of the blobs found in a tree object
    with the corresponding tracked files in the working tree, or with
    the corresponding paths in the index.

Unlike `diff`, which only compares the content. This has the implication
(only on Linux) that if you were to recreate a file (or do something
like change a file's timestamp with `touch`, as in the test) then a
check with `diff-index` would drop us into that code path for there to
subsequently be no known files which had changed.

This behaviour came up when testing with Appraisal, which re-creates
lockfiles when run, but the content is the same.

https://stackoverflow.com/questions/24197606/whats-the-difference-between-git-diff-and-gif-diff-index
https://github.com/thoughtbot/appraisal
  • Loading branch information
nickcharlton committed Sep 2, 2024
1 parent f9da4c4 commit d484cff
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion bin/diff-check
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

set -e

if ! git diff-index --quiet HEAD; then
if ! git diff --quiet HEAD; then
printf 'These files changed when running the command:\n\n'

git diff --name-only | while read -r n ; do
Expand Down
20 changes: 20 additions & 0 deletions spec/black_box/diff-check_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,31 @@
end
end

context "with a non-content file change" do
it "emits no output" do
session = create_session

session.run("touch README")

expect(session.run("diff-check")).to have_no_stdout
expect(session.run("diff-check")).to have_no_stderr
end

it "exits with a status of 0" do
session = create_session

session.run("touch README")

expect(session.run("diff-check")).to be_a_success
end
end

def create_session
session = JetBlack::Session.new

session.run("git init")
session.run("echo 'Hello world' >> README")
session.run("touch -d '2 hours ago' README")
session.run("git add .; git commit -m 'initial commit'")

session
Expand Down

0 comments on commit d484cff

Please sign in to comment.