Chris Nelson
Chris Nelson

Reputation: 3707

Does git have a "relative" treeish?

I'm trying to verify each commit in a sequence, moving from the first to the current tip. It's not really a git bisect, I don't expect any problems and the tip works. But I want to make sure each commit is self-contained and correct.

I can use HEAD^ to go back one from the tip but is there a "one forward from where I am" treeish? That is, if I have

o aaabbbccc (tip)
|
o abcdefabc
|
o fedcbafed
|
o abcdabcde
|
o deadbeefe (root)

I want to do:

git checkout deadbeefe
build and test
git checkout current+1
<up two, return>
<up two, return>

But I can't figure out the treeish for "current+1".

Upvotes: 2

Views: 395

Answers (1)

Richard Hansen
Richard Hansen

Reputation: 54153

Git does not store links from parents to children; only links from children to parents. To find a commit's children, you have to start at the childmost commits in your repository (branch tips, tags, HEAD, etc.) and walk up the chain of parents until you either reach a root node or find the commit in question.

If your commit DAG is linear and tip is a reference to the childmost commit, you can do something like the following to find the child commit of revision deadbeefe:

git rev-list deadbeefe..tip | tail -n 1

This causes Git to start walking from tip until it reaches deadbeefe or the root and print out all the commits it comes across. The output is then piped to tail to select the last visited commit, which will be the child of deadbeefe if the commit history is linear.

HEAD refers to the currently checked-out commit, so if you need the child of the current commit instead of deadbeefe, use HEAD. Commands that require a commit default to HEAD if unspecified, so you can do the following:

git rev-list ..tip | tail -n 1

Again, this only works if the DAG is linear. If the commit history is not linear, you can use the --ancestry-path argument to rev-list.

Doing the above each time you want to move to the next commit would be O(n^2), but Git is so fast that it usually doesn't matter in practice. If you need it to be O(n), I'd do the following:

git rev-list --reverse tip | while IFS= read -r rev; do
    git checkout "${rev}" || handle_checkout_error_here
    # build and test here
done

And @knittl is correct—the term you want here is commit, not treeish. Some terminology:

  • A 'treeish' is something that can act like a file system tree (e.g., the index, your working directory, a subdirectory in your working directory, a commit, a subdirectory inside a commit, etc.).
  • 'Revision' is less well defined than commit, but it usually means the things that identify a commit (commits themselves, annotated tags that point to a commit, and references that refer to a commit).
  • A 'reference' is a pointer to a Git object or another reference (in which case it's called a 'symbolic reference'). A reference usually points to a commit or annotated tag object, but it can point to anything. People usually say reference when they want to generically refer to HEAD, tags, and/or branches.

Upvotes: 4

Related Questions