Last active
August 29, 2015 13:56
-
-
Save mhodgson/9006859 to your computer and use it in GitHub Desktop.
Rugged Merge Conflict Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rugged' | |
repo = Rugged::Repository.init_at('./test-repo') | |
index = repo.index | |
base_commit_options = { | |
author: { name: "Matt", email: "[email protected]" }, | |
committer: { name: "Matt", email: "[email protected]" }, | |
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 = repo.branches['branch'] | |
master = repo.branches['master'] | |
branch_commit = branch.tip | |
master_commit = master.tip | |
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) | |
merge_index.conflicts.count == 0 #=> false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment