2

Wikipedia says this is pretty good: http://en.wikipedia.org/wiki/Merge_(revision_control)#Three-way_merge

But how does one implement that? or are there any gems / plugins for Ruby on Rails that will handle that for me?

My situation:
• I have base text
• changes from person A
• changes from person B
• both changes should be included and not overriding the other

any directions I could be pointed in? thanks!

NullVoxPopuli
  • 61,906
  • 73
  • 206
  • 352
  • have you try Google ? first result [is](https://www.ruby-toolbox.com/gems/merge3) – mb14 Nov 11 '11 at 15:11
  • A google search for "ruby gem three way merge" has [this link](https://www.ruby-toolbox.com/gems/merge3) as the top hit. I have not used it, but it "sounds" like a possibility. – Mark Wilkins Nov 11 '11 at 15:13
  • I am not good at google today o.o – NullVoxPopuli Nov 11 '11 at 15:21
  • actually.... I think it'l broken: Merge3::three_way("1234567890", "1a23456", "1234b56", false) RuntimeError: Error nil string passed start=-12 length=22 str=-= – NullVoxPopuli Nov 11 '11 at 15:45

2 Answers2

1

I think you should look at the merge3 gem again [source].

This small example explains it:

require 'rubygems'
require 'merge3'

start = <<TEXT
This is the baseline.
The start.
The end.
TEXT
changed_A = <<TEXT
This is the baseline.
The start (changed by A).
The end.
TEXT
changed_B = <<TEXT
This is the baseline.
The start.
B added this line.
The end.
TEXT

result = Merge3::three_way(start, changed_A, changed_B)

puts result

The output it generates is:

This is the baseline.
The start (changed by A).
B added this line.
The end.

I am not sure how it handles merge conflicts, and since it is supposed to handle 3-way merges of files, it seems to be line-based. If that is a problem (as your example tries to compare simple string), you could add a newline between every character.

Hope this helps.

nathanvda
  • 49,707
  • 13
  • 117
  • 139
0

If you're storing versioned text that you want to be able to merge, then it sounds like you have a perfect use case for calling a version control system. Store the text in files and call the VCS for version control operations (perhaps the Git or Grit gems would be helpful).

Marnen Laibow-Koser
  • 5,959
  • 1
  • 28
  • 33
  • I am forever a noob when it comes to versioning like git. I know you can make diffs with the git gem. http://rubydoc.info/gems/git/1.2.5/ . But there are no examples of usage / how to do things. o.o – NullVoxPopuli Nov 11 '11 at 18:33
  • Yeah, me too. Now I understand. Perhaps there are usage examples somewhere? Or try Grit? – Marnen Laibow-Koser Nov 11 '11 at 18:42
  • grit looks like it functions exactly like git. Is there a way to just merge files (though blocks of text would be better) without a repository? – NullVoxPopuli Nov 11 '11 at 18:53
  • Not AFAIK, but if you have more than a file or two, you should have a repository anyway, no? – Marnen Laibow-Koser Nov 11 '11 at 19:06
  • Well, I'm doing versioning for a document, so... it's just a bunch of variations on one "file" – NullVoxPopuli Nov 11 '11 at 19:09
  • Just one document, or is the system designed to support more? – Marnen Laibow-Koser Nov 11 '11 at 19:12
  • It'll end up being many documents per account. So.. I guess using a versioning system might work. It kinda would act as a second database... not sure how I feel about that though. I don't think our server does hourly backups of the HD. hmmmmm. this is tricky. – NullVoxPopuli Nov 11 '11 at 19:17
  • 2
    I tend to think it's better to farm out to Git than reinvent the wheel, but if you really need the DB backups, I can see why you might not do that. – Marnen Laibow-Koser Nov 11 '11 at 19:42