Created
December 20, 2023 15:55
-
-
Save Ssenseii/eaeef44bf7ad93b07ed1f8469204c9c1 to your computer and use it in GitHub Desktop.
GitHub Explained in OOP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Git { | |
| constructor(name) { | |
| this.name = name; | |
| this.lastCommitId = -1; | |
| this.branches = []; | |
| var master = new Branch("master, null"); | |
| this.branches.push(master); | |
| this.Head = master; | |
| } | |
| commit(message) { | |
| var commit = new Commit(++this.lastCommitId, this.Head.commit, message); | |
| this.Head.commit = commit | |
| return commit; | |
| } | |
| log() { | |
| var commit = this.Head.commit, history = []; | |
| while (commit) { | |
| history.push(commit); | |
| // Keep following the parent | |
| commit = commit.parent; | |
| } | |
| return history; | |
| } | |
| checkout(branch_name){ | |
| for(var i = this.branches.length; i--;){ | |
| if(this.branches[i].name === branch_name){ | |
| console.log("Switching to existing branch: " + branch_name); | |
| this.Head = this.branches[i]; | |
| return this; | |
| } | |
| } | |
| var new_branch = new Branch(branch_name, this.Head.commit); | |
| this.branches.push(new_branch); | |
| this.Head = new_branch; | |
| console.log("switched to new branch: " + branch_name); | |
| return this; | |
| } | |
| } | |
| class Commit { | |
| constructor(id, parent, message) { | |
| this.id = id; | |
| this.parent = parent; | |
| this.message = message; | |
| } | |
| } | |
| class Branch { | |
| constructor(name, commit) { | |
| this.name = name; | |
| this.commit = commit; | |
| } | |
| } | |
| // git init | |
| var repo = new Git("my-repo"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment