Browse code

Add support for comment in .dockerignore

This fix tries to address the issue raised in #20083 where
comment is not supported in `.dockerignore`.

This fix updated the processing of `.dockerignore` so that any
lines starting with `#` are ignored, which is similiar to the
behavior of `.gitignore`.

Related documentation has been updated.

Additional tests have been added to cover the changes.

This fix fixes #20083.

Signed-off-by: Yong Tang <yong.tang.github@outlook.com>

Yong Tang authored on 2016/06/02 07:20:54
Showing 3 changed files
... ...
@@ -20,7 +20,12 @@ func ReadAll(reader io.ReadCloser) ([]string, error) {
20 20
 	var excludes []string
21 21
 
22 22
 	for scanner.Scan() {
23
-		pattern := strings.TrimSpace(scanner.Text())
23
+		// Lines starting with # (comments) are ignored before processing
24
+		pattern := scanner.Text()
25
+		if strings.HasPrefix(pattern, "#") {
26
+			continue
27
+		}
28
+		pattern = strings.TrimSpace(pattern)
24 29
 		if pattern == "" {
25 30
 			continue
26 31
 		}
... ...
@@ -379,9 +379,13 @@ the working and the root directory.  For example, the patterns
379 379
 in the `foo` subdirectory of `PATH` or in the root of the git
380 380
 repository located at `URL`.  Neither excludes anything else.
381 381
 
382
+If a line in `.dockerignore` file starts with `#` in column 1, then this line is
383
+considered as a comment and is ignored before interpreted by the CLI.
384
+
382 385
 Here is an example `.dockerignore` file:
383 386
 
384 387
 ```
388
+# comment
385 389
     */temp*
386 390
     */*/temp*
387 391
     temp?
... ...
@@ -391,6 +395,7 @@ This file causes the following build behavior:
391 391
 
392 392
 | Rule           | Behavior                                                                                                                                                                     |
393 393
 |----------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
394
+| `# comment`    | Ignored.                 |
394 395
 | `*/temp*`      | Exclude files and directories whose names start with `temp` in any immediate subdirectory of the root.  For example, the plain file `/somedir/temporary.txt` is excluded, as is the directory `/somedir/temp`.                 |
395 396
 | `*/*/temp*`    | Exclude files and directories starting with `temp` from any subdirectory that is two levels below the root. For example, `/somedir/subdir/temporary.txt` is excluded. |
396 397
 | `temp?`        | Exclude files and directories in the root directory whose names are a one-character extension of `temp`.  For example, `/tempa` and `/tempb` are excluded.
... ...
@@ -6755,3 +6755,39 @@ func (s *DockerSuite) TestBuildDeleteCommittedFile(c *check.C) {
6755 6755
 		c.Fatal(err)
6756 6756
 	}
6757 6757
 }
6758
+
6759
+// #20083
6760
+func (s *DockerSuite) TestBuildDockerignoreComment(c *check.C) {
6761
+	name := "testbuilddockerignorecleanpaths"
6762
+	dockerfile := `
6763
+        FROM busybox
6764
+        ADD . /tmp/
6765
+        RUN sh -c "(ls -la /tmp/#1)"
6766
+        RUN sh -c "(! ls -la /tmp/#2)"
6767
+        RUN sh -c "(! ls /tmp/foo) && (! ls /tmp/foo2) && (ls /tmp/dir1/foo)"`
6768
+	ctx, err := fakeContext(dockerfile, map[string]string{
6769
+		"foo":      "foo",
6770
+		"foo2":     "foo2",
6771
+		"dir1/foo": "foo in dir1",
6772
+		"#1":       "# file 1",
6773
+		"#2":       "# file 2",
6774
+		".dockerignore": `# Visual C++ cache files
6775
+# because we have git ;-)
6776
+# The above comment is from #20083
6777
+foo
6778
+#dir1/foo
6779
+foo2
6780
+# The following is considered as comment as # is at the beginning
6781
+#1
6782
+# The following is not considered as comment as # is not at the beginning
6783
+  #2
6784
+`,
6785
+	})
6786
+	if err != nil {
6787
+		c.Fatal(err)
6788
+	}
6789
+	defer ctx.Close()
6790
+	if _, err := buildImageFromContext(name, ctx, true); err != nil {
6791
+		c.Fatal(err)
6792
+	}
6793
+}