Saturday, February 25, 2017

git: Procedures To Resolve Merge Conflict

GIT Command Line Cheat Sheet

git branch -a #List branches
git branch --delete BRANCH_NAME #Delete a branch
git checkout mybranch #switch to mybranch
git merge FromBranch #merge FromBranch to the current working branch

Introduction

This post is an introduction on how to resolve merge conflict. The following procedure will create a merge conflict

As shown in the following picture, the merge conflict is caused by that the same file was change at two different branches at different time. Typically, one develop check out the code from master branch, made some change and merged to the master branch. Another developer checkout the code after the previous developer and committed the code later.

Create Merge Conflicts

$ mkdir groovy
$ cd groovy
$ vi basic_one.groovy

Copy and paste the following code to the file of basic_one.grooy, and safe it (:wq)

println('Hello World!!!')
def myList = [1, 3, 4, 5, [10, 13], 1, 2, 3, [15, 16], 22, 33]
println newList = myList.flatten()
newList.each { item -> print item + " " }
println()

Now execute the following commands

git init
git add .
git commit -am "first commit on master"

So far we have commit a file to our local repository. Next, we will create a new branch, namely, develop. We will make changes to the same file

Now execute the following commands

git branch develop    # create develop branch
git branch -a         # list branches, you should see the * master and develop. 
                      # The start * indicates we are working on master
git checkout develop  # switch to develop branch
git branch -a

Now, I see the following:

Gary2013@Guojiangs-MacBook-Pro:~/atom-dev/groovy$ git branch -a
* develop
  master
Gary2013@Guojiangs-MacBook-Pro:~/atom-dev/groovy$ 

You can see that the current active branch is develop. We will change the content of the file to the following:

println('Hello,  Gary Liu!')
def myList = [10, 13, 15, 16, 22, 33, 44, 55] 
println newList = myList.flatten(); 
newList.each { item -> print item + " " };
println();

Now execute the following commands to commit the develop branch:

git commit -am "First commit to develop branch"

In order to create conflicts, we will need to checkout the master branch and do some modification on the file

git checkout master.

If you execute: cat basic_one.groovy, you should see the following:

println('Hello World!!!')
def myList = [1, 3, 4, 5, [10, 13], 1, 2, 3, [15, 16], 22, 33]
println newList = myList.flatten()
newList.each { item -> print item + " " }
println()

This is where we left off. In the meantime, develop branch has changed. Let's make some change to the file and it will be looks like the following:

println('Hello,  World Again!');
def myList = [-1, -2, 1, 3, 4, 5, [10, 13], 1, 2, 3, [15, 16], 22, 33] 
println newList = myList.flatten()

newList.each {
   item -> print item + " " 
}
println()

Remember that we are at master branch now. We will merge from develop branch to the master

git add .
git commit -am "Second commit on master"
git merge develop

Now we see the following output:

Gary2013@Guojiangs-MacBook-Pro:~/atom-dev/groovy$ git merge develop
Auto-merging basic_one.groovy
CONFLICT (content): Merge conflict in basic_one.groovy
Automatic merge failed; fix conflicts and then commit the result.
Gary2013@Guojiangs-MacBook-Pro:~/atom-dev/groovy$ 

We have successfully created merge conflict. If you can the file of basic_one.groovy, it looks like the following:

Gary2013@Guojiangs-MacBook-Pro:~/atom-dev/groovy$ cat basic_one.groovy 
<<<<<<< HEAD
println('Hello,  World Again!');
def myList = [-1, -2, 1, 3, 4, 5, [10, 13], 1, 2, 3, [15, 16], 22, 33]
println newList = myList.flatten()

newList.each {
   item -> print item + " "
}
println()
=======
println('Hello,  Gary Liu!')
def myList = [10, 13, 15, 16, 22, 33, 44, 55]
println newList = myList.flatten(); 
newList.each { item -> print item + " " };
println();
>>>>>>> develop
Gary2013@Guojiangs-MacBook-Pro:~/atom-dev/groovy$ 

Resolve Merge Conflicts

The above case is a bit extreme, as the whole file is almost changed. The HEAD section is from master branch and the lower part is from develop. To resolve the merge conflict, we need to edit file. For this case I will just take the master case, and the resultant file looks like the following:

Gary2013@Guojiangs-MacBook-Pro:~/atom-dev/groovy$ vi basic_one.groovy 
Gary2013@Guojiangs-MacBook-Pro:~/atom-dev/groovy$ git commit -am "Resolve merge conflict"
[master d2d5ecf] Resolve merge conflict
Gary2013@Guojiangs-MacBook-Pro:~/atom-dev/groovy$ git merge develop
Already up-to-date.
Gary2013@Guojiangs-MacBook-Pro:~/atom-dev/groovy$ cat basic_one.groovy 
println('Hello,  World Again!');
def myList = [-1, -2, 1, 3, 4, 5, [10, 13], 1, 2, 3, [15, 16], 22, 33]
println newList = myList.flatten()

newList.each {
   item -> print item + " "
}
println()
Gary2013@Guojiangs-MacBook-Pro:~/atom-dev/groovy$ 

To resolve merge conflict, we have to manually modify the file. We can use IDE like eclipse, sourceTree, etc, but we have to make decisions on which one is the best. After that we need to commit the changes and do a merge again to make sure that all the conflicts are addressed correctly.

Thursday, February 23, 2017

Mule Dev Trick: Using Message Properties Transformer

The Abuse of set-variable

Many places I saw the code like the following:

Basically, developers set a bunch of flow variables for later usage. First of all, this kind of situation reflects design flaws. We should ask our selves why do we need to set so many flow variables? Can we use bean instead?

Use Message Property Transformer

Mule platform provide a very powerful transformer, namely message property transformer. Here is an example code:
        
            
            
            
            
            
            
        

The scope can be:
  invocation: flow variables
  outbound: property
  session: session variables
By using message property transformer, you can simplify the design, and make the flow easier to review.

Thursday, February 9, 2017

Mule Dev Trick: Load A Text File And Convert To ArrayList

The Challenge

Here is the task. I have a file like the following:
AAAA
BBBB
CCCC
DDDD
I need to convert this content to an ArrayList, so that I can look through them. Actually each line is an object name. I need to query those objects from web services.

Solution

Here are the steps:
  • Load file and payload as string. Mule provide a construct called "parse-template", which can be used to load file. This construct will load the file and convert the content to a string payload.
  • Use Groovy or MEL to convert the string to an ArrayList
Here are the source code:

        
        

        
        

        
            
        

Key Learning Points

Few points we learn from this:
  1. MEL support java directly. This is really very powerful.
  2. Groovy is very powerful tool in Mule application development. collect {it.trim()} is similar as java stream function.