git-gui: Smarter command line parsing for browser, blame
authorShawn O. Pearce <spearce@spearce.org>
Wed, 9 May 2007 02:36:01 +0000 (22:36 -0400)
committerShawn O. Pearce <spearce@spearce.org>
Wed, 9 May 2007 02:36:01 +0000 (22:36 -0400)
The browser subcommand now optionally accepts a single revision
argument; if no revision argument is supplied then we use the
current branch as the tree to browse.  This is very common, so
its a nice option.

Our blame subcommand now tries to perform the same assumptions
as the command line git-blame; both the revision and the file
are optional.  We assume the argument is a filename if the file
exists in the working directory, otherwise we assume the argument
is a revision name.  A -- can be supplied between the two to force
parsing, or before the filename to force it to be a filename.

Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
git-gui.sh

index 8f33f81c58c54aef6bb8f272bf9ae47c08d42a62..e90cdde96c46af611aba61299303ebee650c4fcb 100755 (executable)
@@ -1578,25 +1578,63 @@ bind all <$M1B-Key-Q> do_quit
 bind all <$M1B-Key-w> {destroy [winfo toplevel %W]}
 bind all <$M1B-Key-W> {destroy [winfo toplevel %W]}
 
+set subcommand_args {}
+proc usage {} {
+       puts stderr "usage: $::argv0 $::subcommand $::subcommand_args"
+       exit 1
+}
+
 # -- Not a normal commit type invocation?  Do that instead!
 #
 switch -- $subcommand {
 browser {
-       if {[llength $argv] != 1} {
-               puts stderr "usage: $argv0 browser commit"
-               exit 1
+       set subcommand_args {rev?}
+       switch [llength $argv] {
+       0 {
+               set current_branch [git symbolic-ref HEAD]
+               regsub ^refs/((heads|tags|remotes)/)? \
+                       $current_branch {} current_branch
+       }
+       1 {
+               set current_branch [lindex $argv 0]
+       }
+       default usage
        }
-       set current_branch [lindex $argv 0]
        browser::new $current_branch
        return
 }
 blame {
-       if {[llength $argv] != 2} {
-               puts stderr "usage: $argv0 blame commit path"
-               exit 1
+       set subcommand_args {rev? path?}
+       set path {}
+       set is_path 0
+       foreach a $argv {
+               if {$is_path || [file exists $_prefix$a]} {
+                       if {$path ne {}} usage
+                       set path $a
+                       break
+               } elseif {$a eq {--}} {
+                       if {$path ne {}} {
+                               if {$current_branch ne {}} usage
+                               set current_branch $path
+                               set path {}
+                       }
+                       set is_path 1
+               } elseif {$current_branch eq {}} {
+                       if {$current_branch ne {}} usage
+                       set current_branch $a
+               } else {
+                       usage
+               }
+       }
+       unset is_path
+
+       if {$current_branch eq {} && $path ne {}} {
+               set current_branch [git symbolic-ref HEAD]
+               regsub ^refs/((heads|tags|remotes)/)? \
+                       $current_branch {} current_branch
        }
-       set current_branch [lindex $argv 0]
-       blame::new $current_branch $_prefix[lindex $argv 1]
+       if {$current_branch eq {} || $path eq {}} usage
+       blame::new $current_branch $path
        return
 }
 citool -