cmd/vulnreport: factor out git functions

Change-Id: I5ae3d19ceb6902649fc8eb28b8de1b66df32d5f0
Reviewed-on: https://go-review.googlesource.com/c/vulndb/+/499463
Run-TryBot: Tatiana Bradley <tatianabradley@google.com>
Reviewed-by: Damien Neil <dneil@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
Tatiana Bradley 2023-05-25 16:38:59 -04:00
Родитель f762043b08
Коммит 3ffc445bc4
2 изменённых файлов: 42 добавлений и 33 удалений

35
cmd/vulnreport/git.go Normal file
Просмотреть файл

@ -0,0 +1,35 @@
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"os"
"os/exec"
"golang.org/x/vulndb/internal/derrors"
)
func gitAdd(files ...string) (err error) {
derrors.Wrap(&err, "git add")
addArgs := []string{"add"}
return irun("git", append(addArgs, files...)...)
}
func gitCommit(msg string, files ...string) (err error) {
derrors.Wrap(&err, "git commit")
commitArgs := []string{"commit", "-m", msg, "-e"}
commitArgs = append(commitArgs, files...)
return irun("git", commitArgs...)
}
func irun(name string, arg ...string) error {
// Exec git commands rather than using go-git so as to run commit hooks
// and give the user a chance to edit the commit message.
cmd := exec.Command(name, arg...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}

Просмотреть файл

@ -363,8 +363,8 @@ func handleExcludedIssue(ctx context.Context, cfg *createCfg, iss *issues.Issue)
return "", fmt.Errorf("lint errors %s: %v", filename, lints)
}
if err := irun("git", "add", filename); err != nil {
return "", fmt.Errorf("git add %s: %v", filename, err)
if err := gitAdd(filename); err != nil {
return "", err
}
return r.ID, nil
}
@ -417,13 +417,7 @@ func createExcluded(ctx context.Context, cfg *createCfg) (err error) {
report.ExcludedDir,
strings.Join(successfulGoIDs, ", "),
strings.Join(successfulIssNums, "\nFixes "))
args := []string{"commit", "-m", msg, "-e"}
if err := irun("git", args...); err != nil {
return fmt.Errorf("git commit: %v", err)
}
return nil
return gitCommit(msg)
}
func newReport(ctx context.Context, cfg *createCfg, parsed *parsedIssue) (*report.Report, error) {
@ -914,16 +908,6 @@ func writeCVE(r *report.Report) error {
return database.WriteJSON(r.CVEFilename(), cve, true)
}
func irun(name string, arg ...string) error {
// Exec git commands rather than using go-git so as to run commit hooks
// and give the user a chance to edit the commit message.
cmd := exec.Command(name, arg...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
func commit(ctx context.Context, filename string, ghsaClient *ghsa.Client) (err error) {
defer derrors.Wrap(&err, "commit(%q)", filename)
@ -950,12 +934,9 @@ func commit(ctx context.Context, filename string, ghsaClient *ghsa.Client) (err
files = append(files, r.CVEFilename())
}
// Add the files.
addArgs := []string{"add"}
addArgs = append(addArgs, files...)
if err := irun("git", addArgs...); err != nil {
fmt.Fprintf(os.Stderr, "git add: %v\n", err)
return nil
// Add the files to git.
if err := gitAdd(files...); err != nil {
return err
}
// Commit the files, allowing the user to edit the default commit message.
@ -963,14 +944,7 @@ func commit(ctx context.Context, filename string, ghsaClient *ghsa.Client) (err
if err != nil {
return err
}
commitArgs := []string{"commit", "-m", msg, "-e"}
commitArgs = append(commitArgs, files...)
if err := irun("git", commitArgs...); err != nil {
fmt.Fprintf(os.Stderr, "git commit: %v\n", err)
return nil
}
return nil
return gitCommit(msg, files...)
}
func newCommitMsg(r *report.Report) (string, error) {