Skip to content

Instantly share code, notes, and snippets.

@arthurschreiber
Created February 14, 2014 19:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arthurschreiber/9007153 to your computer and use it in GitHub Desktop.
Save arthurschreiber/9007153 to your computer and use it in GitHub Desktop.
$:.unshift File.expand_path("../lib", __FILE__)
require 'rugged'
repo = Rugged::Repository.init_at('./test-repo')
index = repo.index
base_commit_options = {
author: { name: "Matt", email: "matt@test.com" },
committer: { name: "Matt", email: "matt@test.com" },
update_ref: 'HEAD'
}
version_one = """
Balance Sheets Are Awesome
The cool balance sheet, sometimes called the statement of financial position, lists the company's assets, liabilities,and stockholders' equity as of a specific moment in time.
That specific moment is the close of business on the date of the balance sheet.
A balance sheet is like a nice photograph; it captures the financial position of a company at a particular point in time.
Study about the assets, liabilities, and stockholders equity contained in a balance sheet.
"""
blob_1_oid = repo.write(version_one, :blob)
index.add(path: 'test_file.txt', oid: blob_1_oid)
commit_options = base_commit_options.merge(
parents: repo.empty? ? [] : [repo.head.target].compact,
tree: index.write_tree(repo),
message: 'first commit'
)
Rugged::Commit.create(repo, commit_options)
branch = repo.create_branch('branch')
repo.head = branch.canonical_name
version_two = """
Balance Sheets Are The Best
The cool balance sheet, sometimes called the statement of financial position, lists the company's assets, liabilities,and stockholders' equity as of a specific moment in time.
That specific moment is the close of business on the date of the balance sheet.
A balance sheet is like a nice photograph; it captures the financial position of a company at a particular point in time.
Study about the assets, liabilities, and stockholders equity contained in a balance sheet.
"""
blob_2_oid = repo.write(version_two, :blob)
index.add(path: 'test_file.txt', oid: blob_2_oid)
commit_options = base_commit_options.merge(
parents: repo.empty? ? [] : [repo.head.target].compact,
tree: index.write_tree(repo),
message: 'branch commit'
)
Rugged::Commit.create(repo, commit_options)
master = repo.branches["master"]
repo.head = master.canonical_name
version_three = """
Balance Sheets Are Awesome
The cool balance sheet, sometimes called the statement of financial position, lists the company's assets, liabilities,and stockholders' equity as of a specific moment in time.
That specific moment is the close of business on the date of the balance sheet.
A balance sheet is really like a nice photograph; it captures the financial position of a company at a particular point in time.
Study about the assets, and stockholders equity contained in a balance sheet.
"""
blob_3_oid = repo.write(version_three, :blob)
index.add(path: 'test_file.txt', oid: blob_3_oid)
commit_options = base_commit_options.merge(
parents: repo.empty? ? [] : [repo.head.target].compact,
tree: index.write_tree(repo),
message: 'another master commit'
)
Rugged::Commit.create(repo, commit_options)
branch_commit = branch.target
master_commit = master.target
ancestor_commit_oid = repo.merge_base(master_commit.oid, branch_commit.oid)
ancestor_commit = repo.lookup(ancestor_commit_oid)
merge_index = master_commit.tree.merge(branch_commit.tree, ancestor_commit.tree, favor: :normal)
p merge_index.conflicts.count == 0 #=> false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment