Skip to contentSkip to conversation

Best way to check whether working tree matches a commit's tree

DISCUSSION Sebastian Schuberth 2 participants 3 messages Aug 22, 2017

lore
Share & tools
lore
#0 lore raw

Hi,

I'd like to check whether my working tree exactly matches the tree of a given commit. That is, there should not be any untracked, staged or modified files (including ignored files).

Currently, I'm doing this in two steps:

- check for success and empty output of "git status --ignored --porcelain"
- check that the output of "git rev-parse HEAD" matches the given commit

While this works, it feels sub-optimal. Is there a better / smarter way?

Signature · 1 line
Sebastian Schuberth
#1 lore raw
Quoted 10 lines · Sebastian Schuberth

Hi,

I'd like to check whether my working tree exactly matches the tree of a given commit. That is, there should not be any untracked, staged or modified files (including ignored files).

Currently, I'm doing this in two steps:

- check for success and empty output of "git status --ignored --porcelain"
- check that the output of "git rev-parse HEAD" matches the given commit

While this works, it feels sub-optimal. Is there a better / smarter way?

I do not think so; you want three things to match and you have a way to compare two things at a time.

By the way, I think your second check should compare

	rev-parse HEAD^{tree} $that_commit^{tree}

as you are checking if the tree exactly matches.

#2 lore raw
Quoted 4 lines · Junio C Hamano
Quoted 1 line

While this works, it feels sub-optimal. Is there a better / smarter way?

I do not think so; you want three things to match and you have a way to compare two things at a time.

Right. I was just thinking if there's a lesser known command like "git diff --no-index", but instead of taking two paths, take just one path and a commit.

Quoted 5 lines

By the way, I think your second check should compare

        rev-parse HEAD^{tree} $that_commit^{tree}

as you are checking if the tree exactly matches.

In fact, I was considering to use "git diff HEAD $that_commit" as I don't really care whether the SHA1s are equal, but just about the file contents / tree.

Signature · 1 line
Sebastian Schuberth