Git - Commit Objects

Commit objects

Lab | C-c on the property string first!

Clean it up | execute it first

case $(pwd) in
,*git_lab_1*)
    cd ..
    rm -rf git_lab_1
    mkdir git_lab_1
    cd git_lab_1
    pwd
    ls -al
    ;;
  ,*)
    echo "You're in a wrong directory pal: $(pwd)."
    ;;
esac
/Users/allarm/Tmp/git_lab_1
total 0
drwxr-xr-x   2 allarm  staff   64 Nov 10 18:13 .
drwxr-xr-x  24 allarm  staff  768 Nov 10 18:13 ..

Set it up

echo "Hello world!" > hello.txt
echo "Goodbye world :(" > goodbye.txt
mkdir awesome_dir
echo "I am awesome!" > awesome_dir/awesome.txt
git init
git config user.name "Andrey Che"
git config user.email "andrey@che.org"
git add .
git commit -m "First commit"
Initialized empty Git repository in /Users/allarm/Tmp/git_lab_1/.git/
[master (root-commit) a5190c2] First commit
 3 files changed, 3 insertions(+)
 create mode 100644 awesome_dir/awesome.txt
 create mode 100644 goodbye.txt
 create mode 100644 hello.txt

Let's roll

Let's get the file objects:

objects=$(find .git/objects -type f | awk -F"/" '{print $3$4}')
for o in $objects
do
  echo "$o : $(git cat-file -t $o)"
done
a5190c25b9e06511399c4340382238ae7e22a126 : commit
ed557584085e6ecbc79a01adb943fdba7a05f5f0 : tree
c12fddafdaba3b7b1281f50c9e482ed673f1767c : blob
27c68820a14130e9866371d6cb746ca5cbd39864 : tree
11dd309e25ce41320f8b55ef3be7d50246a8b4a6 : blob
cd0875583aabe89ee197ea133980a9085d08e497 : blob

Now let's take a loot at the contents of the commit objects.

objects=$(find .git/objects -type f | awk -F"/" '{print $3$4}')
for o in $objects
do
  type=$(git cat-file -t $o)
  if [ $type == "commit" ]
  then
    echo "[ $o : $type ]"
    echo
    git cat-file -p $o
    echo
  fi
done
[ a5190c25b9e06511399c4340382238ae7e22a126 : commit ]

tree 27c68820a14130e9866371d6cb746ca5cbd39864
author Andrey Che <andrey@che.org> 1605003225 +0800
committer Andrey Che <andrey@che.org> 1605003225 +0800

First commit

The tree object (27c6) points to the files in the root directory and to another tree object, that is referencing the file inside the awesome_dir:

objects=$(find .git/objects -type f | awk -F"/" '{print $3$4}')
for o in $objects
do
  type=$(git cat-file -t $o)
  if [ $type == "tree" ]
  then
    echo "[ $o : $type ]"
    echo
    git cat-file -p $o
    echo
  fi
done
[ ed557584085e6ecbc79a01adb943fdba7a05f5f0 : tree ]

100644 blob 11dd309e25ce41320f8b55ef3be7d50246a8b4a6	awesome.txt

[ 27c68820a14130e9866371d6cb746ca5cbd39864 : tree ]

040000 tree ed557584085e6ecbc79a01adb943fdba7a05f5f0	awesome_dir
100644 blob c12fddafdaba3b7b1281f50c9e482ed673f1767c	goodbye.txt
100644 blob cd0875583aabe89ee197ea133980a9085d08e497	hello.txt