Re: how to do these 2 one-liners ?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




On Mon, 19 Mar 2007, Christian MICHON wrote:
> 
> 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
-
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]