github.com/marinho/drone@v0.2.1-0.20140504195434-d3ba962e89a7/pkg/build/repo/repo.go (about) 1 package repo 2 3 import ( 4 "fmt" 5 "strings" 6 ) 7 8 type Repo struct { 9 // The name of the Repository. This should be the 10 // canonical name, for example, github.com/drone/drone. 11 Name string 12 13 // The path of the Repoisotry. This could be 14 // the remote path of a Git repository or the path of 15 // of the repository on the local file system. 16 // 17 // A remote path must start with http://, https://, 18 // git://, ssh:// or git@. Otherwise we'll assume 19 // the repository is located on the local filesystem. 20 Path string 21 22 // (optional) Specific Branch that we should checkout 23 // when the Repository is cloned. If no value is 24 // provided we'll assume the default, master branch. 25 Branch string 26 27 // (optional) Specific Commit Hash that we should 28 // checkout when the Repository is cloned. If no 29 // value is provided we'll assume HEAD. 30 Commit string 31 32 // (optional) Pull Request number that we should 33 // checkout when the Repository is cloned. 34 PR string 35 36 // (optional) The filesystem path that the repository 37 // will be cloned into (or copied to) inside the 38 // host system (Docker Container). 39 Dir string 40 41 // (optional) The depth of the `git clone` command. 42 Depth int 43 } 44 45 // IsRemote returns true if the Repository is located 46 // on a remote server (ie Github, Bitbucket) 47 func (r *Repo) IsRemote() bool { 48 switch { 49 case strings.HasPrefix(r.Path, "git://"): 50 return true 51 case strings.HasPrefix(r.Path, "git@"): 52 return true 53 case strings.HasPrefix(r.Path, "http://"): 54 return true 55 case strings.HasPrefix(r.Path, "https://"): 56 return true 57 case strings.HasPrefix(r.Path, "ssh://"): 58 return true 59 } 60 61 return false 62 } 63 64 // IsLocal returns true if the Repository is located 65 // on the local filesystem. 66 func (r *Repo) IsLocal() bool { 67 return !r.IsRemote() 68 } 69 70 // IsGit returns true if the Repository is 71 // a Git repoisitory. 72 func (r *Repo) IsGit() bool { 73 switch { 74 case strings.HasPrefix(r.Path, "git://"): 75 return true 76 case strings.HasPrefix(r.Path, "git@"): 77 return true 78 case strings.HasPrefix(r.Path, "ssh://git@"): 79 return true 80 case strings.HasPrefix(r.Path, "https://github"): 81 return true 82 case strings.HasPrefix(r.Path, "http://github"): 83 return true 84 case strings.HasSuffix(r.Path, ".git"): 85 return true 86 } 87 88 // we could also ping the repository to check 89 90 return false 91 } 92 93 // returns commands that can be used in a Dockerfile 94 // to clone the repository. 95 // 96 // TODO we should also enable Mercurial projects and SVN projects 97 func (r *Repo) Commands() []string { 98 // get the branch. default to master 99 // if no branch exists. 100 branch := r.Branch 101 if len(branch) == 0 { 102 branch = "master" 103 } 104 105 cmds := []string{} 106 cmds = append(cmds, fmt.Sprintf("git clone --depth=%d --recursive --branch=%s %s %s", r.Depth, branch, r.Path, r.Dir)) 107 108 switch { 109 // if a specific commit is provided then we'll 110 // need to clone it. 111 case len(r.PR) > 0: 112 113 cmds = append(cmds, fmt.Sprintf("git fetch origin +refs/pull/%s/head:refs/remotes/origin/pr/%s", r.PR, r.PR)) 114 cmds = append(cmds, fmt.Sprintf("git checkout -qf -b pr/%s origin/pr/%s", r.PR, r.PR)) 115 //cmds = append(cmds, fmt.Sprintf("git fetch origin +refs/pull/%s/merge:", r.PR)) 116 //cmds = append(cmds, fmt.Sprintf("git checkout -qf %s", "FETCH_HEAD")) 117 // if a specific commit is provided then we'll 118 // need to clone it. 119 case len(r.Commit) > 0: 120 cmds = append(cmds, fmt.Sprintf("git checkout -qf %s", r.Commit)) 121 } 122 123 return cmds 124 }