开发者

How to view git objects and index without using git

开发者 https://www.devze.com 2022-12-08 02:41 出处:网络
Using the OS X terminal, How an you view the contents开发者_Go百科 of these files as plain text?alias deflate=\"perl -MCompress::Zlib -e \'undef $/; print uncompress(<>)\'\"

Using the OS X terminal,

How an you view the contents开发者_Go百科 of these files as plain text?


alias deflate="perl -MCompress::Zlib -e 'undef $/; print uncompress(<>)'"

deflate .git/objects/4b/3083256dedabd68e839d7717aa785424119990 | sha1sum
4b3083256dedabd68e839d7717aa785424119990  -


Look at “Object storage format” in Git User Manual.

It is raw compressed data using zlib. One can use zpipe from the 'zlib1g-dev' package. Compile the example with gcc -o zpipe zpipe.c -lz. It is not a standard .gz or something.

./zpipe -d < .git/objects/02/43019ddb4d94114e5a8580eec01baeea195133

prints the content of the blob (header+data)

If you want to check the SHA-1, you have to put the uncompressed blob in a file (myblob) and do

sha1sum myblob


I'm going to interpret your question in a different way. If you want to understand what the objects files ARE, you can use git to view them directly, without navigating the history log or using git checkout, diff etc. For instance:

For the file .git/objects/04/a42e9a7282340ef0256eaa6d59254227b8b141

Run the command

git show 04a42e

which combines the 04 from /04/ and the first four characters of the remaining number a42e.

> git show 04a42e
commit 04f7db976fa54c72fcc6350dd7d0aa06cb249df6
Author: Alex Brown <alex@XXXX.XXX>
Date:   Fri Jan 8 11:02:21 2010 +0000
a text file

diff --git a/1.txt b/1.txt
new file mode 100644
index 0000000..04a42e9
--- /dev/null
+++ b/1.txt
@@ -0,0 +1,3 @@
+Woo
+# boo
+# choo

This one is a commit, other objects may be file blobs, trees, etc.


If you want to view plain text form of git objects (commits and/or blobs i.e. file contents) without using git, it would not be easy, especially if repository is packed. Can't you install git locally, in your home directory (or its equivalent on MacOS X)?

The format for loose objects, stored as files in .git/objects/ fan-out directory, e.g. .git/objects/02/43019ddb4d94114e5a8580eec01baeea195133 (the fan-out directory and file name form SHA-1 identifier of object), is described e.g. in Chapter 9.2 "Git Objects" of "Pro Git" book (available on-line for free) and Chapter 7.1 "How Git Stores Objects" of "Git Community Book".

The pack format, where set of objects is stored in single file in .git/objects/pack/, e.g. .git/objects/pack/pack-1db7aa96d95149a4dd341490a3594181a24415ee.pack, is described in Documentation/technical/pack-format.txt and in Chapter 7.5 "The Packfile" of "Git Community Book" (and mentioned in Chapter 9.4 "Packfiles" of "Pro Git")


If you want to find latest commit, take a look first at .git/HEAD file to find current branch. It would contain something like the following:

ref: refs/heads/master

(if it contains SHA-1, you can take it as id of last commit, and skip a step). Then check e.g. .git/refs/heads/master to find where the branch points to. It would contain SHA-1 of a commit, e.g.:

dbc1b1f71052c084a84b5c395e1cb4b5ae526fcb

Last (most recent) commit would be probably in loose format; in this example it would be in .git/objects/db/c1b1f71052c084a84b5c395e1cb4b5ae526fcb file.


The index is stored under .git/index.

It is a binary uncompressed format specified at: https://github.com/git/git/blob/master/Documentation/technical/index-format.txt, so the only way to read it is with a tool like hd.

The index file contains a list of files with their metadata, including inode, permissions, and modification time. It also contains the SHA-1 of the content, which is stored as an object, which means that when you do git add it may create new objects.

I encourage you to create a simple test repo like git init init && cd init && echo a > a && git add a, and then hd .git/index to verify the format field by field.

The following question focuses more on the index: What does the git index contain EXACTLY?

0

精彩评论

暂无评论...
验证码 换一张
取 消