Skip to content

Instantly share code, notes, and snippets.

@Ssenseii
Created December 20, 2023 15:55
Show Gist options
  • Select an option

  • Save Ssenseii/eaeef44bf7ad93b07ed1f8469204c9c1 to your computer and use it in GitHub Desktop.

Select an option

Save Ssenseii/eaeef44bf7ad93b07ed1f8469204c9c1 to your computer and use it in GitHub Desktop.
GitHub Explained in OOP
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