only push if there are no changes

pull/27/head
Marcusk19 2 years ago
parent 03e893c3fb
commit cfb3f90fb1

@ -108,6 +108,9 @@ func runSyncCommand(cmd *cobra.Command, args []string) {
passwordVal, err := password.Run() passwordVal, err := password.Run()
CheckIfError(err) CheckIfError(err)
fmt.Println("Pulling from remote")
err = w.Pull(&git.PullOptions{ err = w.Pull(&git.PullOptions{
RemoteName: "origin", RemoteName: "origin",
Auth: &http.BasicAuth { Auth: &http.BasicAuth {
@ -122,41 +125,50 @@ func runSyncCommand(cmd *cobra.Command, args []string) {
fmt.Fprintf(cmd.OutOrStdout(), "successfully pulled from %s", origin) fmt.Fprintf(cmd.OutOrStdout(), "successfully pulled from %s", origin)
} }
err = gitAddFiles(w, FileSystem) status, err := w.Status()
if err != nil { if err != nil {
log.Fatalf("Could not add files: %s\n", err) log.Fatalln("Error getting status", err)
} }
commitMessage := "backup " + time.Now().String() if !status.IsClean() {
fmt.Println("Changes detected, committing and pushing...")
commit, err := w.Commit(commitMessage, &git.CommitOptions{ err = gitAddFiles(w, FileSystem)
Author: &object.Signature{ if err != nil {
Name: "dotctl CLI", log.Fatalf("Could not add files: %s\n", err)
Email: "example@example.com", }
When: time.Now(),
},
})
if err != nil { commitMessage := "backup " + time.Now().String()
log.Fatal(err.Error())
}
obj, err := r.CommitObject(commit) commit, err := w.Commit(commitMessage, &git.CommitOptions{
Author: &object.Signature{
Name: "dotctl CLI",
Email: "example@example.com",
When: time.Now(),
},
})
if err != nil { if err != nil {
log.Fatalf("Cannot commit: %s",err) log.Fatal(err.Error())
} }
fmt.Println(obj) obj, err := r.CommitObject(commit)
err = r.Push(&git.PushOptions{ if err != nil {
RemoteName: "origin", log.Fatalf("Cannot commit: %s",err)
Auth: &http.BasicAuth { }
Username: usernameVal,
Password: passwordVal, fmt.Println(obj)
},
})
CheckIfError(err)
viper.WriteConfig()
err = r.Push(&git.PushOptions{
RemoteName: "origin",
Auth: &http.BasicAuth {
Username: usernameVal,
Password: passwordVal,
},
})
CheckIfError(err)
}
viper.WriteConfig()
} }

Loading…
Cancel
Save