@matheusbsbs / matheus-bagliano / commits / 3e45ee1ef2
Fork 0

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 <spearce@spearce.org>

Shawn O. Pearce committed May 8, 2007 at 22:36 UTC 3e45ee1ef268c73539903f89c737af89034d391f
1 file changed +47 -9
git-gui.sh
+47 -9
@@ -1578,25 +1578,63 @@ bind all <$M1B-Key-Q> do_quit
1578 bind all <$M1B-Key-w> {destroy [winfo toplevel %W]}
1579 bind all <$M1B-Key-W> {destroy [winfo toplevel %W]}
1580
1581 +set subcommand_args {}
1582 +proc usage {} {
1583 + puts stderr "usage: $::argv0 $::subcommand $::subcommand_args"
1584 + exit 1
1585 +}
1586 +
1587 # -- Not a normal commit type invocation? Do that instead!
1588 #
1589 switch -- $subcommand {
1590 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
1602 }
1589 - set current_branch [lindex $argv 0]
1603 browser::new $current_branch
1604 return
1605 }
1606 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
1635 }
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
1638 return
1639 }
1640 citool -