Git - Tree Objects

Tree 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:09 .
drwxr-xr-x  24 allarm  staff  768 Nov 10 18:09 ..

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) 680a361] 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
680a3617f703fd2774c56bc7790d4134f7bdd649 : commit
ed557584085e6ecbc79a01adb943fdba7a05f5f0 : tree
c12fddafdaba3b7b1281f50c9e482ed673f1767c : blob
27c68820a14130e9866371d6cb746ca5cbd39864 : tree
11dd309e25ce41320f8b55ef3be7d50246a8b4a6 : blob
cd0875583aabe89ee197ea133980a9085d08e497 : blob

Now let's take a look at the contents of the tree 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 == "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

There are 2 tree objects in the repo. One is pointing to the file in the awesome_dir and another points to the files in the root dir and to the first tree object.

If we commited the changes the commit object would point to the second tree object.