github.com/drone/runner-go@v1.12.0/clone/clone.go (about) 1 // Copyright 2019 Drone.IO Inc. All rights reserved. 2 // Use of this source code is governed by the Polyform License 3 // that can be found in the LICENSE file. 4 5 // Package clone provides utilities for cloning commits. 6 package clone 7 8 import "fmt" 9 10 // 11 // IMPORTANT: DO NOT MODIFY THIS FILE 12 // 13 // this file must not be changed unless the changes have been 14 // discussed and approved by the project maintainers in the 15 // GitHub issue tracker. 16 // 17 18 // Args provide arguments to clone a repository. 19 type Args struct { 20 Branch string 21 Commit string 22 Ref string 23 Remote string 24 Depth int 25 Tags bool 26 NoFF bool 27 } 28 29 // Commands returns posix-compliant commands to clone a 30 // repository and checkout a commit. 31 func Commands(args Args) []string { 32 switch { 33 case isTag(args.Ref): 34 return tag(args) 35 case isPullRequest(args.Ref): 36 return pull(args) 37 default: 38 return branch(args) 39 } 40 } 41 42 // branch returns posix-compliant commands to clone a repository 43 // and checkout the named branch. 44 func branch(args Args) []string { 45 return []string{ 46 "git init", 47 fmt.Sprintf("git remote add origin %s", args.Remote), 48 fmt.Sprintf("git fetch %s origin +refs/heads/%s:", fetchFlags(args), args.Branch), 49 fmt.Sprintf("git checkout %s -b %s", args.Commit, args.Branch), 50 } 51 } 52 53 // tag returns posix-compliant commands to clone a repository 54 // and checkout the tag by reference path. 55 func tag(args Args) []string { 56 return []string{ 57 "git init", 58 fmt.Sprintf("git remote add origin %s", args.Remote), 59 fmt.Sprintf("git fetch %s origin +%s:", fetchFlags(args), args.Ref), 60 "git checkout -qf FETCH_HEAD", 61 } 62 } 63 64 // pull returns posix-compliant commands to clone a repository 65 // and checkout the pull request by reference path. 66 func pull(args Args) []string { 67 return []string{ 68 "git init", 69 fmt.Sprintf("git remote add origin %s", args.Remote), 70 fmt.Sprintf("git fetch %s origin +refs/heads/%s:", fetchFlags(args), args.Branch), 71 fmt.Sprintf("git checkout %s", args.Branch), 72 fmt.Sprintf("git fetch origin %s:", args.Ref), 73 fmt.Sprintf("git merge %s %s", mergeFlags(args), args.Commit), 74 } 75 }