Question

If I have a bare repository and I want to add, edit, and delete files from it how can I do this using LibGit2Sharp?

I see that each Commit has a Tree object that contains TreeEntry objects. Is it possible to add, edit and delete these TreeEntry(s)? If so, will the Index reflect that these changes are pending and need to be staged?

Thanks!

Was it helpful?

Solution

If I have a bare repository and I want to add, edit, and delete files from it how can I do this using LibGit2Sharp?

A bare repository is a "server side" repository from which you fetch/pull changes and to which you push your updates.

It doesn't have a Working Directory nor an Index. Thus, one can not directly add, edit or delete files. You're supposed to prepare your commits in a "standard" repository (with a working folder), then push your commits toward a "bare" central repository.

Note: some git plumbing commands and libgit2 low level functions allow you to work around those limits. However, it's a somewhat advanced toolkit and you've got to be ready to dive into the Git Internals in order to find your way through :) LibGit2Sharp doesn't expose those functionalities.

I see that each Commit has a Tree object that contains TreeEntry objects. Is it possible to add, edit and delete these TreeEntry(s)? If so, will the Index reflect that these changes are pending and need to be staged?

A Commit, a Tree or a Blob are read-only git objects. You can not "add" a TreeEntry to a Tree, nor modify an existing Commit.

A git commit modifier (--amend) can let under the impression that the commit has been updated in-place. This is not the case: a whole new commit is recreated as a child of the parent of the old commit. Then the HEAD is moved from the old commit to the new one.

Update

Release v0.9.0 of LibGit2Sharp exposes some low-level operations which now allow one to directly interact with the object database of a repository, even a bare one.

Leveraging those features, one can create Blobs, Trees and Commits without either involving an Index nor a Working directory.

Thus, it's now possible to load an existing Tree, update it in-memory then persist it back in the object database. Of course, this will create a new Tree and the former one will be kept unmodified.

Some unit tests demonstrating those features can be found in the ObjectDatabaseFixture.cs file.

OTHER TIPS

Please google "git for computer scientists" to understand the DAG (directed acyclic graph). Changing a commit, tree or object will actually make a new one. The idea that you edit any of these things is the wrong thinking. You can only create new ones if content is changed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top