@@ -19,6 +19,7 @@ import (
19
19
"os"
20
20
21
21
"github.com/pfnet-research/git-ghost/pkg/ghost"
22
+ "github.com/pfnet-research/git-ghost/pkg/ghost/git"
22
23
"github.com/pfnet-research/git-ghost/pkg/ghost/types"
23
24
"github.com/pfnet-research/git-ghost/pkg/util/errors"
24
25
@@ -43,7 +44,7 @@ func NewPushCommand() *cobra.Command {
43
44
Short : "push commits(hash1...hash2), diff(hash...current state) to your ghost repo" ,
44
45
Long : "push commits or diff or all to your ghost repo. If you didn't specify any subcommand, this commands works as an alias for 'push diff' command." ,
45
46
Args : cobra .RangeArgs (0 , 1 ),
46
- Run : runPushDiffCommand (& flags ),
47
+ Run : runPushCommitsCommand (& flags ),
47
48
}
48
49
command .AddCommand (& cobra.Command {
49
50
Use : "commits [from-hash] [to-hash(default=HEAD)]" ,
@@ -73,6 +74,26 @@ func NewPushCommand() *cobra.Command {
73
74
return command
74
75
}
75
76
77
+ func getFirstRemoteAncestorCommit (commit string ) (string , errors.GitGhostError ) {
78
+ dir := globalOpts .srcDir
79
+ for {
80
+ branchNames , err := git .GetRemoteBranchesContainingCommit (dir , commit )
81
+ if err != nil {
82
+ return "" , err
83
+ }
84
+ if len (branchNames ) > 0 {
85
+ // This commit is the first ancestor commit in any of remotes.
86
+ break
87
+ }
88
+ parentCommit , err := git .GetParentCommit (dir , commit )
89
+ if err != nil {
90
+ return "" , err
91
+ }
92
+ commit = parentCommit
93
+ }
94
+ return commit , nil
95
+ }
96
+
76
97
type pushCommitsArg struct {
77
98
commitsFrom string
78
99
commitsTo string
@@ -111,6 +132,16 @@ func (arg pushCommitsArg) validate() errors.GitGhostError {
111
132
func runPushCommitsCommand (flags * pushFlags ) func (cmd * cobra.Command , args []string ) {
112
133
return func (cmd * cobra.Command , args []string ) {
113
134
pushArg := newPushCommitsArg (args )
135
+
136
+ // If commitsFrom is not given, find the first ancestor commit that is included in any of remotes.
137
+ if pushArg .commitsFrom == "" {
138
+ commitsFrom , err := getFirstRemoteAncestorCommit (pushArg .commitsTo )
139
+ if err != nil {
140
+ errors .LogErrorWithStack (err )
141
+ os .Exit (1 )
142
+ }
143
+ pushArg .commitsFrom = commitsFrom
144
+ }
114
145
if err := pushArg .validate (); err != nil {
115
146
errors .LogErrorWithStack (err )
116
147
os .Exit (1 )
0 commit comments