git-gui: Better handling of detached HEAD
If the current branch is not a symbolic-ref that points to a name in the refs/heads/ namespace we now just assume that the head is a detached head. In this case we return the special branch name of HEAD rather than empty string, as HEAD is a valid revision specification and the empty string is not. I have also slightly improved the current-branch function by using string functions to parse the symbolic-ref data. This should be slightly faster than using a regsub. I think the code is clearer too. Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Shawn O. Pearce committed
Jul 5, 2007 at 19:19 UTC
311e02a4a5c7b82c996214f06d58e2b51b3ecc22
1 file changed
+14
-4
git-gui.sh
+14
-4
@@ -285,14 +285,24 @@ proc git {args} {
285
}
286
287
proc current-branch {} {
288
- set ref {}
288
set fd [open [gitdir HEAD] r]
290
- if {[gets $fd ref] <16
291
- || ![regsub {^ref: refs/heads/} $ref {} ref]} {
289
+ if {[gets $fd ref] < 1} {
290
set ref {}
291
}
292
close $fd
295
- return $ref
293
+
294
+ set pfx {ref: refs/heads/}
295
+ set len [string length $pfx]
296
+ if {[string equal -length $len $pfx $ref]} {
297
+ # We're on a branch. It might not exist. But
298
+ # HEAD looks good enough to be a branch.
299
+ #
300
+ return [string range $ref $len end]
301
+ } else {
302
+ # Assume this is a detached head.
303
+ #
304
+ return HEAD
305
+ }
306
}
307
308
auto_load tk_optionMenu