Skip to content

Commit 8fa87fd

Browse files
committed
Added git-binary-file-path config variable
When grv falls back to using git it will first attempt to verify it can successfully call the git binary. If unsuccessful it will prompt the user with an error message to set the git-binary-file-path config variable.
1 parent b934174 commit 8fa87fd

File tree

5 files changed

+66
-30
lines changed

5 files changed

+66
-30
lines changed

cmd/grv/config.go

+7
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ const (
3232
cfCommitGraphDefaultValue = false
3333
cfConfirmCheckoutDefaultValue = true
3434
cfPromptHistorySizeDefaultValue = 1000
35+
cfGitBinaryFilePathDefaultValue = ""
3536

3637
cfAllView = "All"
3738
cfMainView = "MainView"
@@ -68,6 +69,8 @@ const (
6869
CfConfirmCheckout ConfigVariable = "confirm-checkout"
6970
// CfPromptHistorySize stores the maximum number of prompt entries retained
7071
CfPromptHistorySize ConfigVariable = "prompt-history-size"
72+
// CfGitBinaryFilePath stores the file path to the git binary
73+
CfGitBinaryFilePath ConfigVariable = "git-binary-file-path"
7174
)
7275

7376
var systemColorValues = map[string]SystemColorValue{
@@ -317,6 +320,10 @@ func NewConfiguration(keyBindings KeyBindings, channels Channels) *Configuration
317320
validator: promptHistorySizeValidator{},
318321
description: "Maximum number of prompt entries retained",
319322
},
323+
CfGitBinaryFilePath: {
324+
defaultValue: cfGitBinaryFilePathDefaultValue,
325+
description: "File path to git binary. Required only when git binary is not in $PATH",
326+
},
320327
}
321328

322329
for _, configVariable := range config.variables {

cmd/grv/grv.go

+9-10
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,10 @@ func NewGRV(readOnly bool) *GRV {
173173
}
174174

175175
channels := grvChannels.Channels()
176+
keyBindings := NewKeyBindingManager()
177+
config := NewConfiguration(keyBindings, channels)
176178

177-
repoDataLoader := NewRepoDataLoader(channels)
179+
repoDataLoader := NewRepoDataLoader(channels, config)
178180
repoData := NewRepositoryData(repoDataLoader, channels)
179181

180182
var repoController RepoController
@@ -185,8 +187,6 @@ func NewGRV(readOnly bool) *GRV {
185187
repoController = NewRepoController(repoData, channels)
186188
}
187189

188-
keyBindings := NewKeyBindingManager()
189-
config := NewConfiguration(keyBindings, channels)
190190
ui := NewNCursesDisplay(channels, config)
191191
view := NewView(repoData, repoController, channels, config)
192192

@@ -208,6 +208,12 @@ func NewGRV(readOnly bool) *GRV {
208208
func (grv *GRV) Initialise(repoPath, workTreePath string) (err error) {
209209
log.Info("Initialising GRV")
210210

211+
channels := grv.channels.Channels()
212+
213+
if configErrors := grv.config.Initialise(); configErrors != nil {
214+
channels.ReportErrors(configErrors)
215+
}
216+
211217
if err = grv.repoInitialiser.CreateRepositoryInstance(repoPath, workTreePath); err != nil {
212218
return
213219
}
@@ -226,13 +232,6 @@ func (grv *GRV) Initialise(repoPath, workTreePath string) (err error) {
226232
return
227233
}
228234

229-
if configErrors := grv.config.Initialise(); configErrors != nil {
230-
for _, configError := range configErrors {
231-
grv.channels.errorCh <- configError
232-
}
233-
}
234-
235-
channels := grv.channels.Channels()
236235
InitReadLine(channels, grv.config)
237236

238237
return

cmd/grv/repo_data_loader.go

+30-9
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,12 @@ type instanceCache struct {
3636

3737
// RepoDataLoader handles loading data from the repository
3838
type RepoDataLoader struct {
39-
repo *git.Repository
40-
cache *instanceCache
41-
channels Channels
42-
diffErrorPresent bool
39+
repo *git.Repository
40+
cache *instanceCache
41+
channels Channels
42+
config Config
43+
diffErrorPresent bool
44+
gitBinaryConfirmed bool
4345
}
4446

4547
// Oid is reference to a git object
@@ -600,10 +602,11 @@ func (cache *instanceCache) getCachedOid(oidStr string) (oid *Oid, exists bool)
600602
}
601603

602604
// NewRepoDataLoader creates a new instance
603-
func NewRepoDataLoader(channels Channels) *RepoDataLoader {
605+
func NewRepoDataLoader(channels Channels, config Config) *RepoDataLoader {
604606
return &RepoDataLoader{
605607
cache: newInstanceCache(),
606608
channels: channels,
609+
config: config,
607610
}
608611
}
609612

@@ -1140,14 +1143,14 @@ const (
11401143

11411144
func (repoDataLoader *RepoDataLoader) generateCommitDiffUsingCLI(commit *Commit) (diff *Diff, err error) {
11421145
log.Debugf("Attempting to load diff using cli for commit: %v", commit.oid.String())
1143-
gitCommand := []string{"git", "show", "--encoding=UTF8", "--pretty=oneline", "--root", "--patch-with-stat", "--no-color", commit.oid.String()}
1146+
gitCommand := []string{"show", "--encoding=UTF8", "--pretty=oneline", "--root", "--patch-with-stat", "--no-color", commit.oid.String()}
11441147
return repoDataLoader.runGitCLIDiff(gitCommand, dtCommit)
11451148
}
11461149

11471150
func (repoDataLoader *RepoDataLoader) generateFileDiffUsingCLI(statusType StatusType, path string) (diff *Diff, err error) {
11481151
log.Debugf("Attempting to load diff using cli for StatusType: %v and file: %v", StatusTypeDisplayName(statusType), path)
11491152

1150-
gitCommand := []string{"git", "diff"}
1153+
gitCommand := []string{"diff"}
11511154

11521155
if statusType == StStaged {
11531156
gitCommand = append(gitCommand, "--cached")
@@ -1163,7 +1166,7 @@ func (repoDataLoader *RepoDataLoader) generateFileDiffUsingCLI(statusType Status
11631166
func (repoDataLoader *RepoDataLoader) generateStageDiffUsingCLI(statusType StatusType) (diff *Diff, err error) {
11641167
log.Debugf("Attempting to load diff using cli for StatusType: %v", StatusTypeDisplayName(statusType))
11651168

1166-
gitCommand := []string{"git", "diff"}
1169+
gitCommand := []string{"diff"}
11671170

11681171
if statusType == StStaged {
11691172
gitCommand = append(gitCommand, "--cached")
@@ -1176,10 +1179,28 @@ func (repoDataLoader *RepoDataLoader) generateStageDiffUsingCLI(statusType Statu
11761179
return repoDataLoader.runGitCLIDiff(gitCommand, dtStage)
11771180
}
11781181

1182+
func (repoDataLoader *RepoDataLoader) gitBinary() string {
1183+
if gitBinary := repoDataLoader.config.GetString(CfGitBinaryFilePath); gitBinary != "" {
1184+
return gitBinary
1185+
}
1186+
1187+
return "git"
1188+
}
1189+
11791190
func (repoDataLoader *RepoDataLoader) runGitCLIDiff(gitCommand []string, diffType diffType) (diff *Diff, err error) {
11801191
diff = &Diff{}
11811192

1182-
cmd := exec.Command(gitCommand[0], gitCommand[1:]...)
1193+
if !repoDataLoader.gitBinaryConfirmed {
1194+
if exec.Command(repoDataLoader.gitBinary(), "version").Run() == nil {
1195+
repoDataLoader.gitBinaryConfirmed = true
1196+
} else {
1197+
err = fmt.Errorf("Unable to successfully call git binary. "+
1198+
"If git is not in $PATH then please set the config variable %v", CfGitBinaryFilePath)
1199+
return
1200+
}
1201+
}
1202+
1203+
cmd := exec.Command(repoDataLoader.gitBinary(), gitCommand...)
11831204
cmd.Env = repoDataLoader.GenerateGitCommandEnvironment()
11841205

11851206
var stdout, stderr bytes.Buffer

cmd/grv/ui.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,10 @@ func (ui *NCursesUI) initialiseNCurses() (err error) {
266266
gc.Raw(true)
267267
gc.MouseInterval(0)
268268

269+
if ui.config.GetBool(CfMouse) {
270+
ui.toggleMouse()
271+
}
272+
269273
if gc.Cursor(0) != nil {
270274
log.Debugf("Unable to hide cursor")
271275
}
@@ -649,13 +653,17 @@ func (ui *NCursesUI) onConfigVariableChange(configVariable ConfigVariable) {
649653
theme := ui.config.GetTheme()
650654
ui.initialiseColorPairsFromTheme(theme)
651655
case CfMouse:
652-
log.Infof("Toggling mouse enabled")
653-
gc.MouseMask(ui.mouseMask, &ui.mouseMask)
656+
ui.toggleMouse()
654657
default:
655658
log.Warn("Received notification for variable I didn't register for: %v", configVariable)
656659
}
657660
}
658661

662+
func (ui *NCursesUI) toggleMouse() {
663+
log.Infof("Toggling mouse enabled")
664+
gc.MouseMask(ui.mouseMask, &ui.mouseMask)
665+
}
666+
659667
func (ui *NCursesUI) initialiseColorPairsFromTheme(theme Theme) {
660668
defaultComponent := theme.GetComponent(CmpAllviewDefault)
661669
fgDefault := ui.getNCursesColor(defaultComponent.fgcolor)

doc/documentation.md

+10-9
Original file line numberDiff line numberDiff line change
@@ -166,15 +166,16 @@ Configuration variables allow features to be enabled, disabled and configured.
166166
They are specified using the set command in the grvrc file or at the command prompt
167167

168168
```
169-
Variable | Type | Default Value | Description
170-
--------------------+--------+---------------+-----------------------------------------------
171-
commit-graph | bool | false | Commit graph visible
172-
confirm-checkout | bool | true | Confirm before performing git checkout
173-
mouse | bool | false | Mouse support enabled
174-
mouse-scroll-rows | int | 3 | Number of rows scrolled for each mouse event
175-
prompt-history-size | int | 1000 | Maximum number of prompt entries retained
176-
tabwidth | int | 8 | Tab character screen width (minimum value: 1)
177-
theme | string | solarized | The currently active theme
169+
Variable | Type | Default Value | Description
170+
---------------------+--------+---------------+------------------------------------------------------------------------
171+
commit-graph | bool | false | Commit graph visible
172+
confirm-checkout | bool | true | Confirm before performing git checkout
173+
git-binary-file-path | string | | File path to git binary. Required only when git binary is not in $PATH
174+
mouse | bool | false | Mouse support enabled
175+
mouse-scroll-rows | int | 3 | Number of rows scrolled for each mouse event
176+
prompt-history-size | int | 1000 | Maximum number of prompt entries retained
177+
tabwidth | int | 8 | Tab character screen width (minimum value: 1)
178+
theme | string | solarized | The currently active theme
178179
```
179180

180181

0 commit comments

Comments
 (0)