-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-topic
executable file
·175 lines (141 loc) · 3.24 KB
/
git-topic
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/tclsh
set silent no
# Strip options and args
set options ""
set args ""
foreach a $argv {
if { [string index $a 0] == "-" } {
lappend options $a
} else {
lappend args $a
}
}
if { "-s" in $options } {
set silent yes
}
if { [catch {exec git rev-parse --show-toplevel} toplevel] } {
if { $silent } {
exit 0
}
puts "git-topic: Not in a git repository"
exit 1
}
# Search for files: .gittopic, .topic, TOPIC
# If begins with [...] then this holds the branch name.
set names {.gittopic .topic TOPIC}
set topicfile ""
foreach n $names {
set fn [file join $toplevel $n]
if { [file exists $fn] } {
set topicfile $fn
break
}
}
# Check extra args
# XXX removed " --points-at HEAD" option, now unsure why. Investigate.
if { [catch {exec git branch | grep ^* | cut -c 3-} curbr] } {
set curbr (none)
} elseif { [string index $curbr 0] == "(" } {
set curbr (detached)
}
# Read mode
if { $args == "" } {
# Read mode. Must have a topicfile
if { $topicfile == "" } {
if { $silent } {
puts "No topic."
} else {
puts "No topic file found ($names)"
}
exit 1
}
# Load topic
set fd [open $topicfile r]
while { [gets $fd line] != -1} {
set line [string trim $line]
if { [string index $line 0] == "\[" } {
set e [string first "\]" $line]
if { $e == -1 } {
puts "($n: invalid syntax)"
exit 1
}
set brname [string range $line 1 $e-1]
set toptext([string trim $brname]) [string trim [string range $line $e+1 end]]
} else {
set toptext(default) $line
}
}
close $fd
if { "-g" in $options || $curbr == "(detached)" } {
set curbr default
}
if { [info exists toptext($curbr)] } {
puts $toptext($curbr)
} elseif { [info exists toptext(default)] } {
puts $toptext(default)
} else {
puts "No topic for $curbr"
}
exit 0
}
# Write mode. Take the current branch
set out_gl ""
set out_for ""
if { "-g" in $options } {
# Set the general topic
if { $topicfile == "" } {
set topiccont ""
} else {
set topiccont [exec cat $topicfile]
}
set topiclines [split $topiccont \n]
# The first line should contain the general topic, others with [branch].
# Simply skip non-branch, set the general
set contents $args\n
foreach l $topiclines {
if { [string trim $l] == "" } {
continue
}
if { [string index $l 0] == {[} } {
append contents $l\n
}
}
set out_gl " global"
} else {
# Branch-related topic
if { $curbr == "(detached)" } {
puts stderr "Current branch is detached, can't set topic!"
exit 1
}
# Unwrap one level case someone has used "".
set ek [catch {llength $args} alen]
if {!$ek && $alen == 1} {
set args [lindex $args 0]
}
if { $topicfile == "" } {
set topiccont ""
} else {
set topiccont [exec cat $topicfile]
}
set topiclines [split $topiccont \n]
set contents ""
# Stream all lines as they are, except the line with current branch.
foreach l $topiclines {
if { [string trim $l] == "" } {
continue
}
if { [string first "\[$curbr\]" $l] == -1 } {
append contents $l\n
}
}
append contents "\[$curbr\] $args\n"
set out_for " for '$curbr'"
}
if { $topicfile == "" } {
set topicfile $toplevel/.topic
}
set fd [open $topicfile w]
puts $fd $contents
close $fd
puts stderr "New$out_gl topic set$out_for: $args"
puts stderr "Written into: $topicfile"