def write_tree(tree = nil, now_tree = nil)
      tree = self.tree if !tree
      tree_contents = {}

      # fill in original tree
      now_tree = read_tree(now_tree) if(now_tree && now_tree.is_a?(String))
      now_tree.contents.each do |obj|
        sha = [obj.id].pack("H*")
        k = obj.name
        k += '/' if (obj.class == Grit::Tree)
        tmode = obj.mode.to_i.to_s  ## remove zero-padding
        tree_contents[k] = "%s %s\0%s" % [tmode, obj.name, sha]
      end if now_tree

      # overwrite with new tree contents
      tree.each do |k, v|
        case v
          when Array
            sha, mode = v
            if sha.size == 40        # must be a sha
              sha = [sha].pack("H*")
              mode = mode.to_i.to_s  # leading 0s not allowed
              k = k.split('/').last  # slashes not allowed
              str = "%s %s\0%s" % [mode, k, sha]
              tree_contents[k] = str
            end
          when String
            sha = write_blob(v)
            sha = [sha].pack("H*")
            str = "%s %s\0%s" % ['100644', k, sha]
            tree_contents[k] = str
          when Hash
            ctree = now_tree/k if now_tree
            sha = write_tree(v, ctree)
            sha = [sha].pack("H*")
            str = "%s %s\0%s" % ['40000', k, sha]
            tree_contents[k + '/'] = str
          when false
            tree_contents.delete(k)
        end
      end

      tr = tree_contents.sort.map { |k, v| v }.join('')
      @last_tree_size = tr.size
      self.repo.git.put_raw_object(tr, 'tree')
    end
def write_tree(tree = nil, now_tree = nil)
      tree = self.tree if !tree
      tree_contents = {}

      # fill in original tree
      now_tree = read_tree(now_tree) if(now_tree && now_tree.is_a?(String))
      now_tree.contents.each do |obj|
        sha = [obj.id].pack("H*")
        k = obj.name
        k += '/' if (obj.class == Grit::Tree)
        tmode = obj.mode.to_i.to_s  ## remove zero-padding
        tree_contents[k] = "%s %s\0%s" % [tmode, obj.name, sha]
      end if now_tree

      # overwrite with new tree contents
      tree.each do |k, v|
        case v
          when Array
            sha, mode = v
            if sha.size == 40        # must be a sha
              sha = [sha].pack("H*")
              mode = mode.to_i.to_s  # leading 0s not allowed
              k = k.split('/').last  # slashes not allowed
              str = "%s %s\0%s" % [mode, k, sha]
              tree_contents[k] = str
            end
          when String
            sha = write_blob(v)
            sha = [sha].pack("H*")
            str = "%s %s\0%s" % ['100644', k, sha]
            tree_contents[k] = str
          when Hash
            ctree = now_tree/k if now_tree
            sha = write_tree(v, ctree)
            sha = [sha].pack("H*")
            str = "%s %s\0%s" % ['40000', k, sha]
            tree_contents[k + '/'] = str
          when false
            tree_contents.delete(k)
        end
      end

      tr = tree_contents.sort.map { |k, v| v }.join('')
      @last_tree_size = tr.size
      self.repo.git.put_raw_object(tr, 'tree')
    end
Recursively write a tree to the index.

 tree -     The Hash tree map:
            key - The String directory or filename.
            val - The Hash submap or the String contents of the file.
 now_tree - The Grit::Tree representing the a previous tree upon which
            this tree will be based (default: nil).

 Returns the String SHA1 String of the tree.