Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GitHub tagging and release creation automation using GitHub and Azure Pipelines APIs #40

Merged
merged 7 commits into from
Apr 29, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 60 additions & 53 deletions cmd/git-go-patch/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,83 +14,90 @@ import (

"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."
type applyCmd struct{}

const applyDescription = applySummary + `
func (a applyCmd) Name() string {
return "apply"
}

func (a applyCmd) Summary() string {
return "Apply patch files as one commit each using 'git apply' on top of HEAD."
}

func (a applyCmd) Description() string {
return `

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
}
func (a applyCmd) Handle(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.")

goDir := filepath.Join(rootDir, "go")
if err := p(); err != nil {
return err
}

// 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
}
}
rootDir, err := findOuterRepoRoot()
if err != nil {
return err
}

if !*noRefresh {
if err := submodule.Reset(rootDir, *force); err != nil {
return err
}
}
goDir := filepath.Join(rootDir, "go")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The extract, applyandrebase` commands have the following pattern:

        rootDir, err := findOuterRepoRoot()
	if err != nil {
		return err
	}

	goDir := filepath.Join(rootDir, "go")

Is it worth factoring that out?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see rootDir, goDir, err := findProjectRoots() working out.

I'd probably want to rename to projectDir, submoduleDir, err := findProjectRoots() to be more forward-looking, because I think we'll be generalizing the patching tools beyond our forks. The findProjectRoots function will then let us migrate off the hard-coded go subdir in one place (finding/reading a config file instead).

These lines are only touched by this PR to change the indentation, but it would be pretty hard to mess up a refactor like this, so I might as well while it's in mind.


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
}
dagood marked this conversation as resolved.
Show resolved Hide resolved
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