Skip to content

Commit 785826e

Browse files
authored
Merge pull request #3058 from carapace-sh/git-subtree
git: subtree
2 parents 6de0ef0 + ef9cbaf commit 785826e

File tree

6 files changed

+199
-0
lines changed

6 files changed

+199
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package cmd
2+
3+
import (
4+
"github.com/carapace-sh/carapace"
5+
"github.com/carapace-sh/carapace-bin/pkg/actions/os"
6+
"github.com/carapace-sh/carapace/pkg/traverse"
7+
"github.com/spf13/cobra"
8+
)
9+
10+
var subtreeCmd = &cobra.Command{
11+
Use: "subtree",
12+
Short: "Merge subtrees together and split repository into subtrees",
13+
Run: func(cmd *cobra.Command, args []string) {},
14+
}
15+
16+
func init() {
17+
carapace.Gen(subtreeCmd).Standalone()
18+
19+
subtreeCmd.PersistentFlags().BoolP("debug", "d", false, "Produce even more unnecessary output messages on stderr")
20+
subtreeCmd.PersistentFlags().StringP("gpg-sign", "S", "", "GPG-sign commits")
21+
subtreeCmd.PersistentFlags().Bool("no-gpg-sign", false, "do not GPG-sign commits")
22+
subtreeCmd.PersistentFlags().StringP("prefix", "P", "", "Specify the path in the repository to the subtree you want to manipulate")
23+
subtreeCmd.PersistentFlags().BoolP("quiet", "q", false, "Suppress unnecessary output messages on stderr")
24+
rootCmd.AddCommand(subtreeCmd)
25+
26+
subtreeCmd.Flag("gpg-sign").NoOptDefVal = " "
27+
28+
carapace.Gen(subtreeCmd).FlagCompletion(carapace.ActionMap{
29+
"gpg-sign": os.ActionGpgKeyIds(),
30+
"prefix": carapace.ActionDirectories().ChdirF(traverse.GitWorkTree), // TODO verify/fix
31+
})
32+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package cmd
2+
3+
import (
4+
"strings"
5+
6+
"github.com/carapace-sh/carapace"
7+
"github.com/carapace-sh/carapace-bin/pkg/actions/tools/git"
8+
"github.com/spf13/cobra"
9+
)
10+
11+
var subtree_addCmd = &cobra.Command{
12+
Use: "add",
13+
Short: "Create the <prefix> subtree",
14+
Run: func(cmd *cobra.Command, args []string) {},
15+
}
16+
17+
func init() {
18+
carapace.Gen(subtree_addCmd).Standalone()
19+
20+
subtree_addCmd.Flags().StringP("message", "m", "", "Specify <message> as the commit message for the merge commit")
21+
subtree_addCmd.Flags().Bool("squash", false, "produce only a single commit that contains all the differences")
22+
subtreeCmd.AddCommand(subtree_addCmd)
23+
24+
carapace.Gen(subtree_addCmd).PositionalCompletion(
25+
carapace.Batch(
26+
git.ActionRefs(git.RefOption{}.Default()),
27+
carapace.ActionValues("https://").NoSpace('/').UnlessF(func(c carapace.Context) bool {
28+
return strings.HasPrefix(c.Value, "https://")
29+
}),
30+
git.ActionRepositorySearch(git.SearchOpts{}.Default()).UnlessF(func(c carapace.Context) bool {
31+
return !strings.HasPrefix(c.Value, "https://")
32+
}),
33+
).ToA(),
34+
carapace.ActionCallback(func(c carapace.Context) carapace.Action {
35+
if !strings.HasPrefix(c.Args[0], "https://") {
36+
return carapace.ActionValues()
37+
}
38+
return git.ActionLsRemoteRefs(git.LsRemoteRefOption{Url: c.Args[0], Branches: true, Tags: true})
39+
}),
40+
)
41+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package cmd
2+
3+
import (
4+
"github.com/carapace-sh/carapace"
5+
"github.com/carapace-sh/carapace-bin/pkg/actions/tools/git"
6+
"github.com/spf13/cobra"
7+
)
8+
9+
var subtree_mergeCmd = &cobra.Command{
10+
Use: "merge <local-commit> [<repository>]",
11+
Short: "Merge recent changes up to <local-commit> into the <prefix> subtree",
12+
Run: func(cmd *cobra.Command, args []string) {},
13+
}
14+
15+
func init() {
16+
carapace.Gen(subtree_mergeCmd).Standalone()
17+
18+
subtree_mergeCmd.Flags().StringP("message", "m", "", "Specify <message> as the commit message for the merge commit")
19+
subtree_mergeCmd.Flags().Bool("squash", false, "produce only a single commit that contains all the differences")
20+
subtreeCmd.AddCommand(subtree_mergeCmd)
21+
22+
carapace.Gen(subtree_mergeCmd).PositionalCompletion(
23+
git.ActionRefs(git.RefOption{}.Default()),
24+
git.ActionRepositorySearch(git.SearchOpts{}.Default()),
25+
)
26+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package cmd
2+
3+
import (
4+
"github.com/carapace-sh/carapace"
5+
"github.com/carapace-sh/carapace-bin/pkg/actions/tools/git"
6+
"github.com/spf13/cobra"
7+
)
8+
9+
var subtree_pullCmd = &cobra.Command{
10+
Use: "pull <repository> <remote-ref>",
11+
Short: "Exactly like merge, but parallels git pull",
12+
Run: func(cmd *cobra.Command, args []string) {},
13+
}
14+
15+
func init() {
16+
carapace.Gen(subtree_pullCmd).Standalone()
17+
18+
subtree_pullCmd.Flags().StringP("message", "m", "", "Specify <message> as the commit message for the merge commit")
19+
subtree_pullCmd.Flags().Bool("squash", false, "produce only a single commit that contains all the differences")
20+
subtreeCmd.AddCommand(subtree_pullCmd)
21+
22+
carapace.Gen(subtree_pullCmd).PositionalCompletion(
23+
git.ActionRepositorySearch(git.SearchOpts{}.Default()),
24+
carapace.ActionCallback(func(c carapace.Context) carapace.Action {
25+
return git.ActionLsRemoteRefs(git.LsRemoteRefOption{Url: c.Args[0], Branches: true, Tags: true})
26+
}),
27+
)
28+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package cmd
2+
3+
import (
4+
"github.com/carapace-sh/carapace"
5+
"github.com/carapace-sh/carapace-bin/pkg/actions/tools/git"
6+
"github.com/spf13/cobra"
7+
)
8+
9+
var subtree_pushCmd = &cobra.Command{
10+
Use: "push <repository> [+][<local-commit>:]<remote-ref>",
11+
Short: "Push your subtree to different branches of the remote repository",
12+
Run: func(cmd *cobra.Command, args []string) {},
13+
}
14+
15+
func init() {
16+
carapace.Gen(subtree_pushCmd).Standalone()
17+
18+
subtree_pushCmd.Flags().String("annotate", "", "add <annotation> as a prefix to each commit message")
19+
subtree_pushCmd.Flags().StringP("branch", "b", "", "create a new branch called <branch> that contains the new history")
20+
subtree_pushCmd.Flags().Bool("ignore-joins", false, "regenerate the entire history")
21+
subtree_pushCmd.Flags().StringP("message", "m", "", "Specify <message> as the commit message for the merge commit")
22+
subtree_pushCmd.Flags().String("onto", "", "specify the commit ID <onto> to build history from")
23+
subtree_pushCmd.Flags().Bool("rejoin", false, "merge the newly created synthetic history back into your main project")
24+
subtreeCmd.AddCommand(subtree_pushCmd)
25+
26+
// TODO check and fix completion
27+
carapace.Gen(subtree_pushCmd).PositionalCompletion(
28+
git.ActionRemotes(),
29+
carapace.ActionMultiPartsN(":", 2, func(c carapace.Context) carapace.Action {
30+
switch len(c.Parts) {
31+
case 0:
32+
return git.ActionRefs(git.RefOption{
33+
LocalBranches: true,
34+
HeadCommits: true,
35+
Tags: true,
36+
}).NoSpace()
37+
default:
38+
return git.ActionRemoteBranchNames(c.Args[0])
39+
}
40+
}),
41+
)
42+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package cmd
2+
3+
import (
4+
"github.com/carapace-sh/carapace"
5+
"github.com/carapace-sh/carapace-bin/pkg/actions/tools/git"
6+
"github.com/spf13/cobra"
7+
)
8+
9+
var subtree_splitCmd = &cobra.Command{
10+
Use: "split [<local-commit>] [<repository>]",
11+
Short: "Extract a new, synthetic project history",
12+
Run: func(cmd *cobra.Command, args []string) {},
13+
}
14+
15+
func init() {
16+
carapace.Gen(subtree_splitCmd).Standalone()
17+
18+
subtree_splitCmd.Flags().String("annotate", "", "add <annotation> as a prefix to each commit message")
19+
subtree_splitCmd.Flags().StringP("branch", "b", "", "create a new branch called <branch> that contains the new history")
20+
subtree_splitCmd.Flags().Bool("ignore-joins", false, "regenerate the entire history")
21+
subtree_splitCmd.Flags().StringP("message", "m", "", "Specify <message> as the commit message for the merge commit")
22+
subtree_splitCmd.Flags().String("onto", "", "specify the commit ID <onto> to build history from")
23+
subtree_splitCmd.Flags().Bool("rejoin", false, "merge the newly created synthetic history back into your main project")
24+
subtreeCmd.AddCommand(subtree_splitCmd)
25+
26+
carapace.Gen(subtree_splitCmd).PositionalCompletion(
27+
git.ActionRefs(git.RefOption{}.Default()),
28+
git.ActionRepositorySearch(git.SearchOpts{}.Default()), // TODO verify
29+
)
30+
}

0 commit comments

Comments
 (0)