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