Question: My code: class Git: _ _ slots _ _ = [ current _ branch, start, selected _ commit, visited _ branches, branches ] def
My code:
class Git:
slotscurrentbranch", "start", "selectedcommit", "visitedbranches", "branches"
def initself:
# Reference to the originalmain branch
self.start GitBranch
self.currentbranch self.start
self.selectedcommit: Node self.start.getlastcommit
self.visitedbranches set
# Maintain a dictionary of all branches
self.branches main: self.start
def getcurrentcommitself Optionalstr:
Return the value stored in the currently selected commit.
:return: Value of the current working commit.
if self.selectedcommit:
return self.selectedcommit.value
return None
def getcurrentbranchnameself Optionalstr:
Return the name of the current workingactive branch.
:return: Name of the current working branch.
return self.currentbranch.name
def commitself 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.selectedcommit self.currentbranch.getlastcommit:
raise ExceptionCannot commit: not at the last commit in the branch."
self.selectedcommit self.currentbranch.pushcommitmessage
def backwardsself 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.selectedcommit:
print
fBefore moving backwards, current commit: selfgetcurrentcommit on branch selfgetcurrentbranchname
if self.selectedcommit and self.selectedcommit.prev:
# Move to the previous commit in the current branch
self.selectedcommit self.selectedcommit.prev
printfMoved Back to Commit: selfselectedcommit.value
# If we are at the branching point the parent node switch to the parent branch
elif self.selectedcommit self.currentbranch.parentnode:
# Switch back to the main branch
self.currentbranch self.start # Main branch starts here
self.selectedcommit self.currentbranch.getlastcommit # Set the commit to the last commit on the main branch
printfSwitched back to 'main' branch, current commit: selfgetcurrentcommit
else:
printfAlready at the first commit of the branch: selfgetcurrentcommit
def forwardself 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.selectedcommit and self.selectedcommit.next:
# Move to the next commit in the current branch
self.selectedcommit self.selectedcommit.next
printfMoved Forward to Commit: selfselectedcommit.value
else:
printfAlready at the last commit of the branch: selfgetcurrentcommit
# DO NOT MODIFY BELOW #
# The following methods are already implemented for you to better understand how Git works,
# better understand tests.py as they are called in tests.py
def checkoutcommitself 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.
existingcommit self.findcommitselfstart, message
if existingcommit is not None:
self.currentbranch existingcommit
self.selectedcommit existingcommit
return
raise ExceptionCommit is not existent"
def checkoutbranchself 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.
existingbranch self.findbranchselfstart, name
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
