Skip to content

Commit 3e45ee1

Browse files
committed
git-gui: Smarter command line parsing for browser, blame
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 <[email protected]>
1 parent c612785 commit 3e45ee1

File tree

1 file changed

+47
-9
lines changed

1 file changed

+47
-9
lines changed

git-gui.sh

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1578,25 +1578,63 @@ bind all <$M1B-Key-Q> do_quit
15781578
bind all <$M1B-Key-w> {destroy [winfo toplevel %W]}
15791579
bind all <$M1B-Key-W> {destroy [winfo toplevel %W]}
15801580

1581+
set subcommand_args {}
1582+
proc usage {} {
1583+
puts stderr "usage: $::argv0 $::subcommand $::subcommand_args"
1584+
exit 1
1585+
}
1586+
15811587
# -- Not a normal commit type invocation? Do that instead!
15821588
#
15831589
switch -- $subcommand {
15841590
browser {
1585-
if {[llength $argv] != 1} {
1586-
puts stderr "usage: $argv0 browser commit"
1587-
exit 1
1591+
set subcommand_args {rev?}
1592+
switch [llength $argv] {
1593+
0 {
1594+
set current_branch [git symbolic-ref HEAD]
1595+
regsub ^refs/((heads|tags|remotes)/)? \
1596+
$current_branch {} current_branch
1597+
}
1598+
1 {
1599+
set current_branch [lindex $argv 0]
1600+
}
1601+
default usage
15881602
}
1589-
set current_branch [lindex $argv 0]
15901603
browser::new $current_branch
15911604
return
15921605
}
15931606
blame {
1594-
if {[llength $argv] != 2} {
1595-
puts stderr "usage: $argv0 blame commit path"
1596-
exit 1
1607+
set subcommand_args {rev? path?}
1608+
set path {}
1609+
set is_path 0
1610+
foreach a $argv {
1611+
if {$is_path || [file exists $_prefix$a]} {
1612+
if {$path ne {}} usage
1613+
set path $a
1614+
break
1615+
} elseif {$a eq {--}} {
1616+
if {$path ne {}} {
1617+
if {$current_branch ne {}} usage
1618+
set current_branch $path
1619+
set path {}
1620+
}
1621+
set is_path 1
1622+
} elseif {$current_branch eq {}} {
1623+
if {$current_branch ne {}} usage
1624+
set current_branch $a
1625+
} else {
1626+
usage
1627+
}
1628+
}
1629+
unset is_path
1630+
1631+
if {$current_branch eq {} && $path ne {}} {
1632+
set current_branch [git symbolic-ref HEAD]
1633+
regsub ^refs/((heads|tags|remotes)/)? \
1634+
$current_branch {} current_branch
15971635
}
1598-
set current_branch [lindex $argv 0]
1599-
blame::new $current_branch $_prefix[lindex $argv 1]
1636+
if {$current_branch eq {} || $path eq {}} usage
1637+
blame::new $current_branch $path
16001638
return
16011639
}
16021640
citool -

0 commit comments

Comments
 (0)