Dmitry Shkolnik
Dmitry Shkolnik

Reputation: 323

Copy Git index into a temp folder

Suppose I have a utility some-check {baseline_folder} {working_folder} which does some comparison on sets of files (not the individual files).

I'd like to use this utility in two slightly different use cases:

The first use case is pretty straightforward, since getting the baseline (i.e. HEAD) into a temp folder is not a big deal.

The real question is how to get index into some folder. The only way I see is something like:

  1. create a stash

  2. apply it back immediately

  3. use git-archive to create a zip by the stash ref

  4. unzip it to a temp folder

  5. drop the stash

Is there any less verbose way to do it?

UPD: Long story short: the problem is how to get a folder which has HEAD+stage, but without modified/untracked.

Upvotes: 2

Views: 710

Answers (2)

torek
torek

Reputation: 488183

For validating what's in the index, you can extract everything that is in it to a temporary work-tree this way:

tmpdir=$(mktemp -d)                # or your other chosen method
trap "rm -rf $tmpdir" 0 1 2 3 15   # do something different in other languages

GIT_WORK_TREE=$tmpdir git checkout-index --all

Be sure that the temporary directory exists and is empty (the mktemp -d here does that), and be sure to clean up when done (the trap here does that).

Note that attribute rules will be applied to the git checkout-index step. Using git archive, you gain more control with export-ignore and export-subst. If you want to use that method, you can turn the current index into a tree object using git write-tree, and then use git archive on that tree object.

Note also that the git write-tree method raises an error with an unmerged index, while the git checkout-index method simply omits the unmerged files from the target work-tree.

Upvotes: 5

Obsidian
Obsidian

Reputation: 3897

Is there any less verbose way to do it?

There is better :

git worktree

This allows you to turn any directory into a annex of a git repository, enabling you to check out two different revision simultaneously. And you can add as many annexes as you need, then discard them when no longer required. It's very useful, for example, to work on two versions of a same library (for instance, its last stable version and the tip of the development one).

There was a similar question here. This should match your needs.

Upvotes: 0

Related Questions