Skip to content

Instantly share code, notes, and snippets.

@rendon
Created February 23, 2016 22:59
Show Gist options
  • Save rendon/e0f1338258eb7b3f172c to your computer and use it in GitHub Desktop.
Save rendon/e0f1338258eb7b3f172c to your computer and use it in GitHub Desktop.
Rugged minimal example
require 'rugged'
dir = "/home/rendon/repos/snippet001/"
name = "README.md"
# For existing repositories
#repo = Rugged::Repository.new(dir)
repo = Rugged::Repository.init_at(dir)
File.open(File.join(dir, name), "w") do |f|
f.write("This is a README file...\n")
end
oid = Rugged::Blob.from_workdir repo, name
index = repo.index
index.add(:path => name, :oid => oid, :mode => 0100644)
options = {}
options[:tree] = index.write_tree(repo)
options[:author] = { :email => "[email protected]", :name => "Test author", :time => Time.now }
options[:comitter] = { :email => "[email protected]", :name => "Test author", :time => Time.now }
options[:message] = "Add README"
options[:parents] = repo.empty? ? [] : [ repo.head.target ].compact
options[:update_ref] = "HEAD"
commit = Rugged::Commit.create(repo, options)
index.write
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment