-
-
Save dzaporozhets/2028101 to your computer and use it in GitHub Desktop.
Testing Git merges using Grit
This file contains 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 'grit' | |
require 'fileutils' | |
# Define our paths. | |
# We're gonna have 3 paths: one original, one for the fork, and yet another one for the merge | |
root_path = "#{File.dirname(__FILE__)}/documents" | |
original_path = "#{root_path}/original_repo.git" | |
fork_path = "#{root_path}/fork_repo.git" | |
merge_path = "#{root_path}/merge_repo" | |
# Remove the directories to assure they are clean | |
FileUtils.rm_rf(original_path) | |
FileUtils.rm_rf(fork_path) | |
FileUtils.rm_rf(merge_path) | |
# Create the original repository and do our 1st commit | |
# At this time, we are using bare repositories. | |
# We are only going to need a working area to the merge | |
original_repo = Grit::Repo.init_bare(original_path) | |
original_index = original_repo.index | |
original_index.add('test_merge.txt', "hello1\n") | |
commit1 = original_index.commit('commit 1', nil, Grit::Actor.from_string("First User <[email protected]>")) | |
puts "Original repo commits - after 1st commit" | |
puts original_repo.commits.inspect | |
puts | |
# Do a bare fork on the original repository to create the fork repository | |
# Do our 2nd commit on the forked repository | |
fork_repo = original_repo.fork_bare(fork_path) | |
fork_index = fork_repo.index | |
fork_index.add('test_merge.txt', "hello1\nhello2\n") | |
commit2 = fork_index.commit('commit 2', commit1, Grit::Actor.from_string("Second User <[email protected]>")) | |
puts "Fork repo commits - after 2nd commit" | |
puts fork_repo.commits.inspect | |
puts | |
# Do our 3rd commit, on the original repository | |
original_index.add('test_merge.txt', "hello0\nhello1\n") | |
commit3 = original_index.commit('commit 3', commit1, Grit::Actor.from_string("First User <[email protected]>")) | |
puts "Original repo commits - after 3rd commit" | |
puts original_repo.commits.inspect | |
puts | |
# Clone the original repository into a new repository | |
# At this time we are accessing git directly through merge_repo.git | |
# We don't have methods for doing this in pure Ruby yet, I think | |
original_repo.git.clone({}, original_path, merge_path) | |
Dir.chdir(merge_path) do | |
merge_repo = Grit::Repo.new('.') | |
# Add the fork repository as a remote | |
merge_repo.git.remote({}, "add", "fork", "../fork_repo.git") | |
# Pull from the remote's master branch | |
merge_repo.git.pull({}, "fork", "master") | |
end | |
# Open the merge_repo and shou 4 commits | |
# The first 3 are our commits, and the 4th is the merge commit | |
merge_repo = Grit::Repo.new(merge_path) | |
puts "Merge repo commits - no conflicts" | |
puts merge_repo.commits.inspect | |
puts |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment