threads / patch / 40718

patchgitk: add --cwd=path commandline parameter to change path

Subject: [PATCH 0/1] gitk: add --cwd=path commandline parameter to change path

## tl;dr

8 messages between Nov 3, 2015 and Dec 19, 2015. Diffs are folded; open one to read it.

replies: 7people: 3as markdown or json

Juha-Pekka Heikkila· Nov 3, 2015, 15:00 UTC · lore

I found I needed this option thus made it for myself, maybe others find it useful too. I'm not skilled with tcl so if there is something totally wrong with my change this is why.

/Juha-Pekka
Juha-Pekka Heikkila (1):
  gitk: add --cwd=path commandline parameter to change path
 Documentation/gitk.txt |  5 +++++
 gitk-git/gitk          | 15 +++++++++------
 2 files changed, 14 insertions(+), 6 deletions(-)
-- 
1.9.1
Juha-Pekka Heikkila· Nov 3, 2015, 15:00 UTC · re: Juha-Pekka Heikkila · lore

[PATCH 1/1] gitk: add --cwd=path commandline parameter to change path

This patch adds --cwd (change working directory) parameter to gitk. With this parameter, instead of need to cd to directory with .git folder, one can point the correct folder from commandline.

Signed-off-by: Juha-Pekka Heikkila <juhapekka.heikkila@gmail.com>
---
 Documentation/gitk.txt |  5 +++++
 gitk-git/gitk          | 15 +++++++++------
 2 files changed, 14 insertions(+), 6 deletions(-)
Show changes to 2 files +14 −6

Documentation/gitk.txt, gitk-git/gitk

diff --git a/Documentation/gitk.txt b/Documentation/gitk.txt
index 6ade002..1f42198 100644
--- a/Documentation/gitk.txt
+++ b/Documentation/gitk.txt
@@ -146,6 +146,11 @@ gitk-specific options
 	Select the specified commit after loading the graph.
 	Default behavior is equivalent to specifying '--select-commit=HEAD'.
 
+--cwd=<path>::
+
+	Change working direcoty to <path>. If the git tree exist elsewhere
+	gitk first cd to given path before start to operate.
+
 Examples
 --------
 gitk v2.6.12.. include/scsi drivers/scsi::
diff --git a/gitk-git/gitk b/gitk-git/gitk
index fcc606e..5fdf459 100755
--- a/gitk-git/gitk
+++ b/gitk-git/gitk
@@ -12279,12 +12279,6 @@ setui $uicolor
 
 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."]
-    exit 1
-}
-
 set selecthead {}
 set selectheadid {}
 
@@ -12305,6 +12299,9 @@ foreach arg $argv {
 	"--argscmd=*" {
 	    set revtreeargscmd [string range $arg 10 end]
 	}
+	"--cwd=*" {
+	    cd [string range $arg 6 end]
+	}
 	default {
 	    lappend revtreeargs $arg
 	}
@@ -12312,6 +12309,12 @@ foreach arg $argv {
     incr i
 }
 
+# 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."]
+    exit 1
+}
+
 if {$selecthead eq "HEAD"} {
     set selecthead {}
 }
-- 
1.9.1
Eric Sunshine· Nov 3, 2015, 18:27 UTC · re: Juha-Pekka Heikkila · lore

Re: [PATCH 1/1] gitk: add --cwd=path commandline parameter to change path

On Tue, Nov 3, 2015 at 10:00 AM, Juha-Pekka Heikkila <juhapekka.heikkila@gmail.com> wrote:

> This patch adds --cwd (change working directory) parameter to
> gitk. With this parameter, instead of need to cd to directory
> with .git folder, one can point the correct folder from
> commandline.

git itself supports this sort of functionality via -C, as does GNU tar, which suggests such an option in gitk should be named -C, as well.

Show 10 quoted lines
> Signed-off-by: Juha-Pekka Heikkila <juhapekka.heikkila@gmail.com>
> ---
> diff --git a/Documentation/gitk.txt b/Documentation/gitk.txt
> @@ -146,6 +146,11 @@ gitk-specific options
>         Select the specified commit after loading the graph.
>         Default behavior is equivalent to specifying '--select-commit=HEAD'.
>
> +--cwd=<path>::
> +
> +       Change working direcoty to <path>. If the git tree exist elsewhere
s/direcoty/directory/
> +       gitk first cd to given path before start to operate.

Taking git's -C documentation as a template, perhaps this could be written instead as:

    Run as if gitk was started in '<path>' instead of the current
    working directory. When multiple `-C` options are given, each
    subsequent non-absolute `-C <path>` is interpreted relative to
    the preceding `-C <path>`.

This description correctly reflects your implementation which allows --cwd to be specified multiple times.

Show 45 quoted lines
>  Examples
>  --------
>  gitk v2.6.12.. include/scsi drivers/scsi::
> diff --git a/gitk-git/gitk b/gitk-git/gitk
> index fcc606e..5fdf459 100755
> --- a/gitk-git/gitk
> +++ b/gitk-git/gitk
> @@ -12279,12 +12279,6 @@ setui $uicolor
>
>  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."]
> -    exit 1
> -}
> -
>  set selecthead {}
>  set selectheadid {}
>
> @@ -12305,6 +12299,9 @@ foreach arg $argv {
>         "--argscmd=*" {
>             set revtreeargscmd [string range $arg 10 end]
>         }
> +       "--cwd=*" {
> +           cd [string range $arg 6 end]
> +       }
>         default {
>             lappend revtreeargs $arg
>         }
> @@ -12312,6 +12309,12 @@ foreach arg $argv {
>      incr i
>  }
>
> +# 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."]
> +    exit 1
> +}
> +
>  if {$selecthead eq "HEAD"} {
>      set selecthead {}
>  }
> --
> 1.9.1
Juha-Pekka Heikkila· Nov 5, 2015, 09:19 UTC · re: Eric Sunshine · lore

[PATCH v2] gitk: add -C <path> commandline parameter to change path

This patch adds -C (change working directory) parameter to gitk. With this parameter, instead of need to cd to directory with .git folder, one can point the correct folder from commandline.

v2: Adjusted the parameter as per Eric's suggestion. I think
    it now work in similar manner as in many GNU tools as well
    as git itself.
Signed-off-by: Juha-Pekka Heikkila <juhapekka.heikkila@gmail.com>
---
 Documentation/gitk.txt |  7 +++++++
 gitk-git/gitk          | 26 +++++++++++++++++---------
 2 files changed, 24 insertions(+), 9 deletions(-)
Show changes to 2 files +24 −9

Documentation/gitk.txt, gitk-git/gitk

diff --git a/Documentation/gitk.txt b/Documentation/gitk.txt
index 6ade002..d194d9b 100644
--- a/Documentation/gitk.txt
+++ b/Documentation/gitk.txt
@@ -146,6 +146,13 @@ gitk-specific options
 	Select the specified commit after loading the graph.
 	Default behavior is equivalent to specifying '--select-commit=HEAD'.
 
+-C <path>::
+
+	Run as if gitk was started in '<path>' instead of the current
+	working directory. When multiple `-C` options are given, each
+	subsequent non-absolute `-C <path>` is interpreted relative to
+	the preceding `-C <path>`.
+
 Examples
 --------
 gitk v2.6.12.. include/scsi drivers/scsi::
diff --git a/gitk-git/gitk b/gitk-git/gitk
index fcc606e..606474a 100755
--- a/gitk-git/gitk
+++ b/gitk-git/gitk
@@ -12279,20 +12279,14 @@ setui $uicolor
 
 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."]
-    exit 1
-}
-
 set selecthead {}
 set selectheadid {}
 
 set revtreeargs {}
 set cmdline_files {}
-set i 0
 set revtreeargscmd {}
-foreach arg $argv {
+for {set i 0} {$i < [llength $argv]} {incr i} {
+	set arg [lindex $argv [expr {$i}]]
     switch -glob -- $arg {
 	"" { }
 	"--" {
@@ -12305,11 +12299,25 @@ foreach arg $argv {
 	"--argscmd=*" {
 	    set revtreeargscmd [string range $arg 10 end]
 	}
+	"-C*" {
+		if {[string length $arg] < 3} {
+			incr i
+			cd [lindex $argv [expr {$i}]]
+			continue
+		} else {
+			cd [string range $arg 2 end]
+		}
+	}
 	default {
 	    lappend revtreeargs $arg
 	}
     }
-    incr i
+}
+
+# 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."]
+    exit 1
 }
 
 if {$selecthead eq "HEAD"} {
-- 
1.9.1
Eric Sunshine· Nov 6, 2015, 09:48 UTC · re: Juha-Pekka Heikkila · lore

Re: [PATCH v2] gitk: add -C <path> commandline parameter to change path

On Thu, Nov 5, 2015 at 4:19 AM, Juha-Pekka Heikkila <juhapekka.heikkila@gmail.com> wrote:

> This patch adds -C (change working directory) parameter to
> gitk. With this parameter, instead of need to cd to directory
> with .git folder, one can point the correct folder from
> commandline.

Thanks, -C makes more sense than --cwd, and is more consistent with other commands.

You'll want to also address the patch to Paul Mackerras (I've cc:'d him) since he's the gitk maintainer, and Junio pulls gitk from him.

> v2: Adjusted the parameter as per Eric's suggestion. I think
>     it now work in similar manner as in many GNU tools as well
>     as git itself.

This sort of explanation of of changes between versions is definitely welcome, but should be placed below the "---" line just under your sign-off so that it does not become part of the permanent commit message.

> Signed-off-by: Juha-Pekka Heikkila <juhapekka.heikkila@gmail.com>
> ---

You'd place your commentary about changes between patch versions here. More below...

Show 36 quoted lines
>  Documentation/gitk.txt |  7 +++++++
>  gitk-git/gitk          | 26 +++++++++++++++++---------
>  2 files changed, 24 insertions(+), 9 deletions(-)
>
> diff --git a/gitk-git/gitk b/gitk-git/gitk
> @@ -12279,20 +12279,14 @@ setui $uicolor
>
>  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."]
> -    exit 1
> -}
> -
>  set selecthead {}
>  set selectheadid {}
>
>  set revtreeargs {}
>  set cmdline_files {}
> -set i 0
>  set revtreeargscmd {}
> -foreach arg $argv {
> +for {set i 0} {$i < [llength $argv]} {incr i} {
> +       set arg [lindex $argv [expr {$i}]]
>      switch -glob -- $arg {
>         "" { }
>         "--" {
> @@ -12305,11 +12299,25 @@ foreach arg $argv {
>         "--argscmd=*" {
>             set revtreeargscmd [string range $arg 10 end]
>         }
> +       "-C*" {
> +               if {[string length $arg] < 3} {
> +                       incr i
> +                       cd [lindex $argv [expr {$i}]]

What happens if no arguments follow -C (that is, the entire command-line is "gitk -C")? Will this simply run "cd" with no argument or with an empty argument or error out or what? Should there be a check for this condition?

> +                       continue
Why does this 'continue' need to be here? Am I missing something obvious?
Show 20 quoted lines
> +               } else {
> +                       cd [string range $arg 2 end]
> +               }
> +       }
>         default {
>             lappend revtreeargs $arg
>         }
>      }
> -    incr i
> +}
> +
> +# 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."]
> +    exit 1
>  }
>
>  if {$selecthead eq "HEAD"} {
> --
> 1.9.1
Juha-Pekka Heikkila· Nov 6, 2015, 10:49 UTC · re: Eric Sunshine · lore

Re: [PATCH v2] gitk: add -C <path> commandline parameter to change path

On 06.11.2015 11:48, Eric Sunshine wrote:
Show 21 quoted lines
> On Thu, Nov 5, 2015 at 4:19 AM, Juha-Pekka Heikkila
> <juhapekka.heikkila@gmail.com> wrote:
>> This patch adds -C (change working directory) parameter to
>> gitk. With this parameter, instead of need to cd to directory
>> with .git folder, one can point the correct folder from
>> commandline.
>
> Thanks, -C makes more sense than --cwd, and is more consistent with
> other commands.
>
> You'll want to also address the patch to Paul Mackerras (I've cc:'d
> him) since he's the gitk maintainer, and Junio pulls gitk from him.
>
>> v2: Adjusted the parameter as per Eric's suggestion. I think
>>      it now work in similar manner as in many GNU tools as well
>>      as git itself.
>
> This sort of explanation of of changes between versions is definitely
> welcome, but should be placed below the "---" line just under your
> sign-off so that it does not become part of the permanent commit
> message.

Ok, I'll make bit later another version. I didn't realize to check where to put this comment as some projects want these in the commit message.

Show 53 quoted lines
>
>> Signed-off-by: Juha-Pekka Heikkila <juhapekka.heikkila@gmail.com>
>> ---
>
> You'd place your commentary about changes between patch versions here.
> More below...
>
>>   Documentation/gitk.txt |  7 +++++++
>>   gitk-git/gitk          | 26 +++++++++++++++++---------
>>   2 files changed, 24 insertions(+), 9 deletions(-)
>>
>> diff --git a/gitk-git/gitk b/gitk-git/gitk
>> @@ -12279,20 +12279,14 @@ setui $uicolor
>>
>>   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."]
>> -    exit 1
>> -}
>> -
>>   set selecthead {}
>>   set selectheadid {}
>>
>>   set revtreeargs {}
>>   set cmdline_files {}
>> -set i 0
>>   set revtreeargscmd {}
>> -foreach arg $argv {
>> +for {set i 0} {$i < [llength $argv]} {incr i} {
>> +       set arg [lindex $argv [expr {$i}]]
>>       switch -glob -- $arg {
>>          "" { }
>>          "--" {
>> @@ -12305,11 +12299,25 @@ foreach arg $argv {
>>          "--argscmd=*" {
>>              set revtreeargscmd [string range $arg 10 end]
>>          }
>> +       "-C*" {
>> +               if {[string length $arg] < 3} {
>> +                       incr i
>> +                       cd [lindex $argv [expr {$i}]]
>
> What happens if no arguments follow -C (that is, the entire
> command-line is "gitk -C")? Will this simply run "cd" with no argument
> or with an empty argument or error out or what? Should there be a
> check for this condition?
>
>> +                       continue
>
> Why does this 'continue' need to be here? Am I missing something obvious?
>

I'll add the missing try/catch for these cd commands, I slowly start to understand how tcl work. Earlier catch was not for some reason needed here when the loop was ran with 'foreach', error was reported once reached UI but now it seem to advertise "error in startup script.." 'continue' was part of this skipping but doesn't do its trick anymore.

Anyway, I'll make v3 and resend. :)
Show 20 quoted lines
>> +               } else {
>> +                       cd [string range $arg 2 end]
>> +               }
>> +       }
>>          default {
>>              lappend revtreeargs $arg
>>          }
>>       }
>> -    incr i
>> +}
>> +
>> +# 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."]
>> +    exit 1
>>   }
>>
>>   if {$selecthead eq "HEAD"} {
>> --
>> 1.9.1
Juha-Pekka Heikkila· Nov 9, 2015, 11:45 UTC · re: Juha-Pekka Heikkila · lore

[PATCH v3] gitk: add -C <path> commandline parameter to change path

This patch adds -C (change working directory) parameter to gitk. With this parameter, instead of need to cd to directory with .git folder, one can point the correct folder from commandline.

Signed-off-by: Juha-Pekka Heikkila <juhapekka.heikkila@gmail.com>
---
v2: Adjusted the parameter as per Eric's suggestion. I think
    it now work in similar manner as in many GNU tools as well
    as git itself.
v3: catch error from cd and other small refinement.
 Documentation/gitk.txt |  7 +++++++
 gitk-git/gitk          | 30 +++++++++++++++++++++---------
 2 files changed, 28 insertions(+), 9 deletions(-)
Show changes to 2 files +28 −9

Documentation/gitk.txt, gitk-git/gitk

diff --git a/Documentation/gitk.txt b/Documentation/gitk.txt
index 6ade002..d194d9b 100644
--- a/Documentation/gitk.txt
+++ b/Documentation/gitk.txt
@@ -146,6 +146,13 @@ gitk-specific options
 	Select the specified commit after loading the graph.
 	Default behavior is equivalent to specifying '--select-commit=HEAD'.
 
+-C <path>::
+
+	Run as if gitk was started in '<path>' instead of the current
+	working directory. When multiple `-C` options are given, each
+	subsequent non-absolute `-C <path>` is interpreted relative to
+	the preceding `-C <path>`.
+
 Examples
 --------
 gitk v2.6.12.. include/scsi drivers/scsi::
diff --git a/gitk-git/gitk b/gitk-git/gitk
index fcc606e..a7076f8 100755
--- a/gitk-git/gitk
+++ b/gitk-git/gitk
@@ -12279,20 +12279,14 @@ setui $uicolor
 
 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."]
-    exit 1
-}
-
 set selecthead {}
 set selectheadid {}
 
 set revtreeargs {}
 set cmdline_files {}
-set i 0
 set revtreeargscmd {}
-foreach arg $argv {
+for {set i 0} {$i < [llength $argv]} {incr i} {
+	set arg [lindex $argv [expr {$i}]]
     switch -glob -- $arg {
 	"" { }
 	"--" {
@@ -12305,11 +12299,29 @@ foreach arg $argv {
 	"--argscmd=*" {
 	    set revtreeargscmd [string range $arg 10 end]
 	}
+	"-C*" {
+		if {[string length $arg] < 3} {
+			incr i
+			set cwd_path [lindex $argv [expr {$i}]]
+		} else {
+			set cwd_path [string range $arg 2 end]
+		}
+
+		if {[catch {cd $cwd_path}]} {
+			show_error {} . [mc "Cannot change directory to '%s'" $cwd_path]
+			exit 1
+		}
+	}
 	default {
 	    lappend revtreeargs $arg
 	}
     }
-    incr i
+}
+
+# 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."]
+    exit 1
 }
 
 if {$selecthead eq "HEAD"} {
-- 
1.9.1
Paul Mackerras· Dec 19, 2015, 03:13 UTC · re: Juha-Pekka Heikkila · lore

Re: [PATCH v3] gitk: add -C <path> commandline parameter to change path

On Mon, Nov 09, 2015 at 01:45:22PM +0200, Juha-Pekka Heikkila wrote:
Show 6 quoted lines
> This patch adds -C (change working directory) parameter to
> gitk. With this parameter, instead of need to cd to directory
> with .git folder, one can point the correct folder from
> commandline.
> 
> Signed-off-by: Juha-Pekka Heikkila <juhapekka.heikkila@gmail.com>
Thanks.

While I like the idea, there are a couple of minor problems with the patch. First, the Documentation directory is in Junio's tree, not mine, so the change to gitk and the change to Documentation need to be separated. Secondly, please use 4-space indentation rather than 8-space for consistency with the rest of the file. See also the comments below.

> +	"-C*" {
> +		if {[string length $arg] < 3} {
> +			incr i
> +			set cwd_path [lindex $argv [expr {$i}]]
No need to say [expr {$i}] here; [lindex $argv $i] works just fine.

Also, if i is now >= [llength $argv], we'll get an empty string in cwd_path. Is that what you meant? Shouldn't we display an appropriate error message instead of trying to cd to ""?

Paul.

← back to recent threads