Question: My code: class Git: _ _ slots _ _ = [ current _ branch, start, selected _ commit, visited _ branches, branches ] def

My code:
class Git:
__slots__=["current_branch", "start", "selected_commit", "visited_branches", "branches"]
def __init__(self):
# Reference to the original/main branch
self.start = GitBranch()
self.current_branch = self.start
self.selected_commit: Node = self.start.get_last_commit()
self.visited_branches = set()
# Maintain a dictionary of all branches
self.branches ={"main": self.start}
def get_current_commit(self)-> Optional[str]:
"""
Return the value stored in the currently selected commit.
:return: Value of the current working commit.
"""
if self.selected_commit:
return self.selected_commit.value
return None
def get_current_branch_name(self)-> Optional[str]:
"""
Return the name of the current working/active branch.
:return: Name of the current working branch.
"""
return self.current_branch.name
def commit(self, message: str)-> None:
"""
Commit to the timeline if it is the last element in the commit.
If the current working commit is not the last commit, raise an exception.
:param message: Message to be added to the commit.
"""
if self.selected_commit != self.current_branch.get_last_commit():
raise Exception("Cannot commit: not at the last commit in the branch.")
self.selected_commit = self.current_branch.push_commit(message)
def backwards(self)-> None:
"""
Moves the reference of the current working commit back one commit.
If already at the first commit of the branch, switch back to the parent branch if available.
"""
if self.selected_commit:
print(
f"Before moving backwards, current commit: {self.get_current_commit()} on branch {self.get_current_branch_name()}")
if self.selected_commit and self.selected_commit.prev:
# Move to the previous commit in the current branch
self.selected_commit = self.selected_commit.prev
print(f"Moved Back to Commit: {self.selected_commit.value}")
# If we are at the branching point (the parent node), switch to the parent branch
elif self.selected_commit == self.current_branch.parent_node:
# Switch back to the main branch
self.current_branch = self.start # Main branch starts here
self.selected_commit = self.current_branch.get_last_commit() # Set the commit to the last commit on the main branch
print(f"Switched back to 'main' branch, current commit: {self.get_current_commit()}")
else:
print(f"Already at the first commit of the branch: {self.get_current_commit()}")
def forward(self)-> None:
"""
Move the reference of the current working commit forward one commit within the current branch.
If already in the last commit of the branch, do not jump to a child branch automatically.
"""
if self.selected_commit and self.selected_commit.next:
# Move to the next commit in the current branch
self.selected_commit = self.selected_commit.next
print(f"Moved Forward to Commit: {self.selected_commit.value}")
else:
print(f"Already at the last commit of the branch: {self.get_current_commit()}")
# DO NOT MODIFY BELOW #
# The following methods are already implemented for you to (1) better understand how Git works,
# (2) better understand tests.py as they are called in tests.py.
def checkout_commit(self, message)-> None:
"""
Check out any commit in the tree, moving the current selected branch to that commit's branch.
If the commit is found, change the current selected branch to be the parent branch of the commit.
If no such commit exists, raise an exception.
:param message: Commit message to look for.
"""
existing_commit = self.find_commit(self.start, message)
if existing_commit is not None:
self.current_branch = existing_commit[0]
self.selected_commit = existing_commit[1]
return
raise Exception("Commit is not existent")
def checkout_branch(self, name)-> None:
"""
Check out a tree branch, and move the working commit to the last commit on the branch.
If the branch with the given name already exist, change the current branch to be that one, and change the current
commit to be the last commit on the branch. If branch does not exist and current working commit does not have a
branch, then create a branch from that commit.
:param name: The branch name to look for.
:return: None.
"""
existing_branch = self.find_branch(self.start, name)
My code: class Git: _ _ slots _ _ = [ " current _

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!