Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/dev/preview/previewctl/pkg/util/git.go
2501 views
1
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package util
6
7
import (
8
"os/exec"
9
10
"github.com/cockroachdb/errors"
11
)
12
13
var (
14
ErrBranchNotExist = errors.New("branch doesn't exist")
15
)
16
17
func BranchFromGit(branch string) (string, error) {
18
if branch == "" {
19
out, err := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD").Output()
20
if err != nil {
21
return "", errors.Wrap(err, "Could not retrieve branch name.")
22
}
23
24
branch = string(out)
25
} else {
26
_, err := exec.Command("git", "rev-parse", "--verify", branch).Output()
27
if err != nil {
28
return "", errors.CombineErrors(err, ErrBranchNotExist)
29
}
30
}
31
32
return branch, nil
33
}
34
35