From cfb3f90fb159435ed3433556cef25b193d43d9d7 Mon Sep 17 00:00:00 2001 From: Marcusk19 Date: Fri, 12 Apr 2024 15:01:43 -0400 Subject: [PATCH] only push if there are no changes --- cmd/sync.go | 68 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 40 insertions(+), 28 deletions(-) diff --git a/cmd/sync.go b/cmd/sync.go index 4d8eee0..eda56a5 100644 --- a/cmd/sync.go +++ b/cmd/sync.go @@ -108,6 +108,9 @@ func runSyncCommand(cmd *cobra.Command, args []string) { passwordVal, err := password.Run() CheckIfError(err) + + fmt.Println("Pulling from remote") + err = w.Pull(&git.PullOptions{ RemoteName: "origin", Auth: &http.BasicAuth { @@ -122,41 +125,50 @@ func runSyncCommand(cmd *cobra.Command, args []string) { fmt.Fprintf(cmd.OutOrStdout(), "successfully pulled from %s", origin) } - err = gitAddFiles(w, FileSystem) + status, err := w.Status() if err != nil { - log.Fatalf("Could not add files: %s\n", err) + log.Fatalln("Error getting status", err) } - - commitMessage := "backup " + time.Now().String() - commit, err := w.Commit(commitMessage, &git.CommitOptions{ - Author: &object.Signature{ - Name: "dotctl CLI", - Email: "example@example.com", - When: time.Now(), - }, - }) + if !status.IsClean() { + fmt.Println("Changes detected, committing and pushing...") - if err != nil { - log.Fatal(err.Error()) - } + err = gitAddFiles(w, FileSystem) + if err != nil { + log.Fatalf("Could not add files: %s\n", err) + } + + commitMessage := "backup " + time.Now().String() - 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 { - log.Fatalf("Cannot commit: %s",err) - } + if err != nil { + log.Fatal(err.Error()) + } - fmt.Println(obj) + obj, err := r.CommitObject(commit) - err = r.Push(&git.PushOptions{ - RemoteName: "origin", - Auth: &http.BasicAuth { - Username: usernameVal, - Password: passwordVal, - }, - }) - CheckIfError(err) - viper.WriteConfig() + if err != nil { + log.Fatalf("Cannot commit: %s",err) + } + + fmt.Println(obj) + err = r.Push(&git.PushOptions{ + RemoteName: "origin", + Auth: &http.BasicAuth { + Username: usernameVal, + Password: passwordVal, + }, + }) + CheckIfError(err) + } + + viper.WriteConfig() }