dev/git.go

98 lines
2.2 KiB
Go

package main
import (
"bufio"
"fmt"
"os"
"os/exec"
"strings"
"time"
)
func isGitRepo() bool {
cmd := exec.Command("git", "rev-parse", "--git-dir")
return cmd.Run() == nil
}
func hasUncommittedChanges() bool {
cmd := exec.Command("git", "diff-index", "--quiet", "HEAD", "--")
return cmd.Run() != nil
}
func getCurrentBranch() (string, error) {
cmd := exec.Command("git", "branch", "--show-current")
output, err := cmd.Output()
if err != nil {
return "", err
}
return strings.TrimSpace(string(output)), nil
}
func handlePush() error {
log("Preparing to push repository...")
if !isGitRepo() {
errorLog("Not in a git repository")
return fmt.Errorf("not in git repo")
}
if hasUncommittedChanges() {
warn("You have uncommitted changes")
fmt.Println()
cmd := exec.Command("git", "status", "--short")
cmd.Stdout = os.Stdout
cmd.Run()
fmt.Println()
defaultMsg := fmt.Sprintf("dev: automated commit - %s", time.Now().Format("2006-01-02 15:04:05"))
fmt.Print("Commit all changes? (Y/n): ")
reader := bufio.NewReader(os.Stdin)
answer, _ := reader.ReadString('\n')
answer = strings.TrimSpace(strings.ToLower(answer))
if answer != "n" && answer != "no" {
fmt.Println()
fmt.Printf("Default: %s\n", defaultMsg)
fmt.Print("Custom commit message (or press Enter for default): ")
commitMsg, _ := reader.ReadString('\n')
commitMsg = strings.TrimSpace(commitMsg)
if commitMsg == "" {
commitMsg = defaultMsg
}
if err := exec.Command("git", "add", ".").Run(); err != nil {
return fmt.Errorf("failed to add changes: %v", err)
}
if err := exec.Command("git", "commit", "-m", commitMsg).Run(); err != nil {
return fmt.Errorf("failed to commit changes: %v", err)
}
log("✓ Changes committed: %s", commitMsg)
}
}
if err := runSecurityScan(); err != nil {
return err
}
branch, err := getCurrentBranch()
if err != nil {
return fmt.Errorf("failed to get current branch: %v", err)
}
log("Pushing branch '%s' to origin...", branch)
cmd := exec.Command("git", "push", "origin", branch)
if err := cmd.Run(); err != nil {
errorLog("❌ Push failed")
return fmt.Errorf("push failed")
}
log("✅ Successfully pushed to origin/%s", branch)
return nil
}