Kishore Vancheeshwaran
Showing diff in orgmode

This is just an example to show diff in orgmode codeblocks. I suppose this would be useful when you use orgmode to demonstrate/teach programming concepts. The diff would help visualizing what lines have changed.

#+NAME: before
#+BEGIN_EXAMPLE
before
foobar
#+END_EXAMPLE

will render

before
foobar

Similarly,

#+NAME: after
#+BEGIN_EXAMPLE
after
foobar
#+END_EXAMPLE

will render

after
foobar

Now the following will evaluate the diff to the #+RESULTs:.

#+BEGIN_SRC sh :results verbatim :exports results :var in=before :var out=after :wrap SRC diff
echo "$in" >before
echo "$out" >after
diff -u before after
rm -f before after
#+END_SRC

Here is the result.

--- before	2021-06-21 10:58:47.749249096 +0530
+++ after	2021-06-21 10:58:47.749249096 +0530
@@ -1,3 +1,3 @@
-before
+after
 foobar

With python it looks like it’s slightly more involved.

beforepy

#+NAME: beforepy
#+BEGIN_SRC python
print('hello')
print('world')
#+END_SRC

renders as below.

print('hello')
print('world')

afterpy

#+NAME: afterpy
#+BEGIN_SRC python
print('hello')
print('myworld')
#+END_SRC

renders as below.

print('hello')
print('myworld')

To evaluate the diff, we do the following.

#+BEGIN_SRC sh :results verbatim :exports results :noweb yes :wrap SRC diff
cat >beforepy <<'EOF'
<<beforepy>>
EOF
cat >afterpy <<'EOF'
<<afterpy>>
EOF
diff -u beforepy afterpy
rm -f beforepy afterpy
#+END_SRC

which gives the following results.

--- beforepy	2021-06-21 11:07:51.129784218 +0530
+++ afterpy	2021-06-21 11:07:51.139784174 +0530
@@ -1,2 +1,2 @@
 print('hello')
-print('world')
+print('myworld')

Hope that helps.

If you would like to leave a comment, contact me via email.
Post 8/99 - part of 100DaystoOffload