Jan 022011
 
Screenshot of Meld comparing two Apache configuration files

Both coders and system administrators encounter many cases where text files need to be compared. Every *nix system for the last few decades has included some type of plain-text diff tool, but this is often not enough. The human brain just doesn’t have the capability/capacity to merge the differences between two files.

My grandfather taught me a very important lesson about manual labor: you let the tool do the work. The same lesson can be applied to other fields. Our brains are trained to quickly pick up on visual cues. I work much more efficiently with a utility that displays differences visually.

I use Gentoo Linux, so I frequently have to update config files in /etc when packages release new versions. There are built-in utilities to assist in comparing and updating configuration files, but the inefficient workflow frustrated me. A text-only, commandline comparison just isn’t intuitive.

Diff is Useless

Consider a fairly major upgrade from Apache 2.0 to 2.2. Here is a snippet from diff -u httpd.conf httpd.conf.old (I’ll spare the gory details of the full output, as it’s 1216 lines long):

--- apache2/httpd.conf  2010-10-05 05:29:36.000000000 -0400
+++ apache2.old/httpd.conf      2008-07-23 10:37:58.000000000 -0400
@@ -1,43 +1,204 @@
-# This is a modification of the default Apache 2.2 configuration file
-# for Gentoo Linux.
 #
-# Support:
-#   http://www.gentoo.org/main/en/lists.xml   [mailing lists]
-#   http://forums.gentoo.org/                 [web forums]
-#   irc://irc.freenode.net#gentoo-apache      [irc chat]
+#  This is a modification of the default Apache 2 configuration
+#  file by Gentoo Linux.  .... [insert more]
+#  
+#  Support:
+#     http://www.gentoo.org/main/en/lists.xml    [mailing lists]
+#     http://forums.gentoo.org/                  [web forums]
+#
+#  Bug Reports:
+#     http://bugs.gentoo.org/      [gentoo related bugs]
+#     http://bugs.apache.org/      [apache httpd related bugs]
+
+#
 #
-# Bug Reports:
-#   http://bugs.gentoo.org                    [gentoo related bugs]
-#   http://httpd.apache.org/bug_report.html   [apache httpd related bugs]
 #
+# Based upon the NCSA server configuration files originally by Rob McCool.
 #
-# This is the main Apache HTTP server configuration file.  It contains the
+# This is the main Apache server configuration file.  It contains the
 # configuration directives that give the server its instructions.
-# See  for detailed information.
-# In particular, see
-# 
-# for a discussion of each configuration directive.
+# See  for detailed information about
+# the directives.
 #
 # Do NOT simply read the instructions in here without understanding
 # what they do.  They're here only as hints or reminders.  If you are unsure
-# consult the online docs. You have been warned.
+# consult the online docs. You have been warned.  
+#
+# The configuration directives are grouped into three basic sections:
+#  1. Directives that control the operation of the Apache server process as a
+#     whole (the 'global environment').
+#  2. Directives that define the parameters of the 'main' or 'default' server,
+#     which responds to requests that aren't handled by a virtual host.
+#     These directives also provide default values for the settings
+#     of all virtual hosts.
+#  3. Settings for virtual hosts, which allow Web requests to be sent to
+#     different IP addresses or hostnames and have them handled by the
+#     same Apache server process.
 #
 # Configuration and logfile names: If the filenames you specify for many
 # of the server's control files begin with "/" (or "drive:/" for Win32), the
 # server will use that explicit path.  If the filenames do *not* begin
-# with "/", the value of ServerRoot is prepended -- so "var/log/apache2/foo_log"
-# with ServerRoot set to "/usr" will be interpreted by the
-# server as "/usr/var/log/apache2/foo.log".
+# with "/", the value of ServerRoot is prepended -- so "logs/foo.log"
+# with ServerRoot set to "/usr/lib/apache2" will be interpreted by the
+# server as "/usr/lib/apache2/logs/foo.log".
+#

Sure, you can make sense of this, but it’s difficult. And these aren’t even serious changes – just some updates to the comments at the top of the file. What’s more, you have to know the syntax of diff to understand the output. Why suffer when there are tools that are simple enough for a layperson to understand?

If you use tools that make the problem simpler, you’ll make fewer mistakes and finish more quickly.

Meld Makes Sense

My favorite diff tool is Meld. The color-coded visual interface greatly simplifies the task of merging files. The files are shown side-by-side with related lines connected to each other. In addition to breaking the differences down by line, Meld highlights the differences character-by-character.

Screenshot of Meld comparing two Apache configuration files

Meld Comparing Two Configuration Files

From the Meld homepage:

Meld is a visual diff and merge tool. You can compare two or three files and edit them in place (diffs update dynamically). You can compare two or three folders and launch file comparisons. You can browse and view a working copy from popular version control systems such such as CVS, Subversion, Bazaar-ng and Mercurial.

Usage Examples – Before and After

Meld before resolving file difference

Meld before resolving file difference

Click an arrow to bring the changes from one file to the other.


Meld before inserting changed line

Meld after inserting changed line

Hold CTRL and click to insert the changed line above or below the new line.


Meld before removing changed lines

Meld after removing changed lines

Hold SHIFT and click to remove a group of changed lines.


Hold SHIFT and click to remove a group of changed lines.

You can also treat either side of the window as a standard text editor and make whatever changes you desire. Right click on either of the editor panes to pull up the menu for that file and save your changes.

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(to be displayed above your comment)

(for moderation; your e-mail address will not be made public)