Git: Editing a file in a bare repo using Git plumbing commands

981 views Asked by At

I'm trying to create a basic GIT web UI for GIT bare repos which allows edit (only) of existing files.

For example, considering these two cases:

  • /dir-a/dir-b/dir-c/a-file.txt
  • /a-file.txt

How could I use the Git low level plumbing commands to save the edits? Also, I'm using Grit, not sure if it provides a shortcut to do this.

1

There are 1 answers

4
Michał Politowski On

I do not know about Grit, but with Git itself you could do it like this, using the index.

Let's assume that the new content to be stored in dir-a/dir-b/dir-c/a-file.txt is available in /tmp/new-content.txt.

git read-tree HEAD
newhash=$(git hash-object -w --path=dir-a/dir-b/dir-c/a-file.txt /tmp/new-content.txt)
git update-index --cacheinfo 0644 $newhash dir-a/dir-b/dir-c/a-file.txt
newtree=$(git write-tree)
newcommit=$(git commit-tree $newtree -p HEAD -m 'file editedin a bare repository')
git update-ref HEAD $newcommit