git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Is a given file known to git?
@ 2008-03-11  7:15 Christoph Duelli
  2008-03-11  7:24 ` Shawn O. Pearce
  2008-03-11  7:38 ` Johannes Sixt
  0 siblings, 2 replies; 7+ messages in thread
From: Christoph Duelli @ 2008-03-11  7:15 UTC (permalink / raw)
  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!
-- 
Christoph Duelli

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Is a given file known to git?
  2008-03-11  7:15 Is a given file known to git? Christoph Duelli
@ 2008-03-11  7:24 ` Shawn O. Pearce
  2008-03-11  7:38 ` Johannes Sixt
  1 sibling, 0 replies; 7+ messages in thread
From: Shawn O. Pearce @ 2008-03-11  7:24 UTC (permalink / raw)
  To: Christoph Duelli; +Cc: git

Christoph Duelli <duelli@melosgmbh.de> wrote:
> 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.

-- 
Shawn.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Is a given file known to git?
  2008-03-11  7:15 Is a given file known to git? Christoph Duelli
  2008-03-11  7:24 ` Shawn O. Pearce
@ 2008-03-11  7:38 ` Johannes Sixt
  2008-03-11  9:27   ` Christoph Duelli
  1 sibling, 1 reply; 7+ messages in thread
From: Johannes Sixt @ 2008-03-11  7:38 UTC (permalink / raw)
  To: Christoph Duelli; +Cc: git

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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Is a given file known to git?
  2008-03-11  7:38 ` Johannes Sixt
@ 2008-03-11  9:27   ` Christoph Duelli
  2008-03-11  9:36     ` Johannes Sixt
  2008-03-11  9:45     ` Junio C Hamano
  0 siblings, 2 replies; 7+ messages in thread
From: Christoph Duelli @ 2008-03-11  9:27 UTC (permalink / raw)
  To: Johannes Sixt, spearce; +Cc: git

Johannes Sixt schrieb:
> 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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Is a given file known to git?
  2008-03-11  9:27   ` Christoph Duelli
@ 2008-03-11  9:36     ` Johannes Sixt
  2008-03-11  9:41       ` Christoph Duelli
  2008-03-11  9:45     ` Junio C Hamano
  1 sibling, 1 reply; 7+ messages in thread
From: Johannes Sixt @ 2008-03-11  9:36 UTC (permalink / raw)
  To: Christoph Duelli; +Cc: spearce, git

Christoph Duelli schrieb:
> 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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Is a given file known to git?
  2008-03-11  9:36     ` Johannes Sixt
@ 2008-03-11  9:41       ` Christoph Duelli
  0 siblings, 0 replies; 7+ messages in thread
From: Christoph Duelli @ 2008-03-11  9:41 UTC (permalink / raw)
  To: Johannes Sixt; +Cc: spearce, git

Johannes Sixt schrieb:
> Christoph Duelli schrieb:
>> 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
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.

-- 
Christoph Duelli

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Is a given file known to git?
  2008-03-11  9:27   ` Christoph Duelli
  2008-03-11  9:36     ` Johannes Sixt
@ 2008-03-11  9:45     ` Junio C Hamano
  1 sibling, 0 replies; 7+ messages in thread
From: Junio C Hamano @ 2008-03-11  9:45 UTC (permalink / raw)
  To: Christoph Duelli; +Cc: Johannes Sixt, spearce, git

Christoph Duelli <duelli@melosgmbh.de> writes:

> 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.

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 ;-).

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2008-03-11  9:46 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-03-11  7:15 Is a given file known to git? Christoph Duelli
2008-03-11  7:24 ` Shawn O. Pearce
2008-03-11  7:38 ` Johannes Sixt
2008-03-11  9:27   ` Christoph Duelli
2008-03-11  9:36     ` Johannes Sixt
2008-03-11  9:41       ` Christoph Duelli
2008-03-11  9:45     ` Junio C Hamano

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).