-
-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathreset.py
138 lines (121 loc) · 4.79 KB
/
reset.py
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
import sys
from enum import Enum
import git
import manim as m
from git_sim.enums import ResetMode
from git_sim.git_sim_base_command import GitSimBaseCommand
from git_sim.settings import settings
class Reset(GitSimBaseCommand):
def __init__(
self, commit: str, mode: ResetMode, soft: bool, mixed: bool, hard: bool
):
super().__init__()
self.commit = commit
self.mode = mode
settings.hide_merged_branches = True
try:
self.resetTo = git.repo.fun.rev_parse(self.repo, self.commit)
except git.exc.BadName:
print(
f"git-sim error: '{self.commit}' is not a valid Git ref or identifier."
)
sys.exit(1)
self.commitsSinceResetTo = list(self.repo.iter_commits(self.commit + "...HEAD"))
self.n = self.n_default
try:
self.selected_branches.append(self.repo.active_branch.name)
except TypeError:
pass
if hard:
self.mode = ResetMode.HARD
if mixed:
self.mode = ResetMode.MIXED
if soft:
self.mode = ResetMode.SOFT
self.cmd += f"{type(self).__name__.lower()}{' --' + self.mode.value if self.mode != ResetMode.DEFAULT else ''} {self.commit}"
def construct(self):
if not settings.stdout and not settings.output_only_path and not settings.quiet:
print(f"{settings.INFO_STRING} {self.cmd}")
self.show_intro()
self.parse_commits()
self.recenter_frame()
self.scale_frame()
self.reset_head_branch(self.resetTo.hexsha)
self.vsplit_frame()
self.setup_and_draw_zones(first_column_name="Changes deleted from")
self.show_command_as_title()
self.fadeout()
self.show_outro()
def build_commit_id_and_message(self, commit, i):
hide_refs = False
if commit == "dark":
commitId = m.Text("", font=self.font, font_size=20, color=self.fontColor)
commitMessage = ""
elif i == 3 and self.resetTo.hexsha not in [
c.hexsha for c in self.get_default_commits()
]:
commitId = m.Text("...", font=self.font, font_size=20, color=self.fontColor)
commitMessage = "..."
hide_refs = True
elif i == 4 and self.resetTo.hexsha not in [
c.hexsha for c in self.get_default_commits()
]:
commitId = m.Text(
self.resetTo.hexsha[:6],
font=self.font,
font_size=20,
color=self.fontColor,
)
commitMessage = self.resetTo.message.split("\n")[0][:40].replace("\n", " ")
commit = self.resetTo
hide_refs = True
else:
commitId = m.Text(
commit.hexsha[:6],
font=self.font,
font_size=20,
color=self.fontColor,
)
commitMessage = commit.message.split("\n")[0][:40].replace("\n", " ")
if (
commit != "dark"
and commit.hexsha == self.resetTo.hexsha
and commit.hexsha != self.repo.head.commit.hexsha
):
hide_refs = True
return commitId, commitMessage, commit, hide_refs
def populate_zones(
self,
firstColumnFileNames,
secondColumnFileNames,
thirdColumnFileNames,
firstColumnArrowMap={},
secondColumnArrowMap={},
thirdColumnArrowMap={},
):
for commit in self.commitsSinceResetTo:
if commit.hexsha == self.resetTo.hexsha:
break
for filename in commit.stats.files:
if self.mode == ResetMode.SOFT:
thirdColumnFileNames.add(filename)
elif self.mode in (ResetMode.MIXED, ResetMode.DEFAULT):
secondColumnFileNames.add(filename)
elif self.mode == ResetMode.HARD:
firstColumnFileNames.add(filename)
for x in self.repo.index.diff(None):
if "git-sim_media" not in x.a_path:
if self.mode == ResetMode.SOFT:
secondColumnFileNames.add(x.a_path)
elif self.mode in (ResetMode.MIXED, ResetMode.DEFAULT):
secondColumnFileNames.add(x.a_path)
elif self.mode == ResetMode.HARD:
firstColumnFileNames.add(x.a_path)
for y in self.repo.index.diff("HEAD"):
if "git-sim_media" not in y.a_path:
if self.mode == ResetMode.SOFT:
thirdColumnFileNames.add(y.a_path)
elif self.mode in (ResetMode.MIXED, ResetMode.DEFAULT):
secondColumnFileNames.add(y.a_path)
elif self.mode == ResetMode.HARD:
firstColumnFileNames.add(y.a_path)