Merging: auto-merge-with-comments

Table of Contents

1 Reading

Read section 3.2 and 7.8. Honestly, most of the commands you'll actually use in this code are from 3.2, but there's a lot of good stuff in 7.8.

You are responsible for memorizing the meaning/use of the folowing commands:

git merge

2 Write the code

Write a script called auto-merge-with-comments.

The basic idea of this script is that it takes two branches, automatically merges them and commits the result. However, if it encounters merge conflicts, it resolves the conflict by using one version as the correct version and including it unmodified, but including the other version as comment in the source code.

It takes 4 parameters:

  • –use SHA this is the SHA of the commit that we want to use unmodified in case of conflicts
  • –commitify SHA this the SHA of the commit we want to insert as comments only
  • –branch NAME this is the name of the branch where the output will go
  • –prefix STRING this is the prefix that will make a line a comment. So we'd use "// " in C++ or "# " in ruby

For example, imagine when attempting to merge branch1 and branch2, we get a conflicted file that looks like this:

This file has several lines
<<<<<<< branch1
Here are some branchone changes
Here's some branch2 changes
That span several lines
>>>>>>> branch2
Some future authors
Might modify it
In a way that could cause a merge conflict
<<<<<<< branch1
branch1 added this line
branch2 added this line
>>>>>>> branch2
That would be sad
This line is added by branch2 but is not a conflict

Note in this case I've replaced HEAD with branch1 to make the example clear.

If we auto-merged these branches using our script, making branch1 be the "use" branch and branch2 be the comment branch with the prefix "COMMENT " the result would be:

This file has several lines
COMMENT Here's some branch2 changes
COMMENT That span several lines
Here are some branchone changes
Some future authors
Might modify it
In a way that could cause a merge conflict
COMMENT branch2 added this line
branch1 added this line
That would be sad
This line is added by branch2 but is not a conflict

This kind of auto-merging should be be done for all conflicted files.

BTW, the command

git diff --name-only --diff-filter=U

Will give you all conflicted files in a merge

This script is more of a toy than a real tool, so here's a few things you don't have to worry about:

  • You can assume the git state of the repo has nothing interesting on it. So your first command can safely be a "reset –hard"
  • This command expect you to work with the conflicted files that are generated by git, containing strings like ">>>>>>>" and "====". There's actually better ways to build merge tools that operate on the original files themselves, but don't bother unless you really want to
  • Similarly, you can safely assume that the special conflicted lines (e.g. >>>>>) will never arise naturally in your files
  • You can assume the repo has strictly EITHER safe auto-merges or files that are in a conflicted editing state. You don't have to worry about (say) files that are deleted on one branch but modified in another

I've provided an example script in ruby that gives you a start:


But, if you prefer feel free to use any scripting language you are comfortable with.

3 Turn-in

You will turn in via Moodle. You must turn in 2 things:

  1. The source code of your script
  2. The a text file with an example output

The example output is a bit tricky because git is so smart about generally avoiding merge conflicts. We'll have to create an example conflict in js-parsons.

Run these commands:

git checkout 7e648132e0
sed -i s/a/aa/ parsons.js examples/turtle-test.html
git commit -am "silly modification"

If this works correctly, you should see a line like this:

[detached HEAD 201a294] silly modification
2 files changed, 1522 insertions(+), 1522 deletions(-)
rewrite examples/turtle-test.html (69%)
rewrite parsons.js (78%)

You will have a different commit SHA because your identity is different. Note that SHA.

Then run your script:

auto-merge-with-comments.rb --use <YOUR_SHA> --commentify deeffdf --branch SomeOutput --prefix "XXXX "

Then checkout the branch with the merge you created (if your script doesn't do that automatically) and run this:

git diff <YOUR_SHA> parsons.js examples/turtle-test.html > output.txt

That will create an output.txt file with the diff of your changes to those two files. Submit that output file plus your code to Moodle.

Author: Buffalo, Rose-Hulman Institute of Technology

Created: 2016-03-30 Wed 14:30

Emacs 24.5.1 (Org mode 8.2.10)