Page 30 - MSDN Magazine, June 2017
P. 30

Figure 5 Exploring Git Objects Using a Git Command-Line Interface
commit ID isn’t displayed for the first commit in the repo). The SHA-1 for each commit object is computed from all of the values contained in these commit-object properties, virtually guaranteeing that each commit object will have a unique commit ID.
Tree and Blob Objects Notice that although the commit object contains information about the commit, it doesn’t contain any files or folders. Instead, it has a Tree ID (also an SHA-1 value) that points to a Git tree object. Tree objects are stored in the .git\\\\objects folder along with all other Git objects.
Commit Object Now’s the time to launch a Git CLI. You can use whichever tool you prefer (Git Bash, PowerShell or command win- dow)—I’ll use PowerShell. To begin, navigate to the solution root’s .git\\\\objects folder, then list its contents (Figure 5, Marker 1). You’ll see that it contains a number of folders named using two-character hex values. To avoid exceeding the number of files permitted in a folder by the OS, Git removes the first two characters from each 40-byte SHA-1 value in order to create a folder name, then it uses the remaining 38 characters as the file name for the object to store. To illustrate, my project’s first commit has ID a759f283, so that object will appear in a folder called a7 (the first two characters of the ID). As expected, when I open that folder, I see a file named 59f283. Remember that all of the files stored in these hex-named folders are Git objects. To save space, Git zlib-compresses the files in the object store. Because this kind of compression produces binary files, you won’t be able to view these files using a text editor. Instead, you’ll need to invoke Git commands that can properly extract Git-object data and present it using a format you can digest.
I already know that file 59f283 contains a commit object because it’s a commit ID. But sometimes you’ll see a file in the objects folder without knowing what it is. Git provides the cat-file plumbing command to report the type of an object, as well as its contents (Marker 3). To obtain the type, specify the -t (type) option when invoking the command, along with the few unique characters of the Git object’s file name:
git cat-file -t a759f2
On my system, this reports the value “commit”—indicating that the file starting with a759f2 contains a commit object. Specifying only the first five characters of the SHA-1 hash value is usually enough, but you can provide as many as you want (don’t forget to add the two charac- ters from the folder name). When you issue the same command with the -p (pretty print) option, Git extracts information from the commit object and presents it in a format that’s human-readable (Marker 4).
A commit object is composed of the following properties: Parent Commit ID, Tree ID, Author Name, Author Email Address, Author Commit Timestamp, Committer Name, Committer Email Address, Committer Commit Timestamp and Commit Message (the parent
26 msdn magazine
Figure 6 depicts the root tree object that’s part of each commit object. The root tree object maps, in turn, to blob objects (covered next) and other tree objects, as needed. Because my project’s second commit (Commit ID bfeb09) includes files, as well as folders (see the earlier Figure 4), I’ll use it to illustrate how the tree object works. Figure 7, Marker 1 shows the cat-file -p bfeb09 output. This time, notice that it includes a parent property, which correctly references the SHA-1 value for the first commit object. (Remember that it’s a commit object’s parent refer- ence that enables Git to construct and maintain its DAG of commits.) The root tree object maps, in turn, to blob objects (zlib-compressed
files) and other tree objects, as needed.
Tag
Commit Object
root tree object
Tree
Tag
Tree Tree
Blob Blob
Blob
Blob Blob
Blob
Blob Blob
Blob
Tag
Tree
Blob Blob
Blob
Figure 6 Visualization of Git Objects That Express a Commit DevOps







































































   28   29   30   31   32