基于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第一次提交' }
]
相关推荐
拉里呱唧19 小时前
一个像在使用PPT的在线 HTML 编辑器:HeyHTML
javascript·交互·html5
changshuaihua00120 小时前
扣子开发指南
javascript·人工智能
光影少年20 小时前
对typescript开发框架的理解?
前端·javascript·typescript
a11177620 小时前
“像风之翼“无人机巡检平台仪表盘
前端·javascript·开源·html·无人机
We་ct1 天前
LeetCode 5. 最长回文子串:DP + 中心扩展
前端·javascript·算法·leetcode·typescript
cn_mengbei1 天前
用React Native开发OpenHarmony应用:Reanimated共享元素过渡
javascript·react native·react.js
kyriewen1 天前
前端测试:别为了100%覆盖率而写测试,那是自欺欺人
前端·javascript·单元测试
Data_Journal1 天前
如何使用cURL更改User Agent
大数据·服务器·前端·javascript·数据库
掌心向暖RPA自动化1 天前
如何获取网页某个元素在屏幕可见部分的中心坐标影刀RPA懒加载坐标定位技巧
java·javascript·自动化·rpa·影刀rpa
竹林8181 天前
wagmi v2 多链钱包切换:一个 Uniswap 仿盘项目让我踩了三天坑
前端·javascript