[Kimchi-devel] Improving diff hunk headers

Christy Perez christy at linux.vnet.ibm.com
Tue May 20 16:25:49 UTC 2014


Hi all,

I was looking over patches today and wished that there was better
context for the changed lines. Specifically, I wanted a function name
instead of just the class name.

I finally came across this e-mail [1] from someone at Red Hat: 

"""
Hello,

I'm sure you're aware of hunk header lines in patches:
    @@ -24,5 +24,5 @@ class SomeClass(object):


It's possible to configure which source line Git selects for the hunk
header. By default it uses non-indented lines. A "python" regexp, which
selects functions, classes and methods, is already included in Git; you
just have to enable it. I also use custom regexps for ldifs and API.txt:



$ cat ~/.gitattributes # (Or .git/info/attributes for project-specific
config)

*.py diff=python
API.txt diff=apitxt
*.ldif diff=ldif
*.update diff=ldif


$ git config -l  # set with e.g. `git config --global key="value"`
...
core.attributesfile=~/.gitattributes
diff.apitxt.xfuncname=^command: .*
diff.ldif.xfuncname=^dn:.*


Enjoy your Friday!

--
Petr³
"""

We can also define our own, as per:
http://stackoverflow.com/questions/2783086/how-does-git-diff-generate-hunk-descriptions

I put *.py diff=python into my kimchi project's .git/info/attributes
file, and I like these diffs a lot better.

For example (using a really old branch I should have deleted while ago!
):

@@ -37,7 +37,6 @@ class AsyncTask(object):
         self.status = 'running'
         self.message = 'OK'
         self._save_helper()
-        self._cp_request = cherrypy.serving.request
         self.thread = threading.Thread(target=self._run_helper,
                                        args=(opaque, self._status_cb))
         self.thread.setDaemon(True)
@@ -37,7 +37,6 @@ class AsyncTask(object):
         self.status = 'running'
         self.message = 'OK'
         self._save_helper()
-        self._cp_request = cherrypy.serving.request
         self.thread = threading.Thread(target=self._run_helper,
                                        args=(opaque, self._status_cb))
         self.thread.setDaemon(True)
@@ -60,17 +59,13 @@ class AsyncTask(object):
         obj = {}
         for attr in ('id', 'target_uri', 'message', 'status'):
             obj[attr] = getattr(self, attr)
-        try:
-            with self.objstore as session:
-                session.store('task', self.id, obj)
-        except Exception as e:
-            raise OperationFailed('KCHASYNC0002E', {'err': e.message})
+        with self.objstore as session:
+            session.store('task', self.id, obj)
 
     def _run_helper(self, opaque, cb):
-        cherrypy.serving.request = self._cp_request
         try:

changes to:

diff --git a/src/kimchi/asynctask.py b/src/kimchi/asynctask.py
index e2d2dcf..8f0d96c 100644
--- a/src/kimchi/asynctask.py
+++ b/src/kimchi/asynctask.py
@@ -37,7 +37,6 @@ def __init__(self, id, target_uri, fn, objstore,
opaque=None):
         self.status = 'running'
         self.message = 'OK'
         self._save_helper()
-        self._cp_request = cherrypy.serving.request
         self.thread = threading.Thread(target=self._run_helper,
                                        args=(opaque, self._status_cb))
         self.thread.setDaemon(True)
@@ -60,17 +59,13 @@ def _save_helper(self):
         obj = {}
         for attr in ('id', 'target_uri', 'message', 'status'):
             obj[attr] = getattr(self, attr)
-        try:
-            with self.objstore as session:
-                session.store('task', self.id, obj)
-        except Exception as e:
-            raise OperationFailed('KCHASYNC0002E', {'err': e.message})
+        with self.objstore as session:

So now you can see the function that the code is in, not the
larger-scope (the class).

I thought I would share, in case any one else wanted to use this.

Maybe we could even add a .gitattributes file to kimchi if others think
it's a good idea to enforce some project-specific conventions.

Happy coding!

- Christy

[1]
http://www.redhat.com/archives/freeipa-devel/2013-April/msg00381.html




More information about the Kimchi-devel mailing list