Skip to contentSkip to conversation

more meaningful error message in gitk when git binary is not available

PATCH In review Josef Assad 3 participants 4 messages Jun 15, 2016 1 message not in the archive

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 more meaningful error message in gitk when git binary is not available

Hi. I ran across what is a decidedly trivial little issue in gitk. The TCL/Tk looked simple enough so I am giving you a patch anyhow in case you want to fix it.

When for whatever reason the git binary is unavailable, gitk would complain about missing git repository instead, so this patch adds a check for git binary availability.

In case anyone is curious, I found this issue here:

http://stackoverflow.com/q/11967110/53936

---
 gitk-git/gitk |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)
gitk-git/gitk +6 -0
index d93bd99..7e2e0a7 100755
@@ -11680,6 +11680,12 @@ setui $uicolor
 
 setoptions
 
+# check that the git executables are available for use
+if [catch {set gitexists [exec which git]}] {
+    show_error {} . [mc "Cannot find a suitable git executable."]
+    exit 1
+}
+
 # check that we can find a .git directory somewhere...
 if {[catch {set gitdir [exec git rev-parse --git-dir]}]} {
     show_error {} . [mc "Cannot find a git repository here."]
Signature · 1 line
1.7.5.4
#1 lore raw
Quoted 4 lines · Josef Assad
---
 gitk-git/gitk |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)

Thanks.

Quoted 7 lines
gitk-git/gitk +0 -0
index d93bd99..7e2e0a7 100755
@@ -11680,6 +11680,12 @@ setui $uicolor
 
 setoptions

gitk-git/gitk setui $uicolor · line 11691

Quoted 10 lines
gitk-git/gitk +6 -0
 
+# check that the git executables are available for use
+if [catch {set gitexists [exec which git]}] {
+    show_error {} . [mc "Cannot find a suitable git executable."]
+    exit 1
+}
+
 # check that we can find a .git directory somewhere...
 if {[catch {set gitdir [exec git rev-parse --git-dir]}]} {
     show_error {} . [mc "Cannot find a git repository here."]

It is somewhat a stupid solution to add an extra fork that will only waste cycles in the normal non-error case, especially when we already have an error codepath that acts on lack of the "git" command anyway, isn't it?

The "rev-parse" you see in the post-context will fail when we are not in a git repository, but it will also fail when we do not have git.

You can add the new check to if {[catch {... git rev-parse }]} block; before unconditionally saying "cannot find a git repo", you check if "git" even exists, and give an appropriate error message. That way, you do not punish normal use case with an extra useless fork.

Something like this, I presume.

 gitk | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
gitk +6 -1
index d93bd99..60794a7 100755
@@ -11682,7 +11682,12 @@ setoptions
 
 # check that we can find a .git directory somewhere...
 if {[catch {set gitdir [exec git rev-parse --git-dir]}]} {
-    show_error {} . [mc "Cannot find a git repository here."]
+    # we could have failed because there is no git to begin with
+    if {[catch {exec git version}]} {
+        show_error {} . [mc "You do not seem to have 'git' command."]
+    } else {
+        show_error {} . [mc "Cannot find a git repository here."]
+    }
     exit 1
 }
 

A message that is not in the archive (sent off-list or not yet received): [email protected]

#3 lore raw
Quoted 61 lines · Josef Assad
Quoted 17 lines · Junio C Hamano
Quoted 4 lines · Josef Assad
---
 gitk-git/gitk |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)

Thanks.

Quoted 8 lines
gitk-git/gitk +0 -0
index d93bd99..7e2e0a7 100755
@@ -11680,6 +11680,12 @@ setui $uicolor
 
 setoptions
 
Quoted 14 lines

gitk-git/gitk setui $uicolor · line 11691

Quoted 9 lines
gitk-git/gitk +6 -0
+# check that the git executables are available for use
+if [catch {set gitexists [exec which git]}] {
+    show_error {} . [mc "Cannot find a suitable git executable."]
+    exit 1
+}
+
 # check that we can find a .git directory somewhere...
 if {[catch {set gitdir [exec git rev-parse --git-dir]}]} {
     show_error {} . [mc "Cannot find a git repository here."]

It is somewhat a stupid solution to add an extra fork that will only waste cycles in the normal non-error case, especially when we already have an error codepath that acts on lack of the "git" command anyway, isn't it?

I don't think it's actually _stupid_, but I also think your solution works better if you're trying to avoid one more exec call.

Mine has one less level of indentation though, and it has clearer delineation between checking for and handling two distinct error conditions. :)

Quoted 21 lines

The "rev-parse" you see in the post-context will fail when we are not in a git repository, but it will also fail when we do not have git.

You can add the new check to if {[catch {... git rev-parse }]} block; before unconditionally saying "cannot find a git repo", you check if "git" even exists, and give an appropriate error message. That way, you do not punish normal use case with an extra useless fork.

Something like this, I presume.

 gitk | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
gitk +0 -0
index d93bd99..60794a7 100755
@@ -11682,7 +11682,12 @@ setoptions
 
Quoted 16 lines

gitk setoptions · line 11692

Quoted 11 lines
gitk +6 -1
 # check that we can find a .git directory somewhere...
 if {[catch {set gitdir [exec git rev-parse --git-dir]}]} {
-    show_error {} . [mc "Cannot find a git repository here."]
+    # we could have failed because there is no git to begin with
+    if {[catch {exec git version}]} {
+        show_error {} . [mc "You do not seem to have 'git' command."]
+    } else {
+        show_error {} . [mc "Cannot find a git repository here."]
+    }
     exit 1
 }

I'm neutral though and not married to my patch in any way, just trying to be helpful. In my opinon, yours is cleaner, mine is a tiny bit more readable in a file already closing on 12k lines.

Bah, forgot to Cc list. My bad.

#4 lore raw
In reply to Josef Assad
Quoted 27 lines · Josef Assad

Hi. I ran across what is a decidedly trivial little issue in gitk. The TCL/Tk looked simple enough so I am giving you a patch anyhow in case you want to fix it.

When for whatever reason the git binary is unavailable, gitk would complain about missing git repository instead, so this patch adds a check for git binary availability.

In case anyone is curious, I found this issue here:

http://stackoverflow.com/q/11967110/53936

---
gitk-git/gitk | 6 ++++++

1 files changed, 6 insertions(+), 0 deletions(-)
gitk-git/gitk +0 -0
index d93bd99..7e2e0a7 100755
@@ -11680,6 +11680,12 @@ setui $uicolor
 

setoptions

gitk-git/gitk setui $uicolor · line 11684

Quoted 2 lines
gitk-git/gitk +2 -0
+# check that the git executables are available for use
+if [catch {set gitexists [exec which git]}] {

I believe 'which' is not portable, you could use 'type' instead.

Quoted 7 lines
gitk-git/gitk +4 -0
+    show_error {} . [mc "Cannot find a suitable git executable."]
+    exit 1
+}
+
# check that we can find a .git directory somewhere...
if {[catch {set gitdir [exec git rev-parse --git-dir]}]} {
    show_error {} . [mc "Cannot find a git repository here."]