Skip to content

Commit

Permalink
Add support for git commit sha
Browse files Browse the repository at this point in the history
copying solution from tektoncd pipeline: tektoncd/pipeline#555
Credit to @dibbles on the original PR
  • Loading branch information
Shash Reddy committed Mar 25, 2019
1 parent dd3ceb3 commit 868fa95
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
17 changes: 13 additions & 4 deletions cmd/git-init/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,17 @@ func run(logger *zap.SugaredLogger, cmd string, args ...string) {
}
}

func runOrFail(logger *zap.SugaredLogger, cmd string, args ...string) {
func runOrFail(logger *zap.SugaredLogger, cmd string, args ...string) error {
c := exec.Command(cmd, args...)
var output bytes.Buffer
c.Stderr = &output
c.Stdout = &output

if err := c.Run(); err != nil {
logger.Fatalf("Unexpected error running %v %v: %v\n%v", cmd, args, err, output.String())
logger.Errorf("Unexpected error running %v %v: %v\n%v", cmd, args, err, output.String())
return err
}
return nil
}

func main() {
Expand Down Expand Up @@ -86,8 +88,15 @@ func main() {
}

run(logger, "git", "remote", "add", "origin", *url)
runOrFail(logger, "git", "fetch", "--depth=1", "--recurse-submodules=yes", "origin", *revision)
runOrFail(logger, "git", "reset", "--hard", "FETCH_HEAD")
err = runOrFail(logger, "git", "fetch", "--depth=1", "--recurse-submodules=yes", "origin", *revision)
if err != nil {
// Fetch can fail if an old commitid was used so try git pull, performing regardless of error
// as no guarantee that the same error is returned by all git servers gitlab, github etc...
run(logger, "git", "pull", "--recurse-submodules=yes", "origin")
runOrFail(logger, "git", "checkout", *revision)
} else {
runOrFail(logger, "git", "reset", "--hard", "FETCH_HEAD")
}

logger.Infof("Successfully cloned %q @ %q in path %q", *url, *revision, dir)
}
26 changes: 26 additions & 0 deletions test/git-source/1-commitid.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copyright 2018 The Knative Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
apiVersion: build.knative.dev/v1alpha1
kind: Build
metadata:
name: test-git-commitid
labels:
expect: succeeded
spec:
source:
git:
url: https://github.com/bazelbuild/rules_docker
revision: 4025a58c156a4ba0fd3606df50051423043495bf
template:
name: dump-workspace-template

0 comments on commit 868fa95

Please sign in to comment.