基于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第一次提交' }
]
相关推荐
遇见~未来25 分钟前
JavaScript数组全解析:从本质到高级技巧
开发语言·前端·javascript
哈__26 分钟前
基础入门 React Native 鸿蒙跨平台开发:TabBar 底部导航栏
javascript·react native·react.js
lili-felicity28 分钟前
React Native 鸿蒙跨平台开发:Animated 实现鸿蒙端组件的左右滑动动画
javascript·react native·react.js
哈__37 分钟前
React Native 鸿蒙跨平台开发:StatusBar 状态栏组件
javascript·react native·react.js
丢,捞仔1 小时前
uni-app上架应用添加权限提示框
前端·javascript·uni-app
Hilaku1 小时前
我是如何用一行 JS 代码,让你的浏览器内存瞬间崩溃的?
前端·javascript·node.js
哈__1 小时前
React Native 鸿蒙跨平台开发:简易记事本 APP
javascript·react native·react.js
贺今宵1 小时前
electron-vue无网络环境,读取本地图片/文件展示在页面vue中protocol
前端·javascript·electron
老前端的功夫1 小时前
TypeScript索引访问类型深度解析:类型系统的动态访问与模式匹配
前端·javascript·ubuntu·架构·typescript·前端框架
张元清1 小时前
大白话讲 React2Shell 漏洞:智能家居的语音助手危机
前端·javascript·react.js