mirror of
				https://github.com/moby/moby.git
				synced 2022-11-09 12:21:53 -05:00 
			
		
		
		
	Remove duplicated git clone logic.
Signed-off-by: David Calavera <david.calavera@gmail.com>
This commit is contained in:
		
							parent
							
								
									8bd5a95e1e
								
							
						
					
					
						commit
						1cfb307d70
					
				
					 4 changed files with 51 additions and 41 deletions
				
			
		| 
						 | 
				
			
			@ -95,20 +95,11 @@ func (cli *DockerCli) CmdBuild(args ...string) error {
 | 
			
		|||
	} else {
 | 
			
		||||
		root := cmd.Arg(0)
 | 
			
		||||
		if urlutil.IsGitURL(root) {
 | 
			
		||||
			remoteURL := cmd.Arg(0)
 | 
			
		||||
			if !urlutil.IsGitTransport(remoteURL) {
 | 
			
		||||
				remoteURL = "https://" + remoteURL
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			root, err = ioutil.TempDir("", "docker-build-git")
 | 
			
		||||
			root, err = utils.GitClone(root)
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				return err
 | 
			
		||||
			}
 | 
			
		||||
			defer os.RemoveAll(root)
 | 
			
		||||
 | 
			
		||||
			if output, err := exec.Command("git", "clone", "--recursive", remoteURL, root).CombinedOutput(); err != nil {
 | 
			
		||||
				return fmt.Errorf("Error trying to use git: %s (%s)", err, output)
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		if _, err := os.Stat(root); err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -5,9 +5,7 @@ import (
 | 
			
		|||
	"fmt"
 | 
			
		||||
	"io"
 | 
			
		||||
	"io/ioutil"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"os"
 | 
			
		||||
	"os/exec"
 | 
			
		||||
	"strings"
 | 
			
		||||
	"sync"
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -23,6 +21,7 @@ import (
 | 
			
		|||
	"github.com/docker/docker/pkg/urlutil"
 | 
			
		||||
	"github.com/docker/docker/registry"
 | 
			
		||||
	"github.com/docker/docker/runconfig"
 | 
			
		||||
	"github.com/docker/docker/utils"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// whitelist of commands allowed for a commit/import
 | 
			
		||||
| 
						 | 
				
			
			@ -106,21 +105,12 @@ func Build(d *daemon.Daemon, buildConfig *Config) error {
 | 
			
		|||
	if buildConfig.RemoteURL == "" {
 | 
			
		||||
		context = ioutil.NopCloser(buildConfig.Context)
 | 
			
		||||
	} else if urlutil.IsGitURL(buildConfig.RemoteURL) {
 | 
			
		||||
		if !urlutil.IsGitTransport(buildConfig.RemoteURL) {
 | 
			
		||||
			buildConfig.RemoteURL = "https://" + buildConfig.RemoteURL
 | 
			
		||||
		}
 | 
			
		||||
		root, err := ioutil.TempDir("", "docker-build-git")
 | 
			
		||||
		root, err := utils.GitClone(buildConfig.RemoteURL)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
		defer os.RemoveAll(root)
 | 
			
		||||
 | 
			
		||||
		clone := cloneArgs(buildConfig.RemoteURL, root)
 | 
			
		||||
 | 
			
		||||
		if output, err := exec.Command("git", clone...).CombinedOutput(); err != nil {
 | 
			
		||||
			return fmt.Errorf("Error trying to use git: %s (%s)", err, output)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		c, err := archive.Tar(root, archive.Uncompressed)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
| 
						 | 
				
			
			@ -242,21 +232,3 @@ func Commit(d *daemon.Daemon, name string, c *daemon.ContainerCommitConfig) (str
 | 
			
		|||
 | 
			
		||||
	return img.ID, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func cloneArgs(remoteURL, root string) []string {
 | 
			
		||||
	args := []string{"clone", "--recursive"}
 | 
			
		||||
	shallow := true
 | 
			
		||||
 | 
			
		||||
	if strings.HasPrefix(remoteURL, "http") {
 | 
			
		||||
		res, err := http.Head(fmt.Sprintf("%s/info/refs?service=git-upload-pack", remoteURL))
 | 
			
		||||
		if err != nil || res.Header.Get("Content-Type") != "application/x-git-upload-pack-advertisement" {
 | 
			
		||||
			shallow = false
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if shallow {
 | 
			
		||||
		args = append(args, "--depth", "1")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return append(args, remoteURL, root)
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										47
									
								
								utils/git.go
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								utils/git.go
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,47 @@
 | 
			
		|||
package utils
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"io/ioutil"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"os/exec"
 | 
			
		||||
	"strings"
 | 
			
		||||
 | 
			
		||||
	"github.com/docker/docker/pkg/urlutil"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func GitClone(remoteURL string) (string, error) {
 | 
			
		||||
	if !urlutil.IsGitTransport(remoteURL) {
 | 
			
		||||
		remoteURL = "https://" + remoteURL
 | 
			
		||||
	}
 | 
			
		||||
	root, err := ioutil.TempDir("", "docker-build-git")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return "", err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	clone := cloneArgs(remoteURL, root)
 | 
			
		||||
 | 
			
		||||
	if output, err := exec.Command("git", clone...).CombinedOutput(); err != nil {
 | 
			
		||||
		return "", fmt.Errorf("Error trying to use git: %s (%s)", err, output)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return root, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func cloneArgs(remoteURL, root string) []string {
 | 
			
		||||
	args := []string{"clone", "--recursive"}
 | 
			
		||||
	shallow := true
 | 
			
		||||
 | 
			
		||||
	if strings.HasPrefix(remoteURL, "http") {
 | 
			
		||||
		res, err := http.Head(fmt.Sprintf("%s/info/refs?service=git-upload-pack", remoteURL))
 | 
			
		||||
		if err != nil || res.Header.Get("Content-Type") != "application/x-git-upload-pack-advertisement" {
 | 
			
		||||
			shallow = false
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if shallow {
 | 
			
		||||
		args = append(args, "--depth", "1")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return append(args, remoteURL, root)
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -1,4 +1,4 @@
 | 
			
		|||
package builder
 | 
			
		||||
package utils
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue