Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Examples for two ways to create Commits

This may help people wanting to do higher-level commit-ish things.
  • Loading branch information...
commit 14aa0904710052de0fadf85d0d5a796fa48a6fc4 1 parent b3ee97a
@rick rick authored
Showing with 31 additions and 8 deletions.
  1. +31 −8 README.md
View
39 README.md
@@ -114,7 +114,24 @@ sha = repo.write(content, type)
```
You can also use the `Commit` object directly to craft a commit; this is a bit
-more high-level, so it may be preferable.
+more high-level, so it may be preferable:
+
+```ruby
+oid = rugged.write("This is a blob.", :blob)
+index = Rugged::Index.new
+index.add(:path => "README.md", :oid => oid, :mode => 0100644)
+
+options = {}
+options[:tree] = index.write_tree(rugged)
+
+options[:author] = { :email => "testuser@github.com", :name => 'Test Author', :time => Time.now }
+options[:committer] = { :email => "testuser@github.com", :name => 'Test Author', :time => Time.now }
+options[:message] ||= "Making a commit via Rugged!"
+options[:parents] = rugged.empty? ? [] : [ rugged.head.target ].compact
+options[:update_ref] = 'HEAD'
+
+Rugged::Commit.create(rugged, options)
+```
---
@@ -217,14 +234,20 @@ tree.each_blob { |entry| puts entry[:name] } # list only files
You can also write trees with the `TreeBuilder`:
```ruby
-entry = {:type => :blob,
- :name => "README.txt",
- :oid => "1385f264afb75a56a5bec74243be9b367ba4ca08",
- :filemode => 33188}
-
+oid = rugged.write("This is a blob.", :blob)
builder = Rugged::Tree::Builder.new
-builder << entry
-sha = builder.write(repo)
+builder << { :type => :blob, :name => "README.md", :oid => oid, :filemode => 0100644 }
+
+options = {}
+options[:tree] = builder.write(rugged)
+
+options[:author] = { :email => "testuser@github.com", :name => 'Test Author', :time => Time.now }
+options[:committer] = { :email => "testuser@github.com", :name => 'Test Author', :time => Time.now }
+options[:message] ||= "Making a commit via Rugged!"
+options[:parents] = rugged.empty? ? [] : [ rugged.head.target ].compact
+options[:update_ref] = 'HEAD'
+
+Rugged::Commit.create(rugged, options)
```
---
Please sign in to comment.
Something went wrong with that request. Please try again.