-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgit-reftrack
More file actions
executable file
·261 lines (214 loc) · 8.41 KB
/
git-reftrack
File metadata and controls
executable file
·261 lines (214 loc) · 8.41 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#!/usr/bin/env python3
"""
The purpose of this script is to help track rebases of a local branch using a
special meta-branch called a 'reftrack' branch. This form of tracking can be
considered as a glorified 'reflog', that is also publishable, better described,
and an alternative to littering a repository with adhoc tags of versions that
you may not want to keep forever.
The script is invoked simply via:
git-reftrack commit -b [base]
Where `base` is the base of the current branch (i.e the most recent commit that
this branch does not add). The commits in the branch are calculated from
`[base]..HEAD`.
When the current branch is named 'x', the created or updated reftrack branch
is named 'x.reftrack'.
The structure of the reftrack branch is the following;
* Each commit is a merge commit with the following parents
[prev-reftrack] [version] [base]
Or
[version] [base]
For the first commit.
* The commit message is of the format:
```
git-reftrack: {subject}
Base: {ref description}
Commits:
{list of commits in the branch}
```
"""
import sys
import os
import tempfile
import re
from optparse import OptionParser
def system(cmd):
return os.system(cmd)
class Abort(Exception): pass
class RefTrack(object):
def __init__(self):
pass
def current_branch(self):
current_branch = os.popen("git branch --show-current").read().strip()
if not current_branch:
print("No current branch, aborted", file=sys.stderr)
raise Abort()
return current_branch
def get_base(self, suggested_base):
current_branch = self.current_branch()
base = suggested_base
if not base:
base = f"{current_branch}.base"
base_state = os.popen(f"git show-ref {base}").read().strip()
if base_state == "":
print(f"Did not find a default 'base' branch {base}", file=sys.stderr)
raise Abort()
else:
base_state = os.popen(f"git show-ref {base}").read().strip()
if base_state == "":
print(f"Base {base} is invalid", file=sys.stderr)
raise Abort()
githash = base_state.split(' ', 1)[0]
# It may be a git tag, we need a commit hash:
base = os.popen(f"git rev-list -n 1 {githash}").read().strip()
return (base, base_state)
def get_reftrack_state(self):
current_branch = self.current_branch()
reftrack_branch = f"{current_branch}.reftrack"
reftrack_state = os.popen(f"git show-ref {reftrack_branch}").read().strip()
return (reftrack_branch, reftrack_state)
def get_versions(self):
(reftrack_branch, reftrack_state) = self.get_reftrack_state()
cmd = f"git log --first-parent {reftrack_branch} --pretty='%H: [%ci] %s' --merges --grep='^git-reftrack:'"
lst = os.popen(cmd).readlines()
v = []
for (idx, line) in enumerate(lst):
line = line.strip()
if not line:
continue
(h, message) = line.split(': ', 1)
v.append((len(lst) - idx, h[:12], message))
return v
def get_versions_dict(self):
v = {}
for (id, commit_hash, message) in self.get_versions():
v[str(id)] = (commit_hash, message)
return v
def get_reftrack_commits(self, git_hash):
cmd = f"git show --pretty='%P' {git_hash}"
parents = os.popen(cmd).read().strip().split(' ')
if len(parents) == 3:
(a, b) = (parents[1], parents[2])
else:
(a, b) = (parents[0], parents[1])
commits = []
for line in os.popen(f"git log --oneline {b}..{a} --pretty='%H: [%ci] %s' --no-decorate").readlines():
(h, message) = line.split(': ', 1)
commits.append((h, message))
return commits
def commit(args):
parser = OptionParser()
parser.add_option("-b", "--base", dest="base", help="base branch")
(options, args) = parser.parse_args(args)
rt = RefTrack()
current_branch = rt.current_branch()
(base, base_state) = rt.get_base(options.base)
(reftrack_branch, reftrack_state) = rt.get_reftrack_state()
state = "HEAD"
tf = tempfile.NamedTemporaryFile()
tempname = tf.name
first_line = f"git-reftrack: \n\n".encode('utf-8')
tf.write(f"git-reftrack: \n\n".encode('utf-8'))
tf.write(f"Base: {base_state}\n".encode('utf-8'))
tf.write(f"Commits:\n\n".encode('utf-8'))
changes = os.popen(f"git log --oneline {base}..{state} --no-decorate").read().strip()
tf.write((changes + "\n").encode('utf-8'))
tf.flush()
editor = os.popen("git config core.editor").read().strip()
system(f"{editor} {tempname}")
first_line = open(tempname).readlines()[0]
m = re.match("git-reftrack: .+", first_line)
if not m:
print("commit message not valid, aborted", file=sys.stderr)
return
cmd = f'git commit-tree {state}^{{tree}} -F {tempname}'
if reftrack_state != '':
cmd += f' -p {reftrack_branch}'
cmd += f" -p {state} -p {base}"
commit_object = os.popen(cmd).read().strip()
if commit_object == "":
raise Exception("error creating commit object")
cmd = f"git update-ref refs/heads/{reftrack_branch} {commit_object}"
r = system(cmd)
tf.close()
print(f"Branch {reftrack_branch} updated.")
def log(args):
parser = OptionParser()
rt = RefTrack()
for (id, commit_hash, message) in rt.get_versions():
print(id, commit_hash, message)
def diff_reftracks(rt, source, dest):
v = rt.get_versions_dict()
a = rt.get_reftrack_commits(v[source][0])
b = rt.get_reftrack_commits(v[dest][0])
outputs = []
for z in [a, b]:
tf = tempfile.NamedTemporaryFile()
for commit in z:
(patch_id, commit_id) = os.popen(f"git show {commit[0]} | git patch-id").read().strip().split(' ')
tf.write((patch_id + "\n").encode('utf-8'))
tf.flush()
outputs.append(tf)
first_idx = 0
second_idx = 0
for line in os.popen(f"diff -U 10000 -durN {outputs[0].name} {outputs[1].name}").readlines()[3:]:
f_id = ("%s:%d" % (source, first_idx))
s_id = ("%s:%d" % (dest, second_idx))
if line.startswith(' '):
patch_id = line.strip()
print(' ', 'Id:' + patch_id[:12], " ", a[first_idx][0][:12], a[first_idx][1].strip())
first_idx += 1
second_idx += 1
elif line.startswith('+'):
patch_id = line.strip()[1:]
print('\u001b[32m', end='')
print('+', 'Id:' +patch_id[:12], ("%6s" % s_id), b[second_idx][0][:12], b[second_idx][1].strip())
print('\u001b[0m', end='')
second_idx += 1
elif line.startswith('-'):
patch_id = line.strip()[1:]
print('\u001b[31m', end='')
print('-', 'Id:' +patch_id[:12], ("%6s" % f_id), a[first_idx][0][:12], a[first_idx][1].strip())
print('\u001b[0m', end='')
first_idx += 1
def diff(*args):
parser = OptionParser()
rt = RefTrack()
v = {}
for (id, commit_hash, message) in rt.get_versions():
v[str(id)] = (commit_hash, message)
source = args[0]
dest = args[1]
if not (':' in source and ":" in dest):
return diff_reftracks(rt, source, dest)
v = rt.get_versions_dict()
(source, source_p) = source.split(':', 1)
(dest, dest_p) = dest.split(':', 1)
a = rt.get_reftrack_commits(v[source][0])
b = rt.get_reftrack_commits(v[dest][0])
source_commit = a[int(source_p)][0]
dest_commit = b[int(dest_p)][0]
tf1 = tempfile.NamedTemporaryFile()
tf1.write(os.popen(f"git show {source_commit} --no-decorate").read().encode('utf-8'))
tf1.flush()
tf2 = tempfile.NamedTemporaryFile()
tf2.write(os.popen(f"git show {dest_commit} --no-decorate").read().encode('utf-8'))
tf2.flush()
print(os.popen(f"diff -urN {tf1.name} {tf2.name}").read())
def main():
parser = OptionParser()
if len(sys.argv) == 1:
print("commit - commit a new reftrack change")
print(" log - show a log of reftrack changes")
print(" diff - help to diff two reftrack versions")
return
if sys.argv[1] == "commit":
commit(sys.argv[2:])
elif sys.argv[1] == "log":
log(sys.argv[2:])
elif sys.argv[1] == "diff":
diff(sys.argv[2], sys.argv[3])
else:
print(f"unknown command {sys.argv[1]}", file=sys.stderr)
sys.exit(-1)
if __name__ == "__main__":
main()