e358d9
From 6d7ab38f33edb9ab87a290a0c68cfd27b55b061f Mon Sep 17 00:00:00 2001
e358d9
From: Nalin Dahyabhai <nalin@redhat.com>
e358d9
Date: Wed, 8 Jan 2020 11:02:05 -0500
e358d9
Subject: [PATCH 1/2] Check for .dockerignore specifically
e358d9
e358d9
When generating the list of exclusions to process .dockerignore
e358d9
contents, don't include .dockerignore if we don't have a .dockerignore
e358d9
file in the context directory.  That way, if the file doesn't exist, and
e358d9
the caller didn't pass in any patterns, we get no patterns instead of
e358d9
just one ".dockerignore" pattern, and we can hit the faster copy path.
e358d9
e358d9
Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
e358d9
e358d9
Closes: #2072
e358d9
Approved by: giuseppe
e358d9
---
e358d9
 add.go | 10 ++++++++--
e358d9
 1 file changed, 8 insertions(+), 2 deletions(-)
e358d9
e358d9
diff --git a/add.go b/add.go
e358d9
index b5119e369..e82a5ef9a 100644
e358d9
--- a/add.go
e358d9
+++ b/add.go
e358d9
@@ -215,7 +215,12 @@ func dockerIgnoreMatcher(lines []string, contextDir string) (*fileutils.PatternM
e358d9
 	if contextDir == "" {
e358d9
 		return nil, nil
e358d9
 	}
e358d9
-	patterns := []string{".dockerignore"}
e358d9
+	// If there's no .dockerignore file, then we don't have to add a
e358d9
+	// pattern to tell copy logic to ignore it later.
e358d9
+	var patterns []string
e358d9
+	if _, err := os.Stat(filepath.Join(contextDir, ".dockerignore")); err == nil || !os.IsNotExist(err) {
e358d9
+		patterns = []string{".dockerignore"}
e358d9
+	}
e358d9
 	for _, ignoreSpec := range lines {
e358d9
 		ignoreSpec = strings.TrimSpace(ignoreSpec)
e358d9
 		// ignore comments passed back from .dockerignore
e358d9
@@ -224,7 +229,8 @@ func dockerIgnoreMatcher(lines []string, contextDir string) (*fileutils.PatternM
e358d9
 		}
e358d9
 		// if the spec starts with '!' it means the pattern
e358d9
 		// should be included. make a note so that we can move
e358d9
-		// it to the front of the updated pattern
e358d9
+		// it to the front of the updated pattern, and insert
e358d9
+		// the context dir's path in between
e358d9
 		includeFlag := ""
e358d9
 		if strings.HasPrefix(ignoreSpec, "!") {
e358d9
 			includeFlag = "!"
e358d9
e358d9
From f999964084ce75c833b0cffd17fb09b947dad506 Mon Sep 17 00:00:00 2001
e358d9
From: Nalin Dahyabhai <nalin@redhat.com>
e358d9
Date: Wed, 8 Jan 2020 11:04:57 -0500
e358d9
Subject: [PATCH 2/2] copyFileWithTar: close source files at the right time
e358d9
e358d9
Close source files after we've finished reading from them, rather than
e358d9
leaving it for later.
e358d9
e358d9
Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
e358d9
e358d9
Closes: #2072
e358d9
Approved by: giuseppe
e358d9
---
e358d9
 util.go | 9 +++------
e358d9
 1 file changed, 3 insertions(+), 6 deletions(-)
e358d9
e358d9
diff --git a/util.go b/util.go
e358d9
index b4670e41c..2f923357c 100644
e358d9
--- a/util.go
e358d9
+++ b/util.go
e358d9
@@ -165,11 +165,6 @@ func (b *Builder) copyFileWithTar(tarIDMappingOptions *IDMappingOptions, chownOp
e358d9
 				if err != nil {
e358d9
 					return errors.Wrapf(err, "error opening %q to copy its contents", src)
e358d9
 				}
e358d9
-				defer func() {
e358d9
-					if err := f.Close(); err != nil {
e358d9
-						logrus.Debugf("error closing %s: %v", fi.Name(), err)
e358d9
-					}
e358d9
-				}()
e358d9
 			}
e358d9
 		}
e358d9
 
e358d9
@@ -200,6 +195,9 @@ func (b *Builder) copyFileWithTar(tarIDMappingOptions *IDMappingOptions, chownOp
e358d9
 					logrus.Debugf("error copying contents of %s: %v", fi.Name(), err)
e358d9
 					copyErr = err
e358d9
 				}
e358d9
+				if err = srcFile.Close(); err != nil {
e358d9
+					logrus.Debugf("error closing %s: %v", fi.Name(), err)
e358d9
+				}
e358d9
 			}
e358d9
 			if err = writer.Close(); err != nil {
e358d9
 				logrus.Debugf("error closing write pipe for %s: %v", hdr.Name, err)
e358d9
@@ -213,7 +211,6 @@ func (b *Builder) copyFileWithTar(tarIDMappingOptions *IDMappingOptions, chownOp
e358d9
 		if err == nil {
e358d9
 			err = copyErr
e358d9
 		}
e358d9
-		f = nil
e358d9
 		if pipeWriter != nil {
e358d9
 			pipeWriter.Close()
e358d9
 		}