git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Linus Torvalds <torvalds@linux-foundation.org>
To: Christian MICHON <christian.michon@gmail.com>
Cc: git <git@vger.kernel.org>
Subject: Re: how to do these 2 one-liners ?
Date: Mon, 19 Mar 2007 16:09:51 -0700 (PDT)	[thread overview]
Message-ID: <Pine.LNX.4.64.0703191549100.6730@woody.linux-foundation.org> (raw)
In-Reply-To: <46d6db660703191525w613b02e1nae0f30efaca3b269@mail.gmail.com>



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

      parent reply	other threads:[~2007-03-19 23:10 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-03-19 22:25 how to do these 2 one-liners ? Christian MICHON
2007-03-19 22:44 ` Junio C Hamano
2007-03-19 22:59   ` Christian MICHON
2007-03-19 23:09 ` Linus Torvalds [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Pine.LNX.4.64.0703191549100.6730@woody.linux-foundation.org \
    --to=torvalds@linux-foundation.org \
    --cc=christian.michon@gmail.com \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).