Patchwork [1,of,8] ignore: refactor file read into a function

login
register
mail settings
Submitter Durham Goode
Date May 13, 2015, 3:13 p.m.
Message ID <7bfb75835fb87a5e9060.1431529995@dev2000.prn2.facebook.com>
Download mbox | patch
Permalink /patch/9043/
State Accepted
Headers show

Comments

Durham Goode - May 13, 2015, 3:13 p.m.
# HG changeset patch
# User Durham Goode <durham@fb.com>
# Date 1431057457 25200
#      Thu May 07 20:57:37 2015 -0700
# Node ID 7bfb75835fb87a5e906007144b04b7d735ddfdfa
# Parent  015adbcd92f3bc15305a3ed4ee59676ff358eabf
ignore: refactor file read into a function

This refactors the ignore file reading code into a function so that in a future
patch we can make it recursive.

Patch

diff --git a/mercurial/ignore.py b/mercurial/ignore.py
--- a/mercurial/ignore.py
+++ b/mercurial/ignore.py
@@ -52,6 +52,20 @@  def ignorepats(lines):
 
     return patterns, warnings
 
+def readignorefile(filepath, warn, skipwarning=False):
+    try:
+        pats = []
+        fp = open(filepath)
+        pats, warnings = ignorepats(fp)
+        fp.close()
+        for warning in warnings:
+            warn("%s: %s\n" % (filepath, warning))
+    except IOError, inst:
+        if not skipwarning:
+            warn(_("skipping unreadable ignore file '%s': %s\n") %
+                 (filepath, inst.strerror))
+    return pats
+
 def readpats(root, files, warn):
     '''return a dict mapping ignore-file-name to list-of-patterns'''
 
@@ -59,17 +73,9 @@  def readpats(root, files, warn):
     for f in files:
         if f in pats:
             continue
-        try:
-            pats[f] = []
-            fp = open(f)
-            pats[f], warnings = ignorepats(fp)
-            fp.close()
-            for warning in warnings:
-                warn("%s: %s\n" % (f, warning))
-        except IOError, inst:
-            if f != files[0]:
-                warn(_("skipping unreadable ignore file '%s': %s\n") %
-                     (f, inst.strerror))
+        skipwarning = f == files[0]
+        pats[f] = readignorefile(f, warn, skipwarning=skipwarning)
+
     return [(f, pats[f]) for f in files if f in pats]
 
 def ignore(root, files, warn):