Skip to content

Commit 936b2c6

Browse files
committed
adding message option to the import subcommand
Signed-off-by: Taylor Jones <[email protected]>
1 parent 82e2dec commit 936b2c6

File tree

6 files changed

+70
-6
lines changed

6 files changed

+70
-6
lines changed

api/client/import.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ func (cli *DockerCli) CmdImport(args ...string) error {
2323
cmd := Cli.Subcmd("import", []string{"file|URL|- [REPOSITORY[:TAG]]"}, "Create an empty filesystem image and import the contents of the\ntarball (.tar, .tar.gz, .tgz, .bzip, .tar.xz, .txz) into it, then\noptionally tag it.", true)
2424
flChanges := opts.NewListOpts(nil)
2525
cmd.Var(&flChanges, []string{"c", "-change"}, "Apply Dockerfile instruction to the created image")
26+
message := cmd.String([]string{"m", "-message"}, "", "Set commit message for imported image")
2627
cmd.Require(flag.Min, 1)
2728

2829
cmd.ParseFlags(args, true)
@@ -35,6 +36,7 @@ func (cli *DockerCli) CmdImport(args ...string) error {
3536

3637
v.Set("fromSrc", src)
3738
v.Set("repo", repository)
39+
v.Set("message", *message)
3840
for _, change := range flChanges.GetAll() {
3941
v.Add("changes", change)
4042
}

api/server/image.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,10 @@ func (s *Server) postImagesCreate(version version.Version, w http.ResponseWriter
7070
}
7171

7272
var (
73-
image = r.Form.Get("fromImage")
74-
repo = r.Form.Get("repo")
75-
tag = r.Form.Get("tag")
73+
image = r.Form.Get("fromImage")
74+
repo = r.Form.Get("repo")
75+
tag = r.Form.Get("tag")
76+
message = r.Form.Get("message")
7677
)
7778
authEncoded := r.Header.Get("X-Registry-Auth")
7879
authConfig := &cliconfig.AuthConfig{}
@@ -126,7 +127,7 @@ func (s *Server) postImagesCreate(version version.Version, w http.ResponseWriter
126127
return err
127128
}
128129

129-
err = s.daemon.Repositories().Import(src, repo, tag, r.Body, output, newConfig)
130+
err = s.daemon.Repositories().Import(src, repo, tag, message, r.Body, output, newConfig)
130131
}
131132
if err != nil {
132133
if !output.Flushed() {

docs/reference/commandline/import.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ weight=1
1818
optionally tag it.
1919

2020
-c, --change=[] Apply specified Dockerfile instructions while importing the image
21+
-m, --message= Set commit message for imported image
2122

2223
You can specify a `URL` or `-` (dash) to take data directly from `STDIN`. The
2324
`URL` can point to an archive (.tar, .tar.gz, .tgz, .bzip, .tar.xz, or .txz)
@@ -46,6 +47,10 @@ Import to docker via pipe and `STDIN`.
4647

4748
$ cat exampleimage.tgz | docker import - exampleimagelocal:new
4849

50+
Import with a commit message
51+
52+
$ cat exampleimage.tgz | docker import --message "New image imported from tarball" - exampleimagelocal:new
53+
4954
Import to docker from a local archive.
5055

5156
$ docker import /path/to/exampleimage.tgz

graph/import.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
// inConfig (if src is "-"), or from a URI specified in src. Progress output is
1717
// written to outStream. Repository and tag names can optionally be given in
1818
// the repo and tag arguments, respectively.
19-
func (s *TagStore) Import(src string, repo string, tag string, inConfig io.ReadCloser, outStream io.Writer, containerConfig *runconfig.Config) error {
19+
func (s *TagStore) Import(src string, repo string, tag string, msg string, inConfig io.ReadCloser, outStream io.Writer, containerConfig *runconfig.Config) error {
2020
var (
2121
sf = streamformatter.NewJSONStreamFormatter()
2222
archive io.ReadCloser
@@ -54,7 +54,11 @@ func (s *TagStore) Import(src string, repo string, tag string, inConfig io.ReadC
5454
}
5555

5656
defer archive.Close()
57-
img, err := s.graph.Create(archive, "", "", "Imported from "+src, "", nil, containerConfig)
57+
if len(msg) == 0 {
58+
msg = "Imported from " + src
59+
}
60+
61+
img, err := s.graph.Create(archive, "", "", msg, "", nil, containerConfig)
5862
if err != nil {
5963
return err
6064
}

integration-cli/docker_cli_import_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"io/ioutil"
66
"os"
77
"os/exec"
8+
"regexp"
89
"strings"
910

1011
"github.com/go-check/check"
@@ -72,6 +73,49 @@ func (s *DockerSuite) TestImportFile(c *check.C) {
7273
}
7374
}
7475

76+
func (s *DockerSuite) TestImportFileWithMessage(c *check.C) {
77+
dockerCmd(c, "run", "--name", "test-import", "busybox", "true")
78+
79+
temporaryFile, err := ioutil.TempFile("", "exportImportTest")
80+
if err != nil {
81+
c.Fatal("failed to create temporary file", "", err)
82+
}
83+
defer os.Remove(temporaryFile.Name())
84+
85+
runCmd := exec.Command(dockerBinary, "export", "test-import")
86+
runCmd.Stdout = bufio.NewWriter(temporaryFile)
87+
88+
_, err = runCommand(runCmd)
89+
if err != nil {
90+
c.Fatal("failed to export a container", err)
91+
}
92+
93+
message := "Testing commit message"
94+
out, _ := dockerCmd(c, "import", "-m", message, temporaryFile.Name())
95+
if n := strings.Count(out, "\n"); n != 1 {
96+
c.Fatalf("display is messed up: %d '\\n' instead of 1:\n%s", n, out)
97+
}
98+
image := strings.TrimSpace(out)
99+
100+
out, _ = dockerCmd(c, "history", image)
101+
split := strings.Split(out, "\n")
102+
103+
if len(split) != 3 {
104+
c.Fatalf("expected 3 lines from image history, got %d", len(split))
105+
}
106+
r := regexp.MustCompile("[\\s]{2,}")
107+
split = r.Split(split[1], -1)
108+
109+
if message != split[3] {
110+
c.Fatalf("expected %s in commit message, got %s", message, split[3])
111+
}
112+
113+
out, _ = dockerCmd(c, "run", "--rm", image, "true")
114+
if out != "" {
115+
c.Fatalf("command output should've been nothing, was %q", out)
116+
}
117+
}
118+
75119
func (s *DockerSuite) TestImportFileNonExistentFile(c *check.C) {
76120
_, exitCode, err := dockerCmdWithError("import", "example.com/myImage.tar")
77121
if exitCode == 0 || err == nil {

man/docker-import.1.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ docker-import - Create an empty filesystem image and import the contents of the
77
# SYNOPSIS
88
**docker import**
99
[**-c**|**--change**[= []**]]
10+
[**-m**|**--message**[=*MESSAGE*]]
1011
[**--help**]
1112
file|URL|- [REPOSITORY[:TAG]]
1213

@@ -15,6 +16,9 @@ file|URL|- [REPOSITORY[:TAG]]
1516
Apply specified Dockerfile instructions while importing the image
1617
Supported Dockerfile instructions: `CMD`|`ENTRYPOINT`|`ENV`|`EXPOSE`|`ONBUILD`|`USER`|`VOLUME`|`WORKDIR`
1718

19+
**-m**, **--message**=""
20+
Set commit message for imported image
21+
1822
# DESCRIPTION
1923
Create a new filesystem image from the contents of a tarball (`.tar`,
2024
`.tar.gz`, `.tgz`, `.bzip`, `.tar.xz`, `.txz`) into it, then optionally tag it.
@@ -35,6 +39,10 @@ Import to docker via pipe and stdin:
3539

3640
# cat exampleimage.tgz | docker import - example/imagelocal
3741

42+
Import with a commit message
43+
44+
# cat exampleimage.tgz | docker import --message "New image imported from tarball" - exampleimagelocal:new
45+
3846
Import to a Docker image from a local file.
3947

4048
# docker import /path/to/exampleimage.tgz

0 commit comments

Comments
 (0)