Skip to content

Commit

Permalink
feat(git): Allow customizing the label applied based on the repo state.
Browse files Browse the repository at this point in the history
  • Loading branch information
jwalton committed Feb 2, 2022
1 parent 2f1343c commit dd4cb33
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 7 deletions.
6 changes: 6 additions & 0 deletions internal/gitutils/head_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func TestHeadOnBranch(t *testing.T) {
Description: "master",
Detached: false,
Hash: "7c088a39dcd2dcda89f4dee1fd3eb41c1d34ea2f",
IsTag: false,
},
state,
)
Expand All @@ -50,6 +51,7 @@ func TestHeadDetached(t *testing.T) {
Description: "(0123456...)",
Detached: true,
Hash: "0123456789abcdef0123456789abcdef01234567",
IsTag: false,
},
state,
)
Expand Down Expand Up @@ -77,6 +79,7 @@ func TestHeadOnTag(t *testing.T) {
Description: "(v1.0.0)",
Detached: true,
Hash: "0123456789abcdef0123456789abcdef01234567",
IsTag: true,
},
state,
)
Expand All @@ -89,6 +92,7 @@ func TestHeadOnTag(t *testing.T) {
Description: "(0123456...)",
Detached: true,
Hash: "0123456789abcdef0123456789abcdef01234567",
IsTag: false,
},
state,
)
Expand Down Expand Up @@ -123,6 +127,7 @@ func TestHeadOnAnnotatedTag(t *testing.T) {
Description: "(v1.1.0)",
Detached: true,
Hash: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
IsTag: true,
},
state,
)
Expand All @@ -147,6 +152,7 @@ func TestHeadOnPackedRefs(t *testing.T) {
Description: "(v1.0.0)",
Detached: true,
Hash: "0123456789abcdef0123456789abcdef01234567",
IsTag: true,
},
state,
)
Expand Down
65 changes: 59 additions & 6 deletions internal/kitsch/modules/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,30 @@ type GitModule struct {
// MaxTagsToSearch is the maximum number of tags to search when checking to
// see if HEAD is a tagged release. Defaults to 200.
MaxTagsToSearch int `yaml:"maxTagsToSearch"`

// RebasingInteractive is a description to show when an interactive rebase in in progress.
RebasingInteractive string `yaml:"rebaseInteractive"`
// RebaseMerging is a description to show when a merge in in progress.
RebaseMerging string `yaml:"rebaseMerging"`
// Rebasing is a description to show when a rebase operation in in progress.
Rebasing string `yaml:"rebasing"`
// AMing is a description to show when an `am` operation in in progress.
AMing string `yaml:"aming"`
// RebaseAMing is a description to show when an ambiguous apply-mailbox or rebase is in progress.
RebaseAMing string `yaml:"rebaseAMing"`
// Merging is a description to show when a merge in in progress.
Merging string `yaml:"merging"`
// CherryPicking is a description to show when a cherry-pick in in progress.
CherryPicking string `yaml:"cherryPicking"`
// Reverting is a description to show when a revert in in progress.
Reverting string `yaml:"reverting"`
// Bisecting is a description to show when a bisect in in progress.
Bisecting string `yaml:"bisecting"`
}

type gitResult struct {
// State is the current state of this repo.
State gitutils.RepositoryStateType `yaml:"state"`
State string `yaml:"state"`
// Step is the current step number if we are rebasing, 0 otherwise.
Step string `yaml:"step"`
// Total is the total number of steps to complete to finish the rebase, or 0
Expand Down Expand Up @@ -93,7 +112,7 @@ func (mod GitModule) Execute(context *Context) ModuleResult {
}

data := gitResult{
State: state.State,
State: mod.getStateDescription(state.State),
Step: state.Step,
Total: state.Total,
Head: head,
Expand All @@ -110,6 +129,31 @@ func (mod GitModule) Execute(context *Context) ModuleResult {
}
}

func (mod GitModule) getStateDescription(state gitutils.RepositoryStateType) string {
switch state {
case gitutils.StateRebasingInteractive:
return mod.RebasingInteractive
case gitutils.StateRebaseMerging:
return mod.RebaseMerging
case gitutils.StateRebasing:
return mod.Rebasing
case gitutils.StateAMing:
return mod.AMing
case gitutils.StateRebaseAMing:
return mod.RebaseAMing
case gitutils.StateMerging:
return mod.Merging
case gitutils.StateCherryPicking:
return mod.CherryPicking
case gitutils.StateReverting:
return mod.Reverting
case gitutils.StateBisecting:
return mod.Bisecting
default:
return string(state)
}
}

func (mod GitModule) renderDefault(
context *Context,
symbol string,
Expand All @@ -129,8 +173,8 @@ func (mod GitModule) renderDefault(
out.WriteString(" " + symbol)
}

if data.State != gitutils.StateNone {
out.WriteString("|" + string(data.State))
if data.State != "" {
out.WriteString("|" + data.State)
if data.Total != "" {
out.WriteString(fmt.Sprintf(" %s/%s", data.Step, data.Total))
}
Expand All @@ -146,8 +190,17 @@ func init() {
jsonSchema: schemas.GitModuleJSONSchema,
factory: func(node *yaml.Node) (Module, error) {
module := GitModule{
Type: "git",
MaxTagsToSearch: 200,
Type: "git",
MaxTagsToSearch: 200,
RebasingInteractive: "REBASE-i",
RebaseMerging: "REBASE-m",
Rebasing: "REBASE",
AMing: "AM",
RebaseAMing: "REBASE/AM",
Merging: "MERGING",
CherryPicking: "CHERRY-PICKING",
Reverting: "REVERTING",
Bisecting: "BISECTING",
}
err := node.Decode(&module)
return &module, err
Expand Down
11 changes: 10 additions & 1 deletion internal/kitsch/modules/schemas/git_schema.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit dd4cb33

Please sign in to comment.