Skip to content

Commit

Permalink
Merge pull request #1593 from rsteube/git-notes
Browse files Browse the repository at this point in the history
git: notes
  • Loading branch information
rsteube authored Mar 24, 2023
2 parents 8944d5d + 0da625c commit 4b648b2
Show file tree
Hide file tree
Showing 15 changed files with 306 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"github.com/rsteube/carapace"
"github.com/spf13/cobra"
)

Expand All @@ -12,6 +13,8 @@ var notesCmd = &cobra.Command{
}

func init() {
carapace.Gen(notesCmd).Standalone()

notesCmd.Flags().String("ref", "", "use notes from <notes-ref>")
rootCmd.AddCommand(notesCmd)
}
34 changes: 34 additions & 0 deletions completers/git_completer/cmd/notes_add.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package cmd

import (
"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/pkg/actions/tools/git"
"github.com/spf13/cobra"
)

var notes_addCmd = &cobra.Command{
Use: "add",
Short: "Add notes for a given object",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
carapace.Gen(notes_addCmd).Standalone()

notes_addCmd.Flags().Bool("allow-empty", false, "Allow an empty note object to be stored")
notes_addCmd.Flags().StringP("file", "F", "", "Take the note message from the given file")
notes_addCmd.Flags().StringP("message", "m", "", "Use the given note message")
notes_addCmd.Flags().StringP("reedit-message", "c", "", "Like -C, but with -c the editor is invoked")
notes_addCmd.Flags().StringP("reuse-message", "C", "", "Take the given blob object as the note message")
notesCmd.AddCommand(notes_addCmd)

carapace.Gen(notes_addCmd).FlagCompletion(carapace.ActionMap{
"file": carapace.ActionFiles(),
"reedit-message": git.ActionRefs(git.RefOption{}.Default()),
"reuse-message": git.ActionRefs(git.RefOption{}.Default()),
})

carapace.Gen(notes_addCmd).PositionalCompletion(
git.ActionRefs(git.RefOption{}.Default()),
)
}
35 changes: 35 additions & 0 deletions completers/git_completer/cmd/notes_append.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package cmd

import (
"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/pkg/actions/tools/git"
"github.com/spf13/cobra"
)

var notes_appendCmd = &cobra.Command{
Use: "append",
Short: "Append to the notes of an existing object",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
carapace.Gen(notes_appendCmd).Standalone()

notes_appendCmd.Flags().Bool("allow-empty", false, "Allow an empty note object to be stored")
notes_appendCmd.Flags().StringP("file", "F", "", "Take the note message from the given file")
notes_appendCmd.Flags().StringP("message", "m", "", "Use the given note message")
notes_appendCmd.Flags().StringP("reedit-message", "c", "", "Like -C, but with -c the editor is invoked")
notes_appendCmd.Flags().StringP("reuse-message", "C", "", "Take the given blob object as the note message")
notesCmd.AddCommand(notes_appendCmd)

carapace.Gen(notes_appendCmd).FlagCompletion(carapace.ActionMap{
"file": carapace.ActionFiles(),
"reedit-message": git.ActionRefs(git.RefOption{}.Default()),
"reuse-message": git.ActionRefs(git.RefOption{}.Default()),
})

carapace.Gen(notes_appendCmd).PositionalCompletion(
git.ActionRefs(git.RefOption{}.Default()),
)

}
26 changes: 26 additions & 0 deletions completers/git_completer/cmd/notes_copy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package cmd

import (
"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/pkg/actions/tools/git"
"github.com/spf13/cobra"
)

var notes_copyCmd = &cobra.Command{
Use: "copy",
Short: "Copy the notes for the first object onto the second object",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
carapace.Gen(notes_copyCmd).Standalone()

notes_copyCmd.Flags().BoolP("force", "f", false, "Overwrite existing notes")
notes_copyCmd.Flags().Bool("stdin", false, "Also read the object names to remove notes from the standard input")
notesCmd.AddCommand(notes_copyCmd)

carapace.Gen(notes_copyCmd).PositionalCompletion(
git.ActionRefs(git.RefOption{}.Default()),
git.ActionRefs(git.RefOption{}.Default()),
)
}
24 changes: 24 additions & 0 deletions completers/git_completer/cmd/notes_edit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package cmd

import (
"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/pkg/actions/tools/git"
"github.com/spf13/cobra"
)

var notes_editCmd = &cobra.Command{
Use: "edit",
Short: "Edit the notes for a given object",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
carapace.Gen(notes_editCmd).Standalone()

notes_editCmd.Flags().Bool("allow-empty", false, "Allow an empty note object to be stored")
notesCmd.AddCommand(notes_editCmd)

carapace.Gen(notes_editCmd).PositionalCompletion(
git.ActionRefs(git.RefOption{}.Default()),
)
}
18 changes: 18 additions & 0 deletions completers/git_completer/cmd/notes_getRef.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package cmd

import (
"github.com/rsteube/carapace"
"github.com/spf13/cobra"
)

var notes_getRefCmd = &cobra.Command{
Use: "get-ref",
Short: "Print the current notes ref",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
carapace.Gen(notes_getRefCmd).Standalone()

notesCmd.AddCommand(notes_getRefCmd)
}
23 changes: 23 additions & 0 deletions completers/git_completer/cmd/notes_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package cmd

import (
"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/pkg/actions/tools/git"
"github.com/spf13/cobra"
)

var notes_listCmd = &cobra.Command{
Use: "list",
Short: "List the notes object for a given object",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
carapace.Gen(notes_listCmd).Standalone()

notesCmd.AddCommand(notes_listCmd)

carapace.Gen(notes_listCmd).PositionalCompletion(
git.ActionRefs(git.RefOption{}.Default()),
)
}
34 changes: 34 additions & 0 deletions completers/git_completer/cmd/notes_merge.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package cmd

import (
"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/pkg/actions/tools/git"
"github.com/spf13/cobra"
)

var notes_mergeCmd = &cobra.Command{
Use: "merge",
Short: "Merge the given notes ref into the current notes ref",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
carapace.Gen(notes_mergeCmd).Standalone()

notes_mergeCmd.Flags().Bool("abort", false, "Abort/reset an in-progress git notes merge")
notes_mergeCmd.Flags().Bool("commit", false, "Finalize an in-progress git notes merge")
notes_mergeCmd.Flags().BoolP("quiet", "q", false, "When merging notes, operate quietly")
notes_mergeCmd.Flags().StringP("strategy", "s", "", "Resolve notes conflicts using the given strategy")
notes_mergeCmd.Flags().BoolP("verbose", "v", false, "When merging notes, be more verbose")
notesCmd.AddCommand(notes_mergeCmd)

notes_mergeCmd.MarkFlagsMutuallyExclusive("abort", "commit", "strategy")

carapace.Gen(notes_mergeCmd).FlagCompletion(carapace.ActionMap{
"strategy": git.ActionNotesMergeStrategies(),
})

carapace.Gen(notes_mergeCmd).PositionalCompletion(
git.ActionNotes(),
)
}
20 changes: 20 additions & 0 deletions completers/git_completer/cmd/notes_prune.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package cmd

import (
"github.com/rsteube/carapace"
"github.com/spf13/cobra"
)

var notes_pruneCmd = &cobra.Command{
Use: "prune",
Short: "Remove all notes for non-existing/unreachable objects",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
carapace.Gen(notes_pruneCmd).Standalone()

notes_pruneCmd.Flags().BoolP("dry-run", "n", false, "Do not remove anything, just report")
notes_pruneCmd.Flags().BoolP("verbose", "v", false, "When merging notes, be more verbose")
notesCmd.AddCommand(notes_pruneCmd)
}
25 changes: 25 additions & 0 deletions completers/git_completer/cmd/notes_remove.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package cmd

import (
"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/pkg/actions/tools/git"
"github.com/spf13/cobra"
)

var notes_removeCmd = &cobra.Command{
Use: "remove",
Short: "Remove the notes for given objects",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
carapace.Gen(notes_removeCmd).Standalone()

notes_removeCmd.Flags().Bool("ignore-missing", false, "Do not consider it an error to request removing non existing notes")
notes_removeCmd.Flags().Bool("stdin", false, "Also read the object names to remove notes from the standard input")
notesCmd.AddCommand(notes_removeCmd)

carapace.Gen(notes_removeCmd).PositionalAnyCompletion(
git.ActionRefs(git.RefOption{}.Default()),
)
}
23 changes: 23 additions & 0 deletions completers/git_completer/cmd/notes_show.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package cmd

import (
"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/pkg/actions/tools/git"
"github.com/spf13/cobra"
)

var notes_showCmd = &cobra.Command{
Use: "show",
Short: "Show the notes for a given object",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
carapace.Gen(notes_showCmd).Standalone()

notesCmd.AddCommand(notes_showCmd)

carapace.Gen(notes_showCmd).PositionalCompletion(
git.ActionRefs(git.RefOption{}.Default()),
)
}
6 changes: 6 additions & 0 deletions pkg/actions/tools/git/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type RefOption struct {
HeadCommits int
Tags bool
Stashes bool
Notes bool
}

func (o RefOption) Default() RefOption {
Expand All @@ -30,6 +31,7 @@ func (o RefOption) Default() RefOption {
o.HeadCommits = 100
o.Tags = true
o.Stashes = true
o.Notes = false
return o

}
Expand Down Expand Up @@ -66,6 +68,10 @@ func ActionRefs(refOption RefOption) carapace.Action {
batch = append(batch, ActionStashes())
}

if refOption.Notes {
batch = append(batch, ActionNotes())
}

return batch.ToA()

})
Expand Down
4 changes: 2 additions & 2 deletions pkg/actions/tools/git/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
// HEAD~1 (commit message)
func ActionHeadCommits(limit int) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
args := []string{"log", "--pretty=tformat:%h %<(64,trunc)%s", "--max-count", strconv.Itoa(limit)}
args := []string{"log", "--no-notes", "--pretty=tformat:%h %<(64,trunc)%s", "--max-count", strconv.Itoa(limit)}
return carapace.ActionExecCommand("git", args...)(func(output []byte) carapace.Action {
lines := strings.Split(string(output), "\n")

Expand All @@ -38,7 +38,7 @@ func ActionHeadCommits(limit int) carapace.Action {
// 123456B ((refname) commit message)
func ActionRecentCommits(limit int) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
args := []string{"log", "--all", "--no-merges", "--pretty=tformat:%h %<(64,trunc)%s", "--max-count", strconv.Itoa(limit)}
args := []string{"log", "--exclude=refs/notes/*", "--all", "--no-merges", "--pretty=tformat:%h %<(64,trunc)%s", "--max-count", strconv.Itoa(limit)}
return carapace.ActionExecCommand("git", args...)(func(output []byte) carapace.Action {
lines := strings.Split(string(output), "\n")

Expand Down
31 changes: 31 additions & 0 deletions pkg/actions/tools/git/note.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package git

import (
"strings"

"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/pkg/styles"
)

// ActionNotes completes notes

func ActionNotes() carapace.Action {
return carapace.ActionExecCommand("git", "log", "--pretty=format:%h\n%<(64,trunc)%s", "--show-notes")(func(output []byte) carapace.Action {
lines := strings.Split(string(output), "\n")
return carapace.ActionValuesDescribed(lines...).Style(styles.Git.Note)
}).Tag("notes")
}

// ActionNotesMergeStrategies completes notes merge strategies
//
// ours (favor the local version)
// theirs (favor the remote version)
func ActionNotesMergeStrategies() carapace.Action {
return carapace.ActionValuesDescribed(
"manual", "check out conflicting notes in a special work tree",
"ours", "favor the local version",
"theirs", "favor the remote version",
"union", "concatenate the local and remote versions",
"cat_sort_uniq", "similar to \"union\", but also sorts removes duplicate lines",
)
}
2 changes: 2 additions & 0 deletions pkg/styles/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ var Git = struct {
Branch string `desc:"git branches"`
Commit string `desc:"git commits"`
HeadCommit string `desc:"git HEAD~ commits"`
Note string `desc:"git notes"`
Stash string `desc:"git stashes"`
Tag string `desc:"git tags"`
}{
Branch: style.Blue,
Commit: style.Default,
HeadCommit: style.Bold,
Note: style.Cyan,
Stash: style.Green,
Tag: style.Yellow,
}
Expand Down

0 comments on commit 4b648b2

Please sign in to comment.