Skip to content

Instantly share code, notes, and snippets.

@paulv
Created March 11, 2009 22:19
Show Gist options
  • Save paulv/77786 to your computer and use it in GitHub Desktop.
Save paulv/77786 to your computer and use it in GitHub Desktop.
# There is precious little documentation on how to use Grit to add files
# to a repository. Basically as far as I understand it the steps are:
#
# (1) Create a repository via 'git init' on the command line
# (2) Create a Grit::Repo object
# (3) Write one or more files to the directory the repository lives in
# (4) Create Grit::Blob objects that represent those files
# (5) Add the files to the index
# (6) Commit the index
require 'grit'
def add_file_to_repo(repo, file)
filename = File.basename file
repo_location = repo.path.gsub(/\.git$/, '')
File.open("#{repo_location}/#{filename}", "w+") do |f|
f << File.read(file)
end
blob = Grit::Blob.create(repo, {
:name => filename,
:data => ""
})
repo.add(blob.name)
repo.commit_index("added #{filename}")
end
REPO_DIR = "/tmp/repo"
repository = Grit::Repo.new(REPO_DIR)
add_file_to_repo(repository, "/etc/passwd")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment