基于JavaScript的简易Git

基于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第一次提交' }
]
相关推荐
前端历劫之路2 小时前
🔥 1.30 分!我的 JS 库 Mettle.js 杀入全球性能榜,紧追 Vue
前端·javascript·vue.js
阿彬爱学习4 小时前
大模型在垂直场景的创新应用:搜索、推荐、营销与客服新玩法
前端·javascript·easyui
橙序员小站4 小时前
通过trae开发你的第一个Chrome扩展插件
前端·javascript·后端
Lazy_zheng4 小时前
一文掌握:JavaScript 数组常用方法的手写实现
前端·javascript·面试
MrSkye5 小时前
🔥从菜鸟到高手:彻底搞懂 JavaScript 事件循环只需这一篇(下)
前端·javascript·面试
泯泷5 小时前
Tiptap 深度教程(三):核心扩展全面指南
前端·javascript·全栈
每天开心6 小时前
🐞一次由事件冒泡引发的 React 弹窗关闭 Bug 排查与解决
前端·javascript·debug
kartjim6 小时前
2025 年现代 Node.js 模式
前端·javascript·node.js
白头吟9 小时前
js函数中的this
javascript
ZzMemory9 小时前
深入理解JS(九):IIFE,即执函数的锁域魔法
前端·javascript·面试