Is a given file known to git?
Given a repository and a path p to a file in it:
Is it possible (how?) to detect (in a bash script) if the file pointed to by p is "known" to git? Something along the line:
if `git knows p?
then
...
fi
Thanks and keep up the good work!
Signature · 1 line
Christoph Duelli
Quoted 8 lines · Christoph Duelli
Given a repository and a path p to a file in it:
Is it possible (how?) to detect (in a bash script) if the file pointed to by p is "known" to git?
Something along the line:
if `git knows p?
then
...
fi
Depends on your definition of "known to git". You can look at the current index, which may have files newly added but not yet committed:
if test $(git ls-files "$p" | wc -l) -gt 0 then ... fi
you can look at a specific committed state too:
if git cat-file -e "$commit:$p" 2>/dev/null then ... fi
where $commit can be any tree-ish, so a tag, a branch, a commit, the symbolic-ref HEAD... etc.
Signature · 1 line
Shawn.
Quoted 8 lines · Christoph Duelli
Given a repository and a path p to a file in it:
Is it possible (how?) to detect (in a bash script) if the file pointed to by p is "known" to git?
Something along the line:
if `git knows p?
then
...
fi
For simplicity, I'll assume with "known to git" you mean whether the current branch's tip has some content at the given path. Then you could write:
if git rev-parse HEAD:"$p" >/dev/null 2>&1 then
# git knows about $p
else
# git does not know about $p
fi
-- Hannes
Quoted 13 lines · Johannes Sixt
Quoted 8 lines · Christoph Duelli
Given a repository and a path p to a file in it:
Is it possible (how?) to detect (in a bash script) if the file pointed to by p is "known" to git?
Something along the line:
if `git knows p?
then
...
fiFor simplicity, I'll assume with "known to git" you mean whether the current branch's tip has some content at the given path. Then you could write:
Quoted 8 lines
if git rev-parse HEAD:"$p" >/dev/null 2>&1 then
# git knows about $p
else
# git does not know about $p
fi-- Hannes
Yes, thank you, Johannes and Shawn, this works.
(A bit slow, though: with this test enabled my script takes 77 secs; without it it takes 0.3 secs. The time is spent in 9000 calls to the above test (the rev-parse version). (ok, the fact that there is a Perl system call around it might take some time, too).)
[For the record:
Shawn's ls-files variant takes: 148 secs Shawn's cat-file variant takes: 251 secs Time taken by time; roughly half user, half system. ]
As this script won't run often, that not too big a deal. Still I would have hoped for a quicker way.
Thanks
Signature · 1 line
Christoph Duelli
Quoted 5 lines · Christoph Duelli
Yes, thank you, Johannes and Shawn, this works.
(A bit slow, though: with this test enabled my script takes 77 secs; without it it takes 0.3 secs. The time is spent in 9000 calls to the above test (the rev-parse version). (ok, the fact that there is a Perl system call around it might take some time, too).)
Oh, you said "a particular path from a bash script". If you had said "9000 particular paths, and I have perl wrapped around it", then there might be (much) better solutions. Like parsing the output of "git ls-files --others" or "git ls-tree -r" into a hash and then lookup therein.
-- Hannes
Quoted 7 lines · Johannes Sixt
Quoted 5 lines · Christoph Duelli
Yes, thank you, Johannes and Shawn, this works.
(A bit slow, though: with this test enabled my script takes 77 secs; without it it takes 0.3 secs. The time is spent in 9000 calls to the above test (the rev-parse version). (ok, the fact that there is a Perl system call around it might take some time, too).)
Quoted 6 lines
Oh, you said "a particular path from a bash script". If you had said "9000 particular paths, and I have perl wrapped around it", then there might be (much) better solutions. Like parsing the output of "git ls-files --others" or "git ls-tree -r" into a hash and then lookup therein.
-- Hannes
Right you are ;-)
I just assumed that such a test would be quick, so I didn't worry about speed back then. But yes, parsing the output of "igt ls-files" would solve that nicely.
Thank you.
Signature · 1 line
Christoph Duelli
Quoted 6 lines · Christoph Duelli
Yes, thank you, Johannes and Shawn, this works.
(A bit slow, though: with this test enabled my script takes 77 secs; without it it takes 0.3 secs. The time is spent in 9000 calls to the above test (the rev-parse version). (ok, the fact that there is a Perl system call around it might take some time, too).)
Quoted 7 lines
[For the record:
Shawn's ls-files variant takes: 148 secs Shawn's cat-file variant takes: 251 secs Time taken by time; roughly half user, half system. ]As this script won't run often, that not too big a deal.
I suspect that you asked a wrong question. If the original question were "I have a git managed project whose tip has about 8000 paths in it, and I have 9000 paths that may or may not belong to the tip. How do I find the paths that are not part of the tip" (or "How do I find the ones that are part of the tip"), the answer would have been quite different.
git ls-files | sort >known
(or "git ls-tree --name-only -r HEAD | sort >known")
$list_your_paths_one_per_line | sort | comm known -
would compare the set of paths in the index (or HEAD) and the set of the paths you are interested in, and give you the list of ones that are only present in one side, the other, or both. Say "comm -13" instead of "comm" if you are only interested in untracked ones, and say "comm -12" if you are only interested in tracked ones. Since you are working inside Perl, probably you would not pipe to sort/comm but do the comm part yourself, I would imagine.
It is as if you asked "how do I add one to a given number", without saying that "I have 9000 paths and I want to repeatedly add one for each path I have", people taught you the answer to your original question, and you ended up looping "n := n + 1" 9000 times. If you told them the real problem you were trying to solve upfront, they would have taught you how to multiply instead ;-).