2

I need to commit the current state of the workdir (i.e. the files and their content as stored on the filesystem) into a non-checked out branch. It is important that this is done in a stateless way because the user may have local modifications or may have staged some files. That means that the files should not be staged through the index before being committed. (The rest of the application already operates in a stateless way.)

One straightforward way to achieve this would be to build that Tree recursively while descending the directory. But I assume that such a common task is very likely already implemented in Rugged/libgit2 (for example, Index#add_all does something very similar).

Does Rugged provide a way to create a Tree object from the current workdir (respecting .gitignore & Co.) without changing the index?

gioele
  • 9,748
  • 5
  • 55
  • 80
  • "current state" means the content of the worktree (files on disk for that dir) ? or the content of the index (staged content for that dir) ? – LeGEC Mar 13 '23 at 05:16
  • "current state" as in "files as stored in the filesystem". I updated the question. – gioele Mar 13 '23 at 09:03

1 Answers1

0

There is a way to have a staging area that doesn't affect your current index: use a separate GIT_INDEX_FILE.

It is part of git cli and my guess is it is supported by libgit2 (I will let you confirm this point though).

You can then use the two plumbing commands write-tree and commit-tree to create a commit that isn't a descendant of HEAD.

Here is an example using git cli:

export GIT_INDEX_FILE .git/my_temp_index

git add that/directory
tree=$(git write-tree)
git commit-tree $tree -p that/other/branch -m "my commit message"


unset GIT_INDEX_FILE
rm .git/my_temp_index
LeGEC
  • 46,477
  • 5
  • 57
  • 104