Writing a tree object to the database

Now, we have manually created objects in the Git database, but as nothing point to these objects, we have to remember them by their SHA-1 identifier. Furthermore, only the content of the files is stored in the database, so we learn to create a tree object that will refer to the blobs created.

Getting ready

We'll use the same repository of the last examples with the objects we created in the database.

How to do it...

We'll start by adding the first version of mytest.txt as follows:

$ git update-index --add --cacheinfo 100644 
926e8ffd3258ed6edd1e254438f02fd24e417acc mytest.txt

Now we can write the content of the staging area to the database:

$ git write-tree
4c4493f8029d491d280695e263e24772ab6962ce

We can update and write a tree for the second version of mytest.txt as follows:

$ git update-index --cacheinfo 100644 
6b3da706d14c3820597ec7109f163bc144dcbb22 mytest.txt
$ git write-tree
2b9697438318f3a62a5e85d14a3b52d69b962907

Finally, we can use the object we created from stdin and we'll put it in a subdirectory, sub, with the name other.txt:

$ git update-index --add --cacheinfo 100644 
70bacd9f51c26d602f474bbdc9f60644aa449e97 sub/other.txt
$ git write-tree
9387b1a7619c6d899d83fe8d1437864bcd88736c

How it works…

The update-index command updates the staging area with the files or content specified. The --add command tells Git to add a new file to the index, and the --cacheinfo switch is used to tell the command to use an existing object from the database. The 100644 command is used to create a normal file. Other types are 100755 (the executable file) and 040000 (a subdirectory), and so on. We could also have used a file directly for the update-index command, and then just provided the filename and omitted --cacheinfo, filetype, and identifier.

We can check the objects created with the cat-file command and verify their types, as shown in the following snippet:

$ git cat-file -p 4c4493f8029d491d280695e263e24772ab6962ce
100644 blob 926e8ffd3258ed6edd1e254438f02fd24e417acc  mytest.txt
$ git cat-file -t 4c4493f8029d491d280695e263e24772ab6962ce
tree
$ git cat-file -p 2b9697438318f3a62a5e85d14a3b52d69b962907
100644 blob 6b3da706d14c3820597ec7109f163bc144dcbb22  mytest.txt
$ git cat-file -t 2b9697438318f3a62a5e85d14a3b52d69b962907
tree
$ git cat-file -p 9387b1a7619c6d899d83fe8d1437864bcd88736c
100644 blob 6b3da706d14c3820597ec7109f163bc144dcbb22  mytest.txt
040000 tree 4cbaf806ece93e483f2515a7cc7326e194844797  sub
$ git cat-file -t 9387b1a7619c6d899d83fe8d1437864bcd88736c
tree
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset