V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
爱意满满的作品展示区。
Geo200

[造轮子]用 node.js 从零实现了一个小型 git

  •  
  •   Geo200 · Aug 17, 2018 · 3022 views
    This topic created in 2814 days ago, the information mentioned may be changed or developed.

    一直对 git 的实现比较感兴趣,尤其是分支切换及版本回退部分,git Pro 这本书也看过对应的原理,但也是看过就忘,印象不深,git 的源码也太多无从下手,索性自己根据原理从零开始模仿 git 的命令做了个版本控制,因为自己对 js 比较熟,涉及到文件操作自然也选择了 node,目前实现了基本的功能( init,add,commit,branch ),操作实体对象也根据 git 原理实现了 blob、tree 和 commit,一套写下来对 git 的实现有更深入理解,这里也分享给有需要的朋友。


    目前已实现的功能:

    • gito init:初始化项目,与 git init 的实现一致;
    • gito status:查询 repo 的状态,与 git status 的实现一致;
    • gito add:将当前工作区文件添加到 gito 暂存区,目前不支持参数,与 git add <pathname>类似; gito commit -m "commit description":将已经加入暂存区的文件提交到版本库,与 git commit -m "description"功能一致,但目前只实现-m 参数;</pathname>
    • gito checkout -f "filename":将未提交至版本库的文件还原为当前 HEAD 指向的版本,与 git checkout "filename"实现一致;
    • gito reset --hard:还原到上次提交的状态,与 git reset --hard 实现一致,目前暂不支持还原到指定提交版本;
    • gito branch <branchname>:新建分支,与 git branch <branchname>一致;</branchname></branchname>
    • gito branch -s <branchname>:切换至指定分支,与 git checkout <branchname>一致;</branchname></branchname>
    • gito branch -l:查看当前所有分支及当前所在分支,与 git branch 一致;
    • gito branch -d <branchname>:删除指定分支,与 git branch -d <branchname>一致;</branchname></branchname>

    围观地址: https://github.com/Geocld/gito

    5 replies    2018-08-21 00:18:39 +08:00
    suley
        1
    suley  
       Aug 17, 2018
    期待 go,java,php 也来造轮子
    wanguorui123
        2
    wanguorui123  
       Aug 17, 2018
    msg7086
        3
    msg7086  
       Aug 18, 2018
    @wanguorui123 了解了,是用 C 写的。能不能找个别的语言实现的?
    DiamondYuan
        4
    DiamondYuan  
       Aug 20, 2018
    @msg7086 喵喵喵。不是用 go 写的吗。
    msg7086
        5
    msg7086  
       Aug 21, 2018
    @DiamondYuan 不是。Gogs 用的是 Linus 写的 C 版 Git。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2467 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 97ms · UTC 08:05 · PVG 16:05 · LAX 01:05 · JFK 04:05
    ♥ Do have faith in what you're doing.