-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-tripconfig
executable file
·102 lines (84 loc) · 2.16 KB
/
git-tripconfig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/tclsh
set opt ""
if { "-l" in $argv } {
set opt "--local "
} elseif { "-g" in $argv } {
set opt "--global "
}
set cmd "exec git config ${opt}-l"
set confitems [split [{*}$cmd] \n]
set config ""
foreach i $confitems {
set value [lassign [split $i =] name]
if { [llength $value] != 1 } {
set value [join $value =]
}
set dictpath [split $name .]
# Fix the path if there's something quite wrong
set first [lindex $dictpath 0]
if { $first in {branch remote submodule} } {
# Take care of that there are exactly THREE elements.
# If there are more than three, it means that some inside contained a dot.
if { [llength $dictpath] > 3 } {
set last [lindex $dictpath end]
set mid [lrange $dictpath 1 end-1]
set dictpath [list $first [join $mid .] $last]
}
}
dict set config {*}$dictpath $value
#puts "\[[join $dictpath {]/[}]\] = $value"
}
proc normalize v {
# Check if the string doesn't contain ""
# If so, use escape characters.
# Unless they are only in the beginning and the end.
set charset [split $v ""]
set quotes [lsearch -all $charset "\""]
if { [llength $quotes] == 2
|| [lindex $quotes 0] == 0
|| [lindex $quotes 1] == [llength $charset]-1 } {
# go on
} else {
#puts stderr "In String: '$v'"
#puts stderr "Found quotes at: $quotes"
set has [expr {$quotes != ""}]
if { $has } {
#puts stderr "Source list: $charset"
# replace with backquotes
foreach ix $quotes {
set charset [lreplace $charset $ix $ix "\\\""]
}
#puts stderr "Processed list: $charset"
set v [join $charset ""]
#puts stderr "Processed string: $v"
}
}
if { [string index $v 0] == "\{" } {
return "\"[string range $v 1 end-1]\""
}
foreach t {" " "\{" "\}" =} {
if { [string first $t $v] != -1 } {
return "\"$v\""
}
}
return $v
}
proc print_dict {tabs d} {
set otabs $tabs
set tabs " $tabs"
set out "{\n"
foreach k [dict keys $d] {
set v [dict get $d $k]
if { [llength $v]%2 == 0 } {
# Subdict
set subdict [print_dict $tabs $v]
append out "$tabs$k $subdict\n"
} else {
set v [normalize $v]
append out "$tabs$k = $v\n"
}
}
append out "$otabs}\n"
return $out
}
puts "git [print_dict {} $config]"