Patchwork [8,of,9] hgk: simplify tags parser

mail settings
Submitter Andrew Shadura
Date March 10, 2013, 4:24 p.m.
Message ID <30be85ab65b1784810b9.1362932689@localhost.localdomain>
Download mbox | patch
Permalink /patch/1106/
State Accepted
Commit 962844e8dd8ecc6b1fbdaf73cbfd2963f937d7df
Headers show


Andrew Shadura - March 10, 2013, 4:24 p.m.
# HG changeset patch
# User Andrew Shadura <>
# Date 1350420104 -7200
# Node ID 30be85ab65b1784810b95eb38774dddf94a5d09b
# Parent  a057824795b78546bd3647e368f03ed7926a34fa
hgk: simplify tags parser.

As when hg tags is being called without -v option, it returns
lines with two elements in each, we can just interate them as
if it were a usual Tcl list using foreach and two variables.
Line endings and whitespace don't matter when doing so, so we
may keep them as is.

When we're processing the lines, tag variable is assigned a tag
name, and rev is a string in form of revision:hash which we can
split on colon. As Tcl8.4 lacks lassign command, and using lindex
makes code a bit less readable, we use foreach to iterate over
two-element list.


diff --git a/contrib/hgk b/contrib/hgk
--- a/contrib/hgk
+++ b/contrib/hgk
@@ -456,16 +456,13 @@  proc readrefs {} {
             exit 2
-    regsub -all "\r\n" $tags "\n" tags
-    set lines [split $tags "\n"]
-    foreach f $lines {
-	regexp {(\S+)$} $f full
-	regsub {\s+(\S+)$} $f "" direct
-	set sha [split $full ':']
-	set tag [lindex $sha 1]
-	lappend tagids($direct) $tag
-	lappend idtags($tag) $direct
+    foreach {tag rev} $tags {
+        # we use foreach as Tcl8.4 doesn't support lassign
+        foreach {- id} [split $rev :] {
+            lappend tagids($tag) $id
+            lappend idtags($id) $tag
+        }
     set status [catch {exec $env(HG) --config ui.report_untrusted=false heads} heads]