-
Notifications
You must be signed in to change notification settings - Fork 1
/
push.go
122 lines (97 loc) · 3.29 KB
/
push.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
Copyright © 2019 Danil Pismenny <danil@brandymint.ru>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package cmd
import (
"fmt"
"io/ioutil"
"os"
"time"
"github.com/spf13/cobra"
"github.com/tcnksm/go-gitconfig"
"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing/object"
)
// pushCmd represents the push command
var pushCmd = &cobra.Command{
Use: "push <file>",
Short: "Push changes of goga-modules into source repository",
Long: `Push specified file into its repository. For example:
# Push single file
> goga push ./app/javascripts/spinner.js
`,
Args: cobra.RangeArgs(1, 1),
Run: func(cmd *cobra.Command, args []string) {
file := args[0]
firstLint := ReadFirstLine(file)
url := FetchUrlFromComment(firstLint)
PushFileToRemoteRepository(file, url)
},
}
func init() {
rootCmd.AddCommand(pushCmd)
}
func PushFileToRemoteRepository(file string, url string) error {
tempDir, err := ioutil.TempDir("", "goga")
CheckIfError(err)
defer os.RemoveAll(tempDir)
destination_file := GetSubdirectoryFromUrl(url)
fmt.Print("Checking ", file)
// TODO Get repo from url
var repo = GetRepoFromUrl(url)
fmt.Print(" to ", repo+"/"+destination_file)
r, err := git.PlainClone(tempDir, false, &git.CloneOptions{URL: repo})
CheckIfError(err)
fmt.Print(" ")
destination_file_path := tempDir + "/" + destination_file
// TODO Remove magic-comment
CopyWithoutMagicComment(file, destination_file_path)
w, err := r.Worktree()
CheckIfError(err)
status, err := w.Status()
CheckIfError(err)
if len(status) == 0 {
fmt.Println("nothing changed, skip")
} else {
CommintAndPush(r, w, tempDir, destination_file, destination_file_path)
}
return err
}
func CommintAndPush(r *git.Repository, w *git.Worktree, tempDir string, destination_file string, destination_file_path string) {
username, err := gitconfig.Username()
email, err := gitconfig.Email()
commitMessage := fmt.Sprintf("Update %s by goga", destination_file)
_, err = w.Add(destination_file)
CheckIfError(err)
commit, err := w.Commit(commitMessage, &git.CommitOptions{
Author: &object.Signature{
Name: username,
Email: email,
When: time.Now(),
},
})
CheckIfError(err)
fmt.Print("commit, ")
_, err = r.CommitObject(commit)
CheckIfError(err)
fmt.Print("pushing.. ")
err = r.Push(&git.PushOptions{})
CheckIfError(err)
fmt.Println("done.")
return
}