-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·500 lines (440 loc) · 15.3 KB
/
Copy pathmain.py
File metadata and controls
executable file
·500 lines (440 loc) · 15.3 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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
#!/usr/bin/env python3
import argparse
import os
import sys
from typing import Dict, Optional, List, Set, Tuple
from pathlib import Path
from subprocess import CalledProcessError
import gitrevise
from gitrevise import merge, utils
from gitrevise.utils import EditorError
from gitrevise.odb import Blob, Repository
from gitrevise.merge import rebase, MergeConflict
USAGE = """\
Create branches for commits in @{upstream}..HEAD if their commit message
subject starts with [<topic>] where <topic> is the desired branch name.
"""
SUBJECT_PREFIX_PREFIX = b"["
SUBJECT_PREFIX_SUFFIX = b"]"
CommitEntries = List[Tuple[str, str, str]]
TrimSubject = bool
Dependency = Tuple[str, TrimSubject]
Dependencies = Dict[str, Dependency]
def parse_log(
repo, prefix_prefix, prefix_suffix, *args
) -> Tuple[CommitEntries, Dependencies]:
commit_entries = []
dependency_graph: Dependencies = {}
if "--reverse" not in args:
dependency_graph = None
include_others = "--reverse" not in args
patches = repo.git("log", "-z", "--format=%H %B", *args).decode().split("\x00")
for entry in patches:
tmp = entry.split(maxsplit=1)
if len(tmp) != 2:
continue
commit, message = tmp
raw_subject = message.split("\n\n", maxsplit=1)[0].strip()
words = raw_subject.split(maxsplit=1)
if len(words) < 2:
if include_others:
commit_entries += [(commit, None, raw_subject)]
continue
prefix, subject = words
if not prefix.startswith(prefix_prefix) or not prefix.endswith(prefix_suffix):
if include_others:
commit_entries += [(commit, None, raw_subject)]
continue
prefix = prefix[len(prefix_prefix) : -len(prefix_suffix)]
topic_with_parents = prefix.split(":")
topic = topic_with_parents[0]
parent_topics = [parse_parent_topic(t) for t in topic_with_parents[1:] if t]
if not topic:
if include_others:
commit_entries += [(commit, "", subject)]
continue
commit_entries += [(commit, topic, subject)]
if dependency_graph is not None:
if topic not in dependency_graph:
dependency_graph[topic] = {}
if parent_topics:
dependency_graph[topic].update(parent_topics)
return commit_entries, dependency_graph
def parse_parent_topic(topic: str) -> Dependency:
keep_tag = False
if topic.startswith("+"):
topic = topic[len("+") :]
keep_tag = True
return (topic, keep_tag)
def transitive_dependencies(depgraph: Dependencies, node: Dependency) -> Dependencies:
visited: Dependencies = {}
transitive_dependencies_rec(depgraph, node, visited)
return visited
def transitive_dependencies_rec(
depgraph: Dependencies, node: Dependency, visited: Dependencies
) -> None:
name, keep_tag = node
if name in visited:
return
visited[name] = keep_tag
if name in depgraph:
for x in depgraph[name].items():
transitive_dependencies_rec(depgraph, x, visited)
class BranchWasModifiedError(Exception):
pass
class InvalidRangeError(Exception):
pass
class TopicNotFoundError(Exception):
pass
def validate_cache(repo, topic_set, force):
cache_path = repo.gitdir / "branchstack-cache"
if not os.path.exists(cache_path):
return
cached_shas = [
(w[0], w[1])
for w in map(
lambda line: line.split(), cache_path.read_bytes().decode().splitlines()
)
]
existing_branches = {}
refs = repo.git(
"for-each-ref",
"--format",
"%(refname:short) %(objectname)",
*[f"refs/heads/{t[0]}" for t in cached_shas],
)
for line in refs.decode().splitlines():
refname, sha = line.split(" ", maxsplit=1)
existing_branches[refname] = sha
for topic, cached_sha in cached_shas:
if topic not in existing_branches:
continue
if topic not in topic_set: # The user did not ask to create this branch.
continue
current_sha = existing_branches[topic]
if current_sha == cached_sha:
continue
if force:
print(f"Will overwrite modified branch {topic}")
else:
raise BranchWasModifiedError(topic)
def update_cache(repo, topics):
cache_path = repo.gitdir / "branchstack-cache"
mode = "rb+" if cache_path.exists() else "wb+"
with open(cache_path, mode) as f:
cached_shas = {
w[0]: w[1] for w in map(lambda line: line.split(), f.read().decode().splitlines())
}
new_topics = cached_shas
for t in topics:
if topics[t] is not None:
new_topics[t] = topics[t]
new_content = ""
for topic in new_topics:
sha = new_topics[topic]
if sha is not None:
new_content += f"{topic} {sha}{os.linesep}"
f.seek(0)
f.truncate()
f.write(new_content.encode())
def trimmed_message(subject: str, message: bytes) -> str:
body = b"\n".join(message.split(b"\n\n", maxsplit=1)[1:])
if body:
return subject.encode() + b"\n\n" + body
return subject.encode()
def create_branches(
repo,
current_branch,
base_commit,
tip="HEAD",
branches=None,
force=False,
keep_tags=None,
) -> None:
prefix_prefix = repo.config(
"branchstack.subjectPrefixPrefix",
default=SUBJECT_PREFIX_PREFIX,
).decode()
prefix_suffix = repo.config(
"branchstack.subjectPrefixSuffix",
default=SUBJECT_PREFIX_SUFFIX,
).decode()
commit_entries, dependency_graph = parse_log(
repo, prefix_prefix, prefix_suffix, f"{base_commit}..{tip}", "--reverse"
)
def by_first_commit_on_topic(commit_entry):
(commit, topic, subject) = commit_entry
for i in range(len(commit_entries)):
if commit_entries[i][1] == topic:
return i
return -1
commit_entries.sort(key=by_first_commit_on_topic)
topics = {commit_entry[1]: None for commit_entry in commit_entries}
all_topics = set(topics)
topic_set = all_topics
if branches:
for topic in branches:
if topic not in all_topics:
raise TopicNotFoundError(topic, base_commit, tip)
topic_set = set()
for topic in branches:
topic_set.add(topic)
topics = {t: None for t in topics if t in topic_set}
for child in topics:
for parent in dependency_graph[child]:
if parent not in all_topics:
print(f"Warning: topic '{child}' depends on missing topic '{parent}'.")
assert current_branch is None or current_branch not in set(
topics
), f"Refusing to overwrite current branch {current_branch}"
base_commit_id = repo.git("rev-parse", base_commit).decode()
validate_cache(repo, topic_set, force)
try:
for topic in topics:
create_branch(
repo,
prefix_prefix,
prefix_suffix,
keep_tags,
base_commit_id,
commit_entries,
topics,
dependency_graph,
topic,
)
finally:
update_cache(repo, topics)
for topic in topics:
print(topic)
for line in (
repo.git("log", f"{base_commit}..refs/heads/{topic}", "--oneline")
.decode()
.splitlines()
):
print("\t", line)
def create_branch(
repo,
prefix_prefix,
prefix_suffix,
keep_tags,
base_commit_id,
commit_entries,
topics,
dependency_graph,
topic,
):
head = repo.get_commit(base_commit_id)
deps = transitive_dependencies(dependency_graph, (topic, False))
for commit, t, subject in commit_entries:
if t not in deps:
continue
keep_tag = deps[t]
patch = repo.get_commit(commit)
def on_conflict(path):
"""
Some commit in "base_commit..commit~" must have touched the
path as well, but is not among our dependencies.
"""
print("Missing dependency on one of the commits below?")
log, _ = parse_log(
repo,
prefix_prefix,
prefix_suffix,
f"{base_commit_id}..{commit}~",
"--",
path,
)
for id, topic, subject in log:
if topic not in deps:
prefix = (
""
if topic is None
else f"{prefix_prefix}{topic}{prefix_suffix} "
)
print(f"\t{id[:7]} {prefix}{subject}")
global ON_CONFLICT
ON_CONFLICT = on_conflict
head = rebase(patch, head)
message = head.message
keep_tag = (
keep_tag
or (keep_tags == "dependencies" and t != topic)
or keep_tags == "all"
)
if not keep_tag:
message = trimmed_message(subject, patch.message)
head = repo.new_commit(
message=message,
tree=head.tree(),
parents=head.parents(),
author=head.author,
committer=patch.committer, # preserve original committer and timestamp
)
topic_fqn = f"refs/heads/{topic}"
if not repo.git("branch", "--list", topic):
repo.git("branch", topic, base_commit_id)
topic_ref = repo.get_commit_ref(topic_fqn)
if head.oid != topic_ref.target.oid:
topic_oid = topic_ref.target.oid
print(f"Updating {topic_ref.name} ({topic_oid} => {head.oid})")
topic_ref.update(head, "git-branchstack rewrite")
topics[topic] = topic_ref.target.oid
ON_CONFLICT = None
def override_merge_blobs(
path: Path,
labels: Tuple[str, str, str],
current: Blob,
base: Optional[Blob],
other: Blob,
) -> Blob:
repo = current.repo
tmpdir = repo.get_tempdir()
annotated_labels = (
f"{path} (new parent): {labels[0]}",
f"{path} (old parent): {labels[1]}",
f"{path} (current): {labels[2]}",
)
(is_clean_merge, merged) = merge.merge_files(
repo,
annotated_labels,
current.body,
base.body if base else b"",
other.body,
tmpdir,
)
if is_clean_merge:
# No conflicts.
return Blob(repo, merged)
try:
path = path.relative_to("/")
except ValueError:
pass
# At this point, we know that there are merge conflicts to resolve.
# Prompt to try and trigger manual resolution.
print(f"Conflict applying '{labels[2]}'")
print(f" Path: '{path}'")
preimage = merged
(normalized_preimage, conflict_id, merged_blob) = merge.replay_recorded_resolution(
repo, tmpdir, preimage
)
if merged_blob is not None:
return merged_blob
ON_CONFLICT(path)
if input(" Edit conflicted file? (Y/n) ").lower() == "n":
raise MergeConflict("user aborted")
# Open the editor on the conflicted file. We ensure the relative path
# matches the path of the original file for a better editor experience.
conflicts = tmpdir / "conflict" / path
conflicts.parent.mkdir(parents=True, exist_ok=True)
conflicts.write_bytes(preimage)
merged = utils.edit_file(repo, conflicts)
# Print warnings if the merge looks like it may have failed.
if merged == preimage:
print("(note) conflicted file is unchanged")
if b"<<<<<<<" in merged or b"=======" in merged or b">>>>>>>" in merged:
print("(note) conflict markers found in the merged file")
# Was the merge successful?
if input(" Merge successful? (y/N) ").lower() != "y":
raise MergeConflict("user aborted")
merge.record_resolution(repo, conflict_id, normalized_preimage, merged)
return Blob(current.repo, merged)
gitrevise.merge.merge_blobs = override_merge_blobs
def dwim(repo: Repository) -> Tuple[str, str]:
rebase_dir = repo.gitdir / "rebase-merge"
if os.path.exists(rebase_dir):
branch = os.path.basename((rebase_dir / "head-name").read_text().strip())
base_commit = os.path.basename((rebase_dir / "onto").read_text().strip())
else:
branch = repo.git("symbolic-ref", "--short", "HEAD").decode()
base_commit = "@{upstream}"
return branch, base_commit
def parser() -> argparse.ArgumentParser:
p = argparse.ArgumentParser(
prog="git branchstack",
description=USAGE,
formatter_class=argparse.RawDescriptionHelpFormatter,
)
p.add_argument(
"<topic>",
nargs="*",
help="only create the given branches",
)
p.add_argument(
"--force",
"-f",
action="store_true",
help="overwrite branches even if they were modified since the last run",
)
p.add_argument(
"--keep-tags",
"-k",
metavar="dependencies|all",
nargs="?",
const="dependencies",
help="keep topic tag on created commits",
)
p.add_argument(
"--range",
"-r",
metavar="<rev1>..<rev2>",
help="use commits from the given range instead of @{upstream}..",
)
return p
def parse_range(repo: Repository, range: str) -> Tuple[str, str]:
if ".." not in range:
raise InvalidRangeError(range)
upper, lower = repo.git("rev-parse", range).decode().splitlines()
assert lower.startswith("^")
return lower[len("^") :], upper
def main(argv: Optional[List[str]] = None):
args = parser().parse_args(argv)
try:
with Repository() as repo:
if args.range is None:
branch, base_commit = dwim(repo)
tip = "HEAD"
else:
branch = None
base_commit, tip = parse_range(repo, args.range)
if args.keep_tags is not None:
if args.keep_tags not in ("dependencies", "all"):
print(
"argument to --keep-tags must be one of 'dependencies' (the default) or 'all'"
)
sys.exit(1)
base_commit = repo.git("merge-base", "--", base_commit, "HEAD").decode()
create_branches(
repo,
branch,
base_commit,
tip,
getattr(args, "<topic>"),
force=args.force,
keep_tags=args.keep_tags,
)
except BranchWasModifiedError as err:
print(
f"error: generated branch {err} has been modified. Use --force to overwrite."
)
sys.exit(1)
except CalledProcessError as err:
print(f"subprocess exited with non-zero status: {err.returncode}")
sys.exit(1)
except EditorError as err:
print(f"editor error: {err}")
sys.exit(1)
except InvalidRangeError as err:
print(f'invalid commit range: {err} should be a valid "a..b" range')
sys.exit(1)
except MergeConflict as err:
print(f"merge conflict: {err}")
sys.exit(1)
except TopicNotFoundError as err:
topic, base_commit, tip = err.args
print(f"error: topic '{topic}' not found {base_commit}..{tip}")
sys.exit(1)
except ValueError as err:
print(f"invalid value: {err}")
sys.exit(1)
if __name__ == "__main__":
main()