threads / discuss / 7320

how to do these 2 one-liners ?

Subject: how to do these 2 one-liners ?

## tl;dr

4 messages between Mar 19, 2007 and Mar 19, 2007.

replies: 3people: 3as markdown or json

Christian MICHON· Mar 19, 2007, 22:25 UTC · lore
Hi list,

in order to code a gvim plugin for git (I started something), I now miss 2 one-liners, and I hope experts around this list will find it easy to answer (for the sake of this plugin)

================================================
1) how do I check the status of a single file ? ( the file is already
added in the index)

I usually use git-status here, but for single files that need update, there should be a faster way.

================================================
2) how do I find in historical reverse order all the commits a
certain file belongs to since the origin ?

I usually do: git-log <file> | grep ^commit I would like to avoid piping here...

I may need more than these 2 one-liners, if they're doable. I'll post something soon :) Thanks in advance!

-- 
Christian
Junio C Hamano· Mar 19, 2007, 22:44 UTC · re: Christian MICHON · lore

Re: how to do these 2 one-liners ?

"Christian MICHON" <christian.michon@gmail.com> writes:
Show 6 quoted lines
> ================================================
> 1) how do I check the status of a single file ? ( the file is already
> added in the index)
>
> I usually use git-status here, but for single files that need update,
> there should be a faster way.

"git-diff-files $PATH" shows if it is different relative to the index, "git-diff-index HEAD $PATH" shows if it is different from the HEAD, and "git-diff-index --cached $PATH" shows if it is different between index and HEAD. git-status uses the first one and the third one AFAIR. You say "that need update", so probably you are interested in seeing the first one only without the third one.

If you have Git 1.5.1, then:
	if git diff-files --quiet $PATH
        then
        	echo Up to date
	else
        	echo Need update
	fi                
Another more portable way is:
	if test -z "$(git diff-files --raw $PATH)"
        then
        	echo Up to date
	else
        	echo Need update
	fi                
Show 6 quoted lines
> ================================================
> 2) how do I find in historical reverse order all the commits a
> certain file belongs to since the origin ?
>
> I usually do: git-log <file> | grep ^commit
> I would like to avoid piping here...
git-rev-list -m HEAD -- "$PATH"
Christian MICHON· Mar 19, 2007, 22:59 UTC · re: Junio C Hamano · lore

Re: how to do these 2 one-liners ?

On 3/19/07, Junio C Hamano <junkio@cox.net> send 2 nice one-liners...

Thanks Junio :)

I'll use these 2 tomorrow. I wasn't that far in my last experiments for the 2nd one...

-- 
Christian
Linus Torvalds· Mar 19, 2007, 23:09 UTC · re: Christian MICHON · lore

Re: how to do these 2 one-liners ?

On Mon, 19 Mar 2007, Christian MICHON wrote:
Show 8 quoted lines
> 
> in order to code a gvim plugin for git (I started something), I now miss
> 2 one-liners, and I hope experts around this list will find it easy
> to answer (for the sake of this plugin)
> 
> ================================================
> 1) how do I check the status of a single file ? ( the file is already
> added in the index)
It really depends on what you want to do.

If you want to know *what* the index contains right there and then, do for example:

	git ls-files --stage -- "$filename"

and it will tell you the index contents (it might be several lines: if the "filename" entry is a directory it will list *all* files under that directory, but even if it's a single file it could give you all the unmerged stages for that file).

On the other hand, often you want to know what the status of a file is not in "absolute" terms, but relative to the index or to the last commit. If so, you can use something like

	git diff-files --name-status -- "$filename"

which just compares the index with the current working tree. And if you want to check against the last commit (ie HEAD), use

	git diff-index --name-status HEAD -- "$filename"

which can also be used with "--cached" to see the difference between the HEAD and the index. That's how "git status" used to do it when it was a shell script (now it's done with a built-in).

In fact, you could look at the old git-commit.sh script before it was turned into a built-in with

	git show v1.4.0:git-commit.sh
and look at the "run_status" shell function.
> I usually use git-status here, but for single files that need update,
> there should be a faster way.

Indeed. HOWEVER! Note that when you ask for the status of a single file, that obviously means that there is no "rename detection" going on, so if you want to see the "renamed from Xyz", you need to do the global analysis that "git-status" does, and then fetch the filename info from there.

(Alternatively, you can choose to always do the single-file case first, and then only *if* it's a newly added file you could ask "was it renamed from anything else").

> 2) how do I find in historical reverse order all the commits a
> certain file belongs to since the origin ?
That's quite an expensive operation:
	git rev-list --reverse HEAD -- "$filename"
Or did you by "historical" mean "newest first"? If so, just do
	git rev-list HEAD -- "$filename"
(that's the order that git considers "natural").
> I usually do: git-log <file> | grep ^commit
> I would like to avoid piping here...

Well, "git log" is really just rev-list with fancy options to make it show more than just the commit name.

Of course, you *could* do
	git log --pretty=oneline -- "$filename"

which gets you pretty close. But if you literally just want the commit SHA1, "git rev-list" is what you're really asking for.

			Linus

← back to recent threads