基于JavaScript的简易Git
项目代码结构
本项目实现了一个简单的Git版本控制系统模拟,包含以下文件:
main.js
: 测试脚本git.js
: Git仓库类实现branch.js
: 分支类实现commit.js
: 提交类实现
代码实现
commit.js
javascript
/**
* 创建Commit类
* @param {number} id 提交id
* @param {Commit} parent 上一次提交 或 本身
* @param {string} message 提交信息
*/
function Commit(id,parent,message){
this.id=id;
this.parent=parent;
this.message=message;
}
export default Commit;
branch.js
javascript
import Commit from "./commit.js";
/**
* 创建Branch类
* @param {string} name 分支名称
* @param {Commit} commit 提交
*/
function Branch(name,commit){
this.name=name;
this.commit=commit;
}
export default Branch;
git.js
javascript
import Commit from "./commit.js";
import Branch from './branch.js'
/**
* 创建Git类
* @param {string} name 仓库名称
*/
function Git(name){
this.name=name;
//记录上一次commit id
this.lastCommitId=-1;
//分支数组
this.branches=[];
//创建默认master分支
var master=new Branch("master",null);
//将master分支添加到branches中
this.branches.push(master);
//指向当前分支
this.HEAD=master;
}
/**
* 为git创建commit命令
* @param {string} message 提交信息
* @returns 提交的id和信息
*/
Git.prototype.commit=function(message){
//更新 提交id、当前分支上一次提交、提交信息
var commit=new Commit(++this.lastCommitId,this.HEAD.commit,message);
//指向当前分支最新提交
this.HEAD.commit=commit;
return commit;
}
/**
* 为git创建log命令
* @returns 历史提交记录
*/
Git.prototype.log=function(){
//创建历史提交数组
var history=[]
//获取当前分支最新提交
var commit=this.HEAD.commit;
//循环获取全部提交
while(commit){
history.push(commit);
//获取上一次提交
commit=commit.parent;
}
return history;
}
/**
* 切换分支
* @param {string} branchName 分支名称
* @returns git实例
*/
Git.prototype.checkout=function(branchName){
//判断当前分支是否存在,找到直接返回,未找到就创建
//1.遍历分支数组,查找分支branchName
this.branches.forEach(branch=>{
if(branch.name === branchName)
this.HEAD=branch;
})
//2.未找到分支branchName
//2.1 创建新分支branchName,并复制当前分支的最新提交给新分支
var newBranch=new Branch(branchName,this.HEAD.commit);
//2.2 新增分支到branches中
this.branches.push(newBranch);
//2.3 更改当前分支
this.HEAD=newBranch;
//2.4 返回git
return this;
}
export default Git;
main.js
javascript
import Git from "./git.js";
//创建仓库my-repo
var repo=new Git("my-repo");
//-----------------------------主分支master
console.log("=================================================================");
console.log("仓库名:",repo.name,"\n当前分支:",repo.HEAD.name);
console.log("=================================================================");
//第一次提交
const firstRes=repo.commit("master第一次提交");
//第二次提交
const secondRes=repo.commit("master第二次提交");
//输出提交记录
console.log(repo.log());
console.log("=================================================================");
//----------------------------切换分支developer
repo.checkout("developer");
console.log("仓库名:",repo.name,"\n当前分支:",repo.HEAD.name);
console.log("=================================================================");
//提交
repo.commit("developer的分支第一次提交");
//输出提交记录
console.log(repo.log());
//-----------------------------切换分支master
repo.checkout("master");
console.log("=================================================================");
console.log("仓库名:",repo.name,"\n当前分支:",repo.HEAD.name);
console.log("=================================================================");
console.log(repo.log());
执行结果
yaml
=================================================================
仓库名: my-repo
当前分支: master
=================================================================
[
Commit { id: 1, parent: Commit { id: 0, parent: null, message: 'master第一次提交' }, message: 'master第二次提交' },
Commit { id: 0, parent: null, message: 'master第一次提交' }
]
=================================================================
仓库名: my-repo
当前分支: developer
=================================================================
[
Commit { id: 2, parent: Commit { id: 1, parent: [Commit], message: 'master第二次提交' }, message: 'developer的分支第一次提交' },
Commit { id: 1, parent: Commit { id: 0, parent: null, message: 'master第一次提交' }, message: 'master第二次提交' },
Commit { id: 0, parent: null, message: 'master第一次提交' }
]
=================================================================
仓库名: my-repo
当前分支: master
=================================================================
[
Commit { id: 2, parent: Commit { id: 1, parent: [Commit], message: 'master第二次提交' }, message: 'developer的分支第一次提交' },
Commit { id: 1, parent: Commit { id: 0, parent: null, message: 'master第一次提交' }, message: 'master第二次提交' },
Commit { id: 0, parent: null, message: 'master第一次提交' }
]