Skip to content

Commit

Permalink
Merge pull request #1 from danny-molnar/dm-feat-core-logic
Browse files Browse the repository at this point in the history
Feat: Implement core logic
  • Loading branch information
danny-molnar authored Mar 25, 2024
2 parents 18c3924 + 389409f commit 1b69afe
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@

# Go workspace file
go.work

# Executable files
gitorchk
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/danny-molnar/gitorchk

go 1.22
43 changes: 43 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -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'.")
}
}
22 changes: 22 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit 1b69afe

Please sign in to comment.