diff --git a/.gitignore b/.gitignore index 3b735ec..69f7ec3 100644 --- a/.gitignore +++ b/.gitignore @@ -19,3 +19,6 @@ # Go workspace file go.work + +# Executable files +gitorchk diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..b907bf1 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/danny-molnar/gitorchk + +go 1.22 diff --git a/main.go b/main.go new file mode 100644 index 0000000..112dc29 --- /dev/null +++ b/main.go @@ -0,0 +1,43 @@ +package main + +import ( + "fmt" + "os/exec" + "strings" +) + +func main() { + // Verify Git repository + if _, err := exec.Command("git", "rev-parse").Output(); err != nil { + fmt.Println("Error: Not inside a Git repository.") + return + } + + // Fetch latest remote information + fmt.Println("Fetching latest information from remote...") + if _, err := exec.Command("git", "fetch").Output(); err != nil { + fmt.Println("Error: Unable to fetch latest information from remote.") + return + } + + // Determine branch status + output, err := exec.Command("git", "rev-list", "--left-right", "--count", "main...origin/main").Output() + if err != nil { + fmt.Println("Error: Unable to determine branch status.") + return + } + + // Parse output + counts := strings.Fields(string(output)) + if len(counts) != 2 { + fmt.Println("Error: Unable to process comparison results.") + } + + // Prompt for action + if counts[1] != "0" { + fmt.Printf("Your local 'main' branch iis behind by %s commits.\n", counts[1]) + fmt.Println("Consider rebasig your work with the latest 'main' branch using 'git rebase' or merging updates using 'git merge'.") + } else { + fmt.Println("Your local 'main' branch is up-to-date with 'origin/main'.") + } +} diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..303fc9e --- /dev/null +++ b/main_test.go @@ -0,0 +1,22 @@ +package main + +import ( + "os/exec" + "testing" +) + +func CheckIfGitRepo() (bool, error) { + cmd := exec.Command("git", "rev-parse", "--is-inside-work-tree") + err := cmd.Run() + if err != nil { + return false, err + } + return true, nil +} + +func TestCheckIfGitRepo(t *testing.T) { + _, err := CheckIfGitRepo() + if err != nil { + t.Errorf("Error: %v", err) + } +}