-2

I believe the two functions below is the same. But AI keep telling me they are different (that I think AI is wrong).

get_changed_files() {
    local staged_files committed_files
    staged_files=$(git diff --cached --name-only)
    committed_files=$(git diff --name-only "$1"..HEAD)

    # Combine and deduplicate files
    printf "%s\n%s\n" "$staged_files" "$committed_files" | sort -u | grep -v '^$'
}
get_changed_files() {
   git diff --name-only "$1" --cached
}

To be safe, I'm checking here, to see if I miss anything.

2
  • 1
    What did AI say why they are different? Commented Oct 28 at 14:47
  • 1
    execise: can you imagine a given case, where a filename would be listed in staged_files and/or committed_files (first function), but not in the comparison between the index and $1 (second function) ? Commented Oct 28 at 15:50

1 Answer 1

0

After some further thought, technically they are the same. However, in detail, there some slight nuances diff.

Case as below

Action 1
- Add and Commit Change to ABC

Action 2
- Revert Commit Change to ABC, and Add (but not commit)

Then
- `git diff --cached --name-only` will return ABC
- `git diff --name-only "$1"..HEAD` will return ABC
- This means `get_changed_file()` will return ABC, even thought technically the line change is 0.

However
- `git diff --name-only "$1" --cached` will not return anything
- because Action 2 has reverted Action 1.

Hence, there's some slight different here, even though overall, if we want to just count the combined line change, both will be the same.

Sign up to request clarification or add additional context in comments.

3 Comments

How did you manage to escape the markdown?! And why? Wouldn't it have been easier and looked better if you didn't?
The mark down, didn't allow me to bullet point the code.
Of course, you just shouldn't have put all the backslashes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.