# [PATCH] gitk: fix error when remote tracking branch is deleted

2 messages from 2025-09-13 to 2025-09-14. Participants: Michael Rappazzo, Johannes Sixt.
Thread: https://gitlist.dev/t/64142

## Michael Rappazzo, 2025-09-13 11:31

Subject: [PATCH] gitk: fix error when remote tracking branch is deleted
Message-ID: <20250913113253.74362-1-rappazzo@gmail.com>
URL: https://gitlist.dev/e/20250913113253.74362-1-rappazzo%40gmail.com

```
When a remote tracking branch is deleted (e.g., via 'git push --delete
origin branch'), the headids array entry for that branch is removed, but
upstreamofref may still reference it. This causes gitk to show an error
and prevents the Tags and Heads view from opening.

Fix by checking that headids($upstreamofref($n)) exists before accessing
it in the refill_reflist function.

Signed-off-by: Michael Rappazzo <rappazzo@gmail.com>
---
To reproduce the case:
ˋˋˋ
mkdir sample-repo-remote &&
cd sample-repo-remote &&
git init --bare --initial-branch master && 
cd .. &&
git clone sample-repo-remote sample-repo &&
cd sample-repo &&
git commit --allow-empty -m "Sample commit" &&
git push origin master &&
git push origin master:master2 &&
git branch master2 origin/master2 &&
git push --delete origin master2
ˋˋˋ
Then run gitk --all then bring up the "Tags and Heads" view.


 gitk | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/gitk b/gitk
index 6e4d71d585..3cfc9ef291 100755
--- a/gitk
+++ b/gitk
@@ -10294,7 +10294,9 @@ proc refill_reflist {} {
         if {![string match "remotes/*" $n] && [string match $reflistfilter $n]} {
             if {[commitinview $headids($n) $curview]} {
                 lappend localrefs [list $n H]
-                if {[info exists upstreamofref($n)] && [commitinview $headids($upstreamofref($n)) $curview]} {
+                if {[info exists upstreamofref($n)] && \
+                        [info exists headids($upstreamofref($n))] && \
+                        [commitinview $headids($upstreamofref($n)) $curview]} {
                     lappend trackedremoterefs [list $upstreamofref($n) R]
                 }
             } else {
-- 
2.51.0


```

## Johannes Sixt, 2025-09-14 18:32

Subject: Re: [PATCH] gitk: fix error when remote tracking branch is deleted
Message-ID: <8cd33841-9959-4cd9-99f2-84cac042ceec@kdbg.org>
URL: https://gitlist.dev/e/8cd33841-9959-4cd9-99f2-84cac042ceec%40kdbg.org
In-Reply-To: <20250913113253.74362-1-rappazzo@gmail.com>

```
Am 13.09.25 um 13:31 schrieb Michael Rappazzo:
> When a remote tracking branch is deleted (e.g., via 'git push --delete
> origin branch'), the headids array entry for that branch is removed, but
> upstreamofref may still reference it. This causes gitk to show an error
> and prevents the Tags and Heads view from opening.
> 
> Fix by checking that headids($upstreamofref($n)) exists before accessing
> it in the refill_reflist function.
> 
> Signed-off-by: Michael Rappazzo <rappazzo@gmail.com>

Thank you, this looks reasonable. Queued.

> ---
> To reproduce the case:
> ˋˋˋ
> mkdir sample-repo-remote &&
> cd sample-repo-remote &&
> git init --bare --initial-branch master && 
> cd .. &&
> git clone sample-repo-remote sample-repo &&
> cd sample-repo &&
> git commit --allow-empty -m "Sample commit" &&
> git push origin master &&
> git push origin master:master2 &&
> git branch master2 origin/master2 &&
> git push --delete origin master2
> ˋˋˋ
> Then run gitk --all then bring up the "Tags and Heads" view.
> 
> 
>  gitk | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/gitk b/gitk
> index 6e4d71d585..3cfc9ef291 100755
> --- a/gitk
> +++ b/gitk
> @@ -10294,7 +10294,9 @@ proc refill_reflist {} {
>          if {![string match "remotes/*" $n] && [string match $reflistfilter $n]} {
>              if {[commitinview $headids($n) $curview]} {
>                  lappend localrefs [list $n H]
> -                if {[info exists upstreamofref($n)] && [commitinview $headids($upstreamofref($n)) $curview]} {
> +                if {[info exists upstreamofref($n)] && \
> +                        [info exists headids($upstreamofref($n))] && \
> +                        [commitinview $headids($upstreamofref($n)) $curview]} {
>                      lappend trackedremoterefs [list $upstreamofref($n) R]
>                  }
>              } else {


```
