BeeOnRope
BeeOnRope

Reputation: 64925

Given a file, can I check if an identical file exists in the history of a git repo?

Given a git repository with a file foo.c, and an arbitrary file, can I check if the given file ever existed with identical contents as foo.c in the repository?

That is, given some copy of a file foo.c I want to check if such (byte-wise identical) foo.c ever existed in the repository.

Upvotes: 4

Views: 359

Answers (3)

Alderath
Alderath

Reputation: 3859

A simple approach would be:

git cat-file -t $(git hash-object path/to/foo.c)`  # Assuming bash syntax...

This would print blob if any file byte-identical to foo.c has been committed to the repo (regardless of file path/name), and fatal: git cat-file: could not get object info if it does not exist in the repo.

It computes the sha-1 hash of your input file path/to/foo.c, then it queries the type of the git-object identified by that hash, which will yield an error message if the file does not exist in the git object database.

Drawback is that it just answers the question whether the file exists in the repo.
It doesn't tell you the path of the matching file, or which commit/commits that contain the matching file.

Upvotes: 2

jthill
jthill

Reputation: 60295

Certainly. git log --raw shows the old and new sha's at every changed path, maybe the simplest output to paw through would be from

git log --raw --no-abbrev --all -m --pretty=format:%H

and hunt for your sha in that.

If "some copy of a file foo.c" means you don't necessarily want to add that foo.c to the repo, you can get shas for arbitrary content with e.g. git hash-object foo.c, otherwise git rev-parse can hunt up any existing id.

So something like

git log --raw --no-abbrev --all -m --pretty=format:%H \
| awk 'NF==1 {commit=$1} $4==thehash{print commit,$0}' thehash=`git hash-object foo.c`

Upvotes: 1

Syntax Error
Syntax Error

Reputation: 1640

Find the file with git log --all -- foo.c or perhaps git rev-list --all | xargs -I '{}' git ls-tree --full-tree -r '{}' | grep 'foo.c'. Then you could compare hashes with git ls-files -s foo.c for the one in the git repo and git hash-object foo.c for the one in your file system, just to ensure it's the same file.

Upvotes: 1

Related Questions