Format Code Differences

The diff command has variety of options and produces output in different formats. I have tried some of them and I find the context and unified formats, easier to interpret and explain. Through this blog, I intend to present python based solution for rendering the output using HTML.

Input Files

Before we do that, let us create couple of scripts (namely A and B), which would provide input for diff command variations.

[vagrant@localhost python]$ cat -n A
     1  while read num
     2  do
     3    printf "%d " ${num}
     4  done <<EOF
     5  1
     6  2
     7  3
     8  4
     9  5
    10  EOF
    11
    12  echo
    13
[vagrant@localhost python]$ cat -n B
     1  typeset -i sum=0
     2
     3  while read num
     4  do
     5    printf "%d " ${num}
     6    sum=sum+${num}
     7  done <<EOF
     8  1
     9  2
    10  3
    11  4
    12  5
    13  EOF
    14
    15  echo; echo "Sum: ${sum}"
[vagrant@localhost python]$

Surfing through various open source repositories, I have observed the diff command output is captured in the unified format almost everywhere. The below two sections will give you an example of diff command output in both context/unified format.

Context Differences (Click Here)

[vagrant@localhost python]$ diff -c A B
*** A   2014-08-20 20:13:30.315009258 +0000
--- B   2014-08-20 20:13:39.021009349 +0000
***************
*** 1,6 ****
--- 1,9 ----
+ typeset -i sum=0
+
  while read num
  do
    printf "%d " ${num}
+   sum=sum+${num}
  done <<EOF
  1
  2
***************
*** 9,13 ****
  5
  EOF

! echo
!
--- 12,15 ----
  5
  EOF

! echo; echo "Sum: ${sum}"
[vagrant@localhost python]$

Unified Differences (Click Here)

[vagrant@localhost python]$ diff -u A B
--- A   2014-08-20 20:13:30.315009258 +0000
+++ B   2014-08-20 20:13:39.021009349 +0000
@@ -1,6 +1,9 @@
+typeset -i sum=0
+
 while read num
 do
   printf "%d " ${num}
+  sum=sum+${num}
 done <<EOF
 1
 2
@@ -9,5 +12,4 @@
 5
 EOF

-echo
-
+echo; echo "Sum: ${sum}"
[vagrant@localhost python]$

Development Environment

The commands present earlier on this page and in related blog posts, were executed under environment setup using vagrant 1.6.3 with chef/centos-6.5 (virtualbox, 1.0.0)

If you have questions on setting up environment like that, please go through any books or vagrant’s website for more information.

[vagrant@localhost python]$ uname -a
Linux localhost.localdomain 2.6.32-431.el6.x86_64 #1 SMP Fri Nov 22 03:15:09 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
[vagrant@localhost python]$ python --version
Python 2.6.6
[vagrant@localhost python]$ diff --version
diff (GNU diffutils) 2.8.1
Copyright (C) 2002 Free Software Foundation, Inc.

This program comes with NO WARRANTY, to the extent permitted by law.
You may redistribute copies of this program
under the terms of the GNU General Public License.
For more information about these matters, see the file named COPYING.

Written by Paul Eggert, Mike Haertel, David Hayes,
Richard Stallman, and Len Tower.
[vagrant@localhost python]$
Advertisements

2 thoughts on “Format Code Differences

  1. Pingback: code4Py | Style Context Differences | PrayogShala

  2. Pingback: code4Py | Style Unified Differences | PrayogShala

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s