Skip to content

Commit a0db0d6

Browse files
committed
git-gui: Generate blame on uncommitted working tree file
If the user doesn't give us a revision parameter to our blame subcommand then we can generate blame against the working tree file by passing the file path off to blame with the --contents argument. In this case we cannot obtain the contents of the file from the ODB; instead we must obtain the contents by reading the working directory file as-is. Signed-off-by: Shawn O. Pearce <[email protected]>
1 parent 3e45ee1 commit a0db0d6

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

git-gui.sh

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1605,6 +1605,7 @@ browser {
16051605
}
16061606
blame {
16071607
set subcommand_args {rev? path?}
1608+
set head {}
16081609
set path {}
16091610
set is_path 0
16101611
foreach a $argv {
@@ -1614,27 +1615,30 @@ blame {
16141615
break
16151616
} elseif {$a eq {--}} {
16161617
if {$path ne {}} {
1617-
if {$current_branch ne {}} usage
1618-
set current_branch $path
1618+
if {$head ne {}} usage
1619+
set head $path
16191620
set path {}
16201621
}
16211622
set is_path 1
1622-
} elseif {$current_branch eq {}} {
1623-
if {$current_branch ne {}} usage
1624-
set current_branch $a
1623+
} elseif {$head eq {}} {
1624+
if {$head ne {}} usage
1625+
set head $a
16251626
} else {
16261627
usage
16271628
}
16281629
}
16291630
unset is_path
16301631

1631-
if {$current_branch eq {} && $path ne {}} {
1632+
if {$head eq {}} {
16321633
set current_branch [git symbolic-ref HEAD]
16331634
regsub ^refs/((heads|tags|remotes)/)? \
16341635
$current_branch {} current_branch
1636+
} else {
1637+
set current_branch $head
16351638
}
1636-
if {$current_branch eq {} || $path eq {}} usage
1637-
blame::new $current_branch $path
1639+
1640+
if {$path eq {}} usage
1641+
blame::new $head $path
16381642
return
16391643
}
16401644
citool -

lib/blame.tcl

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,12 @@ constructor new {i_commit i_path} {
170170
bind $top <Visibility> "focus $top"
171171
bind $top <Destroy> [list delete_this $this]
172172

173-
set cmd [list git cat-file blob "$commit:$path"]
174-
set fd [open "| $cmd" r]
173+
if {$commit eq {}} {
174+
set fd [open $path r]
175+
} else {
176+
set cmd [list git cat-file blob "$commit:$path"]
177+
set fd [open "| $cmd" r]
178+
}
175179
fconfigure $fd -blocking 0 -translation lf -encoding binary
176180
fileevent $fd readable [cb _read_file $fd]
177181
}
@@ -194,7 +198,13 @@ method _read_file {fd} {
194198
if {[eof $fd]} {
195199
close $fd
196200
_status $this
197-
set cmd [list git blame -M -C --incremental $commit -- $path]
201+
set cmd [list git blame -M -C --incremental]
202+
if {$commit eq {}} {
203+
lappend cmd --contents $path
204+
} else {
205+
lappend cmd $commit
206+
}
207+
lappend cmd -- $path
198208
set fd [open "| $cmd" r]
199209
fconfigure $fd -blocking 0 -translation lf -encoding binary
200210
fileevent $fd readable [cb _read_blame $fd]

0 commit comments

Comments
 (0)