基于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第一次提交' }
]
相关推荐
ByteCraze2 分钟前
系统性整理组件传参14种方式
前端·javascript·vue.js
大杯咖啡3 分钟前
基于 Vue3 (tsx语法)的动态表单深度实践-只看这一篇就够了
前端·javascript·vue.js
izx8886 分钟前
JavaScript 中 `this` 的真相:由调用方式决定的动态指针
javascript
前端缘梦8 分钟前
JavaScript核心机制:执行栈、作用域与this指向完全解析
前端·javascript·面试
春卷同学11 分钟前
拼图游戏 - Electron for 鸿蒙PC项目实战案例
javascript·electron·harmonyos
皮蛋瘦肉粥_12112 分钟前
pink老师-jsAPIS-day1
javascript·apis
Youyzq12 分钟前
react 元素触底hooks封装
前端·javascript·react.js
灵犀坠28 分钟前
前端面试&项目实战核心知识点总结(Vue3+Pinia+UniApp+Axios)
前端·javascript·css·面试·职场和发展·uni-app·html
fruge32 分钟前
Vue3 与 Vue2 核心差异:响应式原理、生命周期及迁移方案
前端·javascript·vue.js
zlpzlpzyd37 分钟前
vue.js 3项目整合vue-router 4的问题
前端·javascript·vue.js