Example code:
idx = repo.index
idx.read_tree('master')
idx.add('db object 1', "Metadata 1")
idx.add('db object 2', "Metadata 2")
idx.commit('Added db-only objects')
puts "TREE CONTENTS AFTER INDEX-ADD:"
puts repo.tree.inspect
puts repo.git.ls_tree({}, 'master', [])
puts "."
name = 'fs_object'
File.open(name, 'w') { |f| f.puts 'User data' }
repo.add(name)
repo.commit_index('Added fs object')
puts "TREE CONTENTS AFTER REPO-ADD:"
puts repo.tree.inspect
puts repo.git.ls_tree({}, 'master', [])
puts "."
This produces the following output:
TREE CONTENTS AFTER INDEX-ADD:
<Grit::Tree "master">
100644 blob 42c2824cd367d91de1fca29376857633241b8185 db object 1
100644 blob 5a741e1457a6f55702d9258231d317144108fddb db object 2
.
TREE CONTENTS AFTER REPO-ADD:
<Grit::Tree "master">
100644 blob 48de7d82a09381130aa193b9fb47dc7f67fd8676 fs_object
.
It seems as if 'git-update --remove' is getting called somewhere in grit, since the default behaviour for git (using the commands git-hash-object -w, git-update-index --add --cacheinfo, and git-add) is to ignore (not delete!) the objects in the database that are not on-disk.
In addition to all of this, there is the unexpected (but much less disturbing) behaviour of Repo#index. One would expect this to return the staging Index, or at least the same Index each time. This may be due to the fact that grit calls out to git-add for stuff like adding files instead of using its own index... but it makes the index appear to be an afterthought in grit.
UPDATE: I was mistaken about this working using the git command line tools; the second commit is what causes the stuff in the object database (and not the fs) to be removed. This happens whether I use grit or git.
Here is a shell script to test:
#!/bin/sh
echo Building test repo
rm -rf 'test'
mkdir 'test'
cd 'test'
git init .
echo Writing metadata
echo 'meta 1' | git hash-object -w --stdin
echo 'meta 2' | git hash-object -w --stdin
git update-index --add --cacheinfo 100644 41d1b9dbb485634b60349ac7fafdb2ebec03e85b db_obj_1
git update-index --add --cacheinfo 100644 afaf77a436aa213bd1031c2018329e7b9878bc86 db_obj_2
git write-tree
echo 'added metadata' | git commit-tree 9841ec9887bc2577aaad347d939312e8134f7a1b
echo writing FS data
echo 'data 1' > fs_object
git add fs_object
#git commit -m 'added fs object' .
git ls-files --cached --stage