88 "time"
99)
1010
11- // hasChanges returns true if there are staged or unstaged changes.
1211func hasChanges (folder string ) (bool , error ) {
1312 cmd := exec .Command ("git" , "-C" , folder , "status" , "--porcelain" )
1413 out , err := cmd .Output ()
@@ -18,31 +17,24 @@ func hasChanges(folder string) (bool, error) {
1817 return len (bytes .TrimSpace (out )) > 0 , nil
1918}
2019
21- // CommitAndPush adds, commits (if changes), and pushes.
22- // It will skip commits if there are no changes.
2320func CommitAndPush (folder string ) error {
2421 changed , err := hasChanges (folder )
2522 if err != nil {
2623 return fmt .Errorf ("git status failed: %w" , err )
2724 }
2825 if ! changed {
29- // nothing to commit
3026 return nil
3127 }
3228
33- // git add .
3429 if out , err := exec .Command ("git" , "-C" , folder , "add" , "." ).CombinedOutput (); err != nil {
3530 return fmt .Errorf ("git add failed: %v: %s" , err , strings .TrimSpace (string (out )))
3631 }
3732
38- // commit
3933 msg := "Auto backup " + time .Now ().Format (time .RFC3339 )
4034 if out , err := exec .Command ("git" , "-C" , folder , "commit" , "-m" , msg ).CombinedOutput (); err != nil {
41- // ignore "nothing to commit" or other benign messages
4235 return fmt .Errorf ("git commit failed: %v: %s" , err , strings .TrimSpace (string (out )))
4336 }
4437
45- // push
4638 if out , err := exec .Command ("git" , "-C" , folder , "push" ).CombinedOutput (); err != nil {
4739 return fmt .Errorf ("git push failed: %v: %s" , err , strings .TrimSpace (string (out )))
4840 }
0 commit comments