Skip to content

Commit

Permalink
Add GitHub tagging and release creation automation using GitHub and A…
Browse files Browse the repository at this point in the history
…zure Pipelines APIs (#40)

* Switch to interface-based subcommand system

* Add GitHub tagging and release creation automation

* Clarify reason for defaultless goReleaseConfigVariableGroup

* Update cmd/git-go-patch/apply.go

Co-authored-by: Jared Parsons <jaredpparsons@gmail.com>

* Change Option to struct, merge in OptionArgTaker

* Change set to set-azdo-variable

* Centralize goDir calculation in findProjectRoots

Co-authored-by: Jared Parsons <jaredpparsons@gmail.com>
  • Loading branch information
dagood and jaredpar committed Apr 29, 2022
1 parent 4fb9273 commit c492dea
Show file tree
Hide file tree
Showing 18 changed files with 1,527 additions and 343 deletions.
114 changes: 57 additions & 57 deletions cmd/git-go-patch/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,88 +9,88 @@ import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"

"github.com/microsoft/go-infra/executil"
"github.com/microsoft/go-infra/patch"
"github.com/microsoft/go-infra/subcmd"
"github.com/microsoft/go-infra/submodule"
)

const applySummary = "Apply patch files as one commit each using 'git apply' on top of HEAD."

const applyDescription = applySummary + `
func init() {
subcommands = append(subcommands, subcmd.Option{
Name: "apply",
Summary: "Apply patch files as one commit each using 'git apply' on top of HEAD.",
Description: `
This command also records the state of the repository before applying patches, so "extract" can be used
later to create patch files after adding more commits, or altering the patch commits.
apply uses "git am", internally. If patches fail to apply, use "git am" inside the submodule to
resolve and continue the patch application process.
` + repoRootSearchDescription

var apply = subcommand{
Name: "apply",
Summary: applySummary,
Handle: func() error {
force := flag.Bool("f", false, "Force reapply: throw away changes in the submodule.")
noRefresh := flag.Bool(
"no-refresh",
false,
"Skip the submodule refresh (reset, clean, checkout) that happens before applying patches.\n"+
"This may be useful for advanced workflows.")

if err := parseFlagArgs(applyDescription); err != nil {
return err
}

rootDir, err := findOuterRepoRoot()
if err != nil {
return err
}
` + repoRootSearchDescription,
Handle: handleApply,
})
}

goDir := filepath.Join(rootDir, "go")
func handleApply(p subcmd.ParseFunc) error {
force := flag.Bool("f", false, "Force reapply: throw away changes in the submodule.")
noRefresh := flag.Bool(
"no-refresh",
false,
"Skip the submodule refresh (reset, clean, checkout) that happens before applying patches.\n"+
"This may be useful for advanced workflows.")

// If we're being careful, abort if the submodule commit isn't what we expect.
if !*force {
if err := ensureSubmoduleCommitNotDirty(rootDir, goDir); err != nil {
return err
}
}
if err := p(); err != nil {
return err
}

if !*noRefresh {
if err := submodule.Reset(rootDir, *force); err != nil {
return err
}
}
rootDir, goDir, err := findProjectRoots()
if err != nil {
return err
}

prePatchHead, err := getCurrentCommit(goDir)
if err != nil {
// If we're being careful, abort if the submodule commit isn't what we expect.
if !*force {
if err := ensureSubmoduleCommitNotDirty(rootDir, goDir); err != nil {
return err
}
}

// Record the pre-patch commit. We must do this before applying the patch: if patching
// fails, the user needs to be able to fix up the patches inside the submodule and then run
// "git go-patch extract" to apply the fixes to the patch files. "extract" depends on the
// pre-patch status file. Start by ensuring the dir exists, then write the file.
if err := os.MkdirAll(getStatusFileDir(rootDir), os.ModePerm); err != nil {
return err
}
if err := writeStatusFiles(prePatchHead, getPrePatchStatusFilePath(rootDir)); err != nil {
if !*noRefresh {
if err := submodule.Reset(rootDir, *force); err != nil {
return err
}
}

if err := patch.Apply(rootDir, patch.ApplyModeCommits); err != nil {
return err
}
prePatchHead, err := getCurrentCommit(goDir)
if err != nil {
return err
}

postPatchHead, err := getCurrentCommit(goDir)
if err != nil {
return err
}
// Record the pre-patch commit. We must do this before applying the patch: if patching
// fails, the user needs to be able to fix up the patches inside the submodule and then run
// "git go-patch extract" to apply the fixes to the patch files. "extract" depends on the
// pre-patch status file. Start by ensuring the dir exists, then write the file.
if err := os.MkdirAll(getStatusFileDir(rootDir), os.ModePerm); err != nil {
return err
}

if err := writeStatusFiles(prePatchHead, getPrePatchStatusFilePath(rootDir)); err != nil {
return err
}

if err := patch.Apply(rootDir, patch.ApplyModeCommits); err != nil {
return err
}

postPatchHead, err := getCurrentCommit(goDir)
if err != nil {
return err
}

// Record the post-patch commit.
return writeStatusFiles(postPatchHead, getPostPatchStatusFilePath(rootDir))
},
// Record the post-patch commit.
return writeStatusFiles(postPatchHead, getPostPatchStatusFilePath(rootDir))
}

func writeStatusFiles(commit string, file string) error {
Expand Down
Loading

0 comments on commit c492dea

Please sign in to comment.