Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions backup.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"log"
"net/url"
"os/exec"
Expand All @@ -16,6 +17,16 @@ var execCommand = exec.Command
var appFS = afero.NewOsFs()
var gitCommand = "git"
var gethomeDir = homedir.Dir
var lookPath = exec.LookPath

// checkGitAvailability verifies that the git command is available in the system PATH
func checkGitAvailability() error {
_, err := lookPath(gitCommand)
if err != nil {
return fmt.Errorf("git command not found in PATH. Please install git to use gitbackup. Visit https://git-scm.com/downloads for installation instructions")
}
return nil
}

// Check if we have a copy of the repo already, if
// we do, we update the repo, else we do a fresh clone
Expand Down
36 changes: 36 additions & 0 deletions backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"os/exec"
"path"
"strings"
"sync"
"testing"

Expand Down Expand Up @@ -243,3 +244,38 @@ func TestSetupBackupDir(t *testing.T) {
}
}
}

func TestCheckGitAvailability(t *testing.T) {
// Save original lookPath
originalLookPath := lookPath
defer func() {
lookPath = originalLookPath
}()

t.Run("git is available", func(t *testing.T) {
lookPath = func(file string) (string, error) {
if file == "git" {
return "/usr/bin/git", nil
}
return "", fmt.Errorf("not found")
}
err := checkGitAvailability()
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
})

t.Run("git is not available", func(t *testing.T) {
lookPath = func(file string) (string, error) {
return "", fmt.Errorf("executable file not found in $PATH")
}
err := checkGitAvailability()
if err == nil {
t.Error("Expected error when git is not available, got nil")
}
expectedMsg := "git command not found in PATH"
if err != nil && !strings.HasPrefix(err.Error(), expectedMsg) {
t.Errorf("Expected error message to start with '%s', got '%s'", expectedMsg, err.Error())
}
})
}
5 changes: 5 additions & 0 deletions git_repository_clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import (
// handleGitRepositoryClone clones or updates all repositories for the configured service
func handleGitRepositoryClone(client interface{}, c *appConfig) error {

// Check if git is available before proceeding
if err := checkGitAvailability(); err != nil {
return err
}

// Used for waiting for all the goroutines to finish before exiting
var wg sync.WaitGroup
defer wg.Wait()
Expand Down
Loading