DEV Community

Yousef
Yousef

Posted on • Edited on

Merge Conflicts

This week, I focused on practicing managing multiple topic branches simultaneously and merging them into the main branch. One of my peers created two issues on my project, Learn2Blog, which required my attention and resolution. To address these issues, I created a separate branch for each one based on the main branch. The first merge was smooth, as there were no changes to the main branch since the creation of these topic branches. Git executed a fast-forward merge without any conflicts. However, the second merge proved more challenging, necessitating a three-way recursive merge due to changes on the main branch during the development of the first topic branch. This resulted in a merge conflict.

Code Refactor

Last week, a peer made contributions to my project by adding Markdown support for the app. Unfortunately, due to the state of the code structure at that time, a significant portion of code was duplicated in two separate functions. The contributor offered to refactor the code, but I decided to handle it myself to maintain consistency with my coding style. Consequently, the contributor created an issue outlining the problem. This refactor required substantial changes to the codebase. Initially, there were two functions responsible for generating the HTML output, both containing redundant HTML boilerplate code. To address this, I refactored the code so that there is only one function responsible for generating HTML output, which takes a body argument to populate the HTML body. Additionally, I segregated the logic for processing .txt and .md files into separate functions. Before calling the HtmlBuilder function, either ProcessText or ProcessMarkdown is invoked based on the file extension, and the respective function's output is used inside the HTML body.

Bug Fix: Files with Same Name but Different Extensions in Input Directory

Another issue addressed a bug that caused the output to be overwritten when two files with the same name but different extensions existed in the input directory. For example, if the input directory contained both inputSample.txt and inputSample.md, the app would process both files, but only one output would be generated. To resolve this issue, I introduced logic to check for existing files and append an incrementing number to the file name:

if (File.Exists(outputFileName)) {
    int fileNumber = 1;
    string fileName = Path.GetFileNameWithoutExtension(inputPath);
    string newFileName = $"{fileName}_{fileNumber}";
    outputFileName = Path.Combine(outputPath, newFileName + ".html");

    while (File.Exists(outputFileName)) {
        fileNumber++;
        newFileName = $"{fileName}_{fileNumber}";
        outputFileName = Path.Combine(outputPath, newFileName + ".html");
    }
}
Enter fullscreen mode Exit fullscreen mode

In the code above, I check if the output file name already exists in the output directory. If it does, I add a number to the name and continue checking until a unique file name is found. Then, I adjust outputFileName accordingly and proceed with the rest of the processing. You can review all the changes made in commit 9b83415.

Merge Conflict

As mentioned earlier, merging the code refactor changes into the main branch went smoothly without conflicts. However, merging the bug fix led to a merge conflict because the main branch had changed since the creation of the branch addressing the bug fix. This conflict became evident when I initiated the pull request, as Git couldn't automatically resolve it. I then switched to Visual Studio Code (VS Code) and began the merge process by checking out the main branch and running the merge command:

git merge issue-10
Enter fullscreen mode Exit fullscreen mode

Fortunately, VS Code offers excellent Git integrations and provides notifications when a merge creates a conflict. While the interface may seem daunting at first, it's divided into three sections: incoming changes, existing code, and the resulting changes. Once you become familiar with it, it's a powerful tool for quickly selecting which changes to accept from the incoming branch. Nonetheless, reviewing the required changes was necessary, especially since the previous merge from the code refactor had made significant alterations to the codebase. These changes can be found in commit 8b24da6.

Summary

In summary, this week provided an excellent opportunity to refresh my knowledge of managing topic branches and handling merge conflicts. Reflecting on past experiences, I recall the stress and uncertainty associated with merge conflicts when my team worked on a game project last year. At the time, we lacked confidence in resolving them, often hoping that nothing would break after the merge. Unfortunately, sometimes issues did arise. However, I now feel more confident in dealing with merge conflicts and am better equipped to manage them effectively.

Top comments (0)