git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: Is a given file known to git?

From
CDChristoph Duelli <duelli@melosgmbh.de>
Date
Mar 11, 2008, 09:27 UTC
Message-ID
<47D65074.4000505@melosgmbh.de>
In-Reply-To
<47D636FC.2060203@viscovery.net>
Johannes Sixt schrieb:
Show 21 quoted lines
> Christoph Duelli schrieb:
>> 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

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
-- 
Christoph Duelli
Previous: Johannes SixtNext: Johannes Sixt
Message 4 of 7 in “Is a given file known to git?”
  1. Christoph DuelliMar 11, 2008
  2. Shawn O. PearceMar 11, 2008
  3. Johannes SixtMar 11, 2008
  4. Christoph DuelliMar 11, 2008
  5. Johannes SixtMar 11, 2008
  6. Christoph DuelliMar 11, 2008
  7. Junio C HamanoMar 11, 2008

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.