2014-02-07 10:10:01 +00:00
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Repo struct {
|
2014-03-03 06:35:01 +00:00
|
|
|
// The name of the Repository. This should be the
|
|
|
|
// canonical name, for example, github.com/drone/drone.
|
|
|
|
Name string
|
|
|
|
|
2014-02-07 10:10:01 +00:00
|
|
|
// The path of the Repoisotry. This could be
|
|
|
|
// the remote path of a Git repository or the path of
|
|
|
|
// of the repository on the local file system.
|
|
|
|
//
|
|
|
|
// A remote path must start with http://, https://,
|
|
|
|
// git://, ssh:// or git@. Otherwise we'll assume
|
|
|
|
// the repository is located on the local filesystem.
|
|
|
|
Path string
|
|
|
|
|
|
|
|
// (optional) Specific Branch that we should checkout
|
|
|
|
// when the Repository is cloned. If no value is
|
|
|
|
// provided we'll assume the default, master branch.
|
|
|
|
Branch string
|
|
|
|
|
|
|
|
// (optional) Specific Commit Hash that we should
|
|
|
|
// checkout when the Repository is cloned. If no
|
|
|
|
// value is provided we'll assume HEAD.
|
|
|
|
Commit string
|
|
|
|
|
|
|
|
// (optional) Pull Request number that we should
|
|
|
|
// checkout when the Repository is cloned.
|
|
|
|
PR string
|
|
|
|
|
|
|
|
// (optional) The filesystem path that the repository
|
|
|
|
// will be cloned into (or copied to) inside the
|
|
|
|
// host system (Docker Container).
|
|
|
|
Dir string
|
2014-02-12 17:58:07 +00:00
|
|
|
|
|
|
|
// (optional) The depth of the `git clone` command.
|
|
|
|
Depth int
|
2014-02-07 10:10:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsRemote returns true if the Repository is located
|
|
|
|
// on a remote server (ie Github, Bitbucket)
|
|
|
|
func (r *Repo) IsRemote() bool {
|
|
|
|
switch {
|
|
|
|
case strings.HasPrefix(r.Path, "git://"):
|
|
|
|
return true
|
|
|
|
case strings.HasPrefix(r.Path, "git@"):
|
|
|
|
return true
|
|
|
|
case strings.HasPrefix(r.Path, "http://"):
|
|
|
|
return true
|
|
|
|
case strings.HasPrefix(r.Path, "https://"):
|
|
|
|
return true
|
|
|
|
case strings.HasPrefix(r.Path, "ssh://"):
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsLocal returns true if the Repository is located
|
|
|
|
// on the local filesystem.
|
|
|
|
func (r *Repo) IsLocal() bool {
|
|
|
|
return !r.IsRemote()
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsGit returns true if the Repository is
|
|
|
|
// a Git repoisitory.
|
|
|
|
func (r *Repo) IsGit() bool {
|
|
|
|
switch {
|
|
|
|
case strings.HasPrefix(r.Path, "git://"):
|
|
|
|
return true
|
|
|
|
case strings.HasPrefix(r.Path, "git@"):
|
|
|
|
return true
|
|
|
|
case strings.HasPrefix(r.Path, "ssh://git@"):
|
|
|
|
return true
|
2014-02-09 14:24:13 +00:00
|
|
|
case strings.HasPrefix(r.Path, "https://github"):
|
2014-02-07 10:10:01 +00:00
|
|
|
return true
|
2014-02-09 14:24:13 +00:00
|
|
|
case strings.HasPrefix(r.Path, "http://github"):
|
2014-02-07 10:10:01 +00:00
|
|
|
return true
|
|
|
|
case strings.HasSuffix(r.Path, ".git"):
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// we could also ping the repository to check
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns commands that can be used in a Dockerfile
|
|
|
|
// to clone the repository.
|
|
|
|
//
|
|
|
|
// TODO we should also enable Mercurial projects and SVN projects
|
|
|
|
func (r *Repo) Commands() []string {
|
|
|
|
// get the branch. default to master
|
|
|
|
// if no branch exists.
|
|
|
|
branch := r.Branch
|
|
|
|
if len(branch) == 0 {
|
|
|
|
branch = "master"
|
2014-02-11 19:18:19 +00:00
|
|
|
}
|
2014-02-07 10:10:01 +00:00
|
|
|
|
|
|
|
cmds := []string{}
|
2014-02-12 17:58:07 +00:00
|
|
|
cmds = append(cmds, fmt.Sprintf("git clone --depth=%d --recursive --branch=%s %s %s", r.Depth, branch, r.Path, r.Dir))
|
2014-02-07 10:10:01 +00:00
|
|
|
|
|
|
|
switch {
|
|
|
|
// if a specific commit is provided then we'll
|
|
|
|
// need to clone it.
|
|
|
|
case len(r.PR) > 0:
|
|
|
|
|
|
|
|
cmds = append(cmds, fmt.Sprintf("git fetch origin +refs/pull/%s/head:refs/remotes/origin/pr/%s", r.PR, r.PR))
|
2014-03-02 13:03:05 +00:00
|
|
|
cmds = append(cmds, fmt.Sprintf("git checkout -qf -b pr/%s origin/pr/%s", r.PR, r.PR))
|
2014-02-07 10:10:01 +00:00
|
|
|
//cmds = append(cmds, fmt.Sprintf("git fetch origin +refs/pull/%s/merge:", r.PR))
|
|
|
|
//cmds = append(cmds, fmt.Sprintf("git checkout -qf %s", "FETCH_HEAD"))
|
|
|
|
// if a specific commit is provided then we'll
|
|
|
|
// need to clone it.
|
|
|
|
case len(r.Commit) > 0:
|
|
|
|
cmds = append(cmds, fmt.Sprintf("git checkout -qf %s", r.Commit))
|
|
|
|
}
|
|
|
|
|
|
|
|
return cmds
|
|
|
|
}
|