xref: /aosp_15_r20/external/bazelbuild-rules_go/go/tools/releaser/git.go (revision 9bb1b549b6a84214c53be0924760be030e66b93a)
1*9bb1b549SSpandan Das// Copyright 2021 The Bazel Authors. All rights reserved.
2*9bb1b549SSpandan Das//
3*9bb1b549SSpandan Das// Licensed under the Apache License, Version 2.0 (the "License");
4*9bb1b549SSpandan Das// you may not use this file except in compliance with the License.
5*9bb1b549SSpandan Das// You may obtain a copy of the License at
6*9bb1b549SSpandan Das//
7*9bb1b549SSpandan Das//    http://www.apache.org/licenses/LICENSE-2.0
8*9bb1b549SSpandan Das//
9*9bb1b549SSpandan Das// Unless required by applicable law or agreed to in writing, software
10*9bb1b549SSpandan Das// distributed under the License is distributed on an "AS IS" BASIS,
11*9bb1b549SSpandan Das// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*9bb1b549SSpandan Das// See the License for the specific language governing permissions and
13*9bb1b549SSpandan Das// limitations under the License.
14*9bb1b549SSpandan Das
15*9bb1b549SSpandan Daspackage main
16*9bb1b549SSpandan Das
17*9bb1b549SSpandan Dasimport (
18*9bb1b549SSpandan Das	"context"
19*9bb1b549SSpandan Das	"errors"
20*9bb1b549SSpandan Das)
21*9bb1b549SSpandan Das
22*9bb1b549SSpandan Dasfunc checkNoGitChanges(ctx context.Context, dir string) error {
23*9bb1b549SSpandan Das	out, err := runForOutput(ctx, dir, "git", "status", "--porcelain", "--untracked-files=no")
24*9bb1b549SSpandan Das	if err != nil {
25*9bb1b549SSpandan Das		return err
26*9bb1b549SSpandan Das	}
27*9bb1b549SSpandan Das	if len(out) > 0 {
28*9bb1b549SSpandan Das		return errors.New("Repository has pending changes. Commit them and try again.")
29*9bb1b549SSpandan Das	}
30*9bb1b549SSpandan Das	return nil
31*9bb1b549SSpandan Das}
32*9bb1b549SSpandan Das
33*9bb1b549SSpandan Dasfunc gitBranchExists(ctx context.Context, dir, branchName string) bool {
34*9bb1b549SSpandan Das	err := runForError(ctx, dir, "git", "show-ref", "--verify", "--quiet", "refs/heads/"+branchName)
35*9bb1b549SSpandan Das	return err == nil
36*9bb1b549SSpandan Das}
37*9bb1b549SSpandan Das
38*9bb1b549SSpandan Dasfunc gitCreateBranch(ctx context.Context, dir, branchName, refName string) error {
39*9bb1b549SSpandan Das	return runForError(ctx, dir, "git", "branch", branchName, refName)
40*9bb1b549SSpandan Das}
41*9bb1b549SSpandan Das
42*9bb1b549SSpandan Dasfunc gitPushBranch(ctx context.Context, dir, branchName string) error {
43*9bb1b549SSpandan Das	return runForError(ctx, dir, "git", "push", "origin", branchName)
44*9bb1b549SSpandan Das}
45*9bb1b549SSpandan Das
46*9bb1b549SSpandan Dasfunc gitCreateArchive(ctx context.Context, dir, branchName, arcName string) error {
47*9bb1b549SSpandan Das	return runForError(ctx, dir, "git", "archive", "--output="+arcName, branchName)
48*9bb1b549SSpandan Das}
49*9bb1b549SSpandan Das
50*9bb1b549SSpandan Dasfunc gitCatFile(ctx context.Context, dir, refName, fileName string) ([]byte, error) {
51*9bb1b549SSpandan Das	return runForOutput(ctx, dir, "git", "cat-file", "blob", refName+":"+fileName)
52*9bb1b549SSpandan Das}
53