Skip to content

Commit

Permalink
Merge pull request #62 from yosssi/add-git-depth-option
Browse files Browse the repository at this point in the history
Implemented a function for developers to specify the `--depth` option of the `git clone` command
  • Loading branch information
bradrydzewski committed Feb 13, 2014
2 parents 901d48e + 074de9d commit bb90a49
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 3 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,15 @@ notify:
on_failure: true
```

### Git Command Options

You can specify the `--depth` option of the `git clone` command (Defalut value is `50`):

```
git:
depth: 1
```

### Docs

Coming Soon to [drone.readthedocs.org](http:https://drone.readthedocs.org/)
Expand Down
22 changes: 22 additions & 0 deletions pkg/build/git/git.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package git

const (
DefaultGitDepth = 50
)

// Git stores the configuration details for
// executing Git commands.
type Git struct {
Depth *int `yaml:"depth,omitempty"`
}

// GitDepth returns GitDefaultDepth
// when Git.Depth is empty.
// GitDepth returns Git.Depth
// when it is not empty.
func GitDepth(g *Git) int {
if g == nil || g.Depth == nil {
return DefaultGitDepth
}
return *g.Depth
}
40 changes: 40 additions & 0 deletions pkg/build/git/git_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package git

import (
"testing"
)

func TestGitDepth(t *testing.T) {
var g *Git
var expected int

expected = DefaultGitDepth
g = nil
if actual := GitDepth(g); actual != expected {
t.Errorf("The result is invalid. [expected: %d][actual: %d]", expected, actual)
}

expected = DefaultGitDepth
g = &Git{}
if actual := GitDepth(g); actual != expected {
t.Errorf("The result is invalid. [expected: %d][actual: %d]", expected, actual)
}

expected = DefaultGitDepth
g = &Git{Depth: nil}
if actual := GitDepth(g); actual != expected {
t.Errorf("The result is invalid. [expected: %d][actual: %d]", expected, actual)
}

expected = 0
g = &Git{Depth: &expected}
if actual := GitDepth(g); actual != expected {
t.Errorf("The result is invalid. [expected: %d][actual: %d]", expected, actual)
}

expected = 1
g = &Git{Depth: &expected}
if actual := GitDepth(g); actual != expected {
t.Errorf("The result is invalid. [expected: %d][actual: %d]", expected, actual)
}
}
6 changes: 4 additions & 2 deletions pkg/build/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ type Repo struct {
// will be cloned into (or copied to) inside the
// host system (Docker Container).
Dir string

// (optional) The depth of the `git clone` command.
Depth int
}

// IsRemote returns true if the Repository is located
Expand Down Expand Up @@ -88,7 +91,6 @@ func (r *Repo) IsGit() bool {
//
// 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
Expand All @@ -97,7 +99,7 @@ func (r *Repo) Commands() []string {
}

cmds := []string{}
cmds = append(cmds, fmt.Sprintf("git clone --recursive --branch=%s %s %s", branch, r.Path, r.Dir))
cmds = append(cmds, fmt.Sprintf("git clone --depth=%d --recursive --branch=%s %s %s", r.Depth, branch, r.Path, r.Dir))

switch {
// if a specific commit is provided then we'll
Expand Down
2 changes: 2 additions & 0 deletions pkg/build/script/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"launchpad.net/goyaml"

"github.com/drone/drone/pkg/build/buildfile"
"github.com/drone/drone/pkg/build/git"
"github.com/drone/drone/pkg/plugin/deploy"
"github.com/drone/drone/pkg/plugin/notify"
"github.com/drone/drone/pkg/plugin/publish"
Expand Down Expand Up @@ -54,6 +55,7 @@ type Build struct {
Deploy *deploy.Deploy `yaml:"deploy,omitempty"`
Publish *publish.Publish `yaml:"publish,omitempty"`
Notifications *notify.Notification `yaml:"notify,omitempty"`
Git *git.Git `yaml:"git,omitempty"`
}

// Write adds all the steps to the build script, including
Expand Down
3 changes: 2 additions & 1 deletion pkg/queue/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
bldr "github.com/drone/drone/pkg/build"
"github.com/drone/drone/pkg/build/git"
r "github.com/drone/drone/pkg/build/repo"
"github.com/drone/drone/pkg/build/script"
"github.com/drone/drone/pkg/channel"
Expand Down Expand Up @@ -135,7 +136,7 @@ func (b *BuildTask) execute() error {
// execute the build
builder := bldr.Builder{}
builder.Build = b.Script
builder.Repo = &r.Repo{Path: b.Repo.URL, Branch: b.Commit.Branch, Commit: b.Commit.Hash, PR: b.Commit.PullRequest, Dir: filepath.Join("/var/cache/drone/src", b.Repo.Slug)}
builder.Repo = &r.Repo{Path: b.Repo.URL, Branch: b.Commit.Branch, Commit: b.Commit.Hash, PR: b.Commit.PullRequest, Dir: filepath.Join("/var/cache/drone/src", b.Repo.Slug), Depth: git.GitDepth(b.Script.Git)}
builder.Key = []byte(b.Repo.PrivateKey)
builder.Stdout = buf
builder.Timeout = 300 * time.Minute
Expand Down

0 comments on commit bb90a49

Please sign in to comment.