# Patchwork [1,of,4] trydiff: collect header-writing in one place

Submitter Martin von Zweigbergk Feb. 2, 2015, 11:45 p.m. mbox | patch /patch/7605/ Accepted show

Martin von Zweigbergk - Feb. 2, 2015, 11:45 p.m.
```# HG changeset patch
# User Martin von Zweigbergk <martinvonz@google.com>
# Date 1421974854 28800
#      Thu Jan 22 17:00:54 2015 -0800
# Node ID e9a8ab833c8ba15c3fb80e274c2cd0b132763ea1
# Parent  8b88870cbd1eeefaee0af053ae36728f8c0a1847
trydiff: collect header-writing in one place

This is the first step towards simplifying the big loop in
trydiff(). This will make both the header code and the non-header code
clearer, and it prepares for further simplification of the many nested
if-statements in the body of the loop.
```

## Patch

```diff --git a/mercurial/patch.py b/mercurial/patch.py
--- a/mercurial/patch.py
+++ b/mercurial/patch.py
@@ -1795,10 +1795,12 @@
if f not in ctx1:
for f in sorted(modified + added + removed):
+        flag1 = None
+        flag2 = None
content1 = None
content2 = None
binarydiff = False
+        op = None
content1 = getfilectx(f, ctx1).data()
if f not in removedset:
@@ -1806,26 +1808,21 @@
f1, f2 = f, f
if opts.git or losedatafn:
-                mode2 = gitmode[ctx2.flags(f)]
+                flag2 = ctx2.flags(f)
if f in copy:
if opts.git:
f1 = copy[f]
-                        mode1 = gitmode[ctx1.flags(f1)]
+                        flag1 = ctx1.flags(f1)
if f1 in removedset and f1 not in gone:
op = 'rename'
else:
op = 'copy'
-                        header.append('%s from %s\n' % (op, join(f1)))
-                        header.append('%s to %s\n' % (op, join(f)))
content1 = getfilectx(f1, ctx1).data()
else:
losedatafn(f)
else:
-                    if opts.git:
-                        header.append('new file mode %s\n' % mode2)
-                    elif ctx2.flags(f):
+                    if not opts.git and flag2:
losedatafn(f)
if util.binary(content1) or util.binary(content2):
if opts.git:
@@ -1842,8 +1839,7 @@
and copy[copyto[f]] == f):
continue
else:
-                        header.append('deleted file mode %s\n' %
-                                      gitmode[ctx1.flags(f)])
+                        flag1 = ctx1.flags(f)
if util.binary(content1):
binarydiff = True
elif not content1 or util.binary(content1):
@@ -1854,12 +1850,23 @@
flag2 = ctx2.flags(f)
binary = util.binary(content1) or util.binary(content2)
if opts.git:
if binary:
binarydiff = True
elif binary or flag2 != flag1:
losedatafn(f)

+        if opts.git:
+            if content1 is None: # added
+                header.append('new file mode %s\n' % gitmode[flag2])
+            elif content2 is None: # removed
+                header.append('deleted file mode %s\n' % gitmode[flag1])
+            else:  # modified/copied/renamed