百科.dev
全部条目AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
G

git_store

> 编程语言
开源

Ruby 中的 Git 作为版本化数据存储

170 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

Ruby 中的 Git 作为版本化数据存储

Git Store - using Git as versioned data store in Ruby

GitStore implements a versioned data store based on the revision management system Git. You can store object hierarchies as nested hashes, which will be mapped on the directory structure of a git repository. Basically GitStore checks out the repository into a in-memory representation, which can be modified and finally committed.

GitStore supports transactions, so that updates to the store either fail or succeed completely.

Installation

GitStore can be installed as gem easily:

$ gem sources -a http://gems.github.com
$ sudo gem install georgi-git_store

Usage Example

First thing you should do, is to initialize a new git repository.

$ mkdir test
$ cd test
$ git init

Now you can instantiate a GitStore instance and store some data. The data will be serialized depending on the file extension. So for YAML storage you can use the 'yml' extension:

store = GitStore.new('/path/to/repo')

store['users/matthias.yml'] = User.new('Matthias')
store['pages/home.yml'] = Page.new('matthias', 'Home')

store.commit 'Added user and page'

Transactions

GitStore manages concurrent access by a file locking scheme. So only one process can start a transaction at one time. This is implemented by locking the refs/head/.lock file, which is also respected by the git binary.

If you access the repository from different processes or threads, you should write to the store using transactions. If something goes wrong inside a transaction, all changes will be rolled back to the original state.

store = GitStore.new('/path/to/repo')

store.transaction do
  # If an exception happens here, the transaction will be aborted.
  store['pages/home.yml'] = Page.new('matthias', 'Home')
end

A transaction without a block looks like this:

store.start_transaction

store['pages/home.yml'] = Page.new('matthias', 'Home')

store.rollback # This will restore the original state

Data Storage

When you call the commit method, your data is written back straight into the git repository. No intermediate file representation. So if you want to have a look at your data, you can use a git browser like git-gui or checkout the files:

$ git checkout

Iteration

Iterating over the data objects is quite easy. Furthermore you can iterate over trees and subtrees, so you can partition your data in a meaningful way. For example you may separate the config files and the pages of a wiki:

store['pages/home.yml'] = Page.new('matthias', 'Home')
store['pages/about.yml'] = Page.new('matthias', 'About')
store['config/wiki.yml'] = { 'name' => 'My Personal Wiki' }

# Enumerate all objects
store.each { |obj| ... } 

# Enumerate only pages
store['pages'].each { |page| ... }

Serialization

Serialization is dependent on the filename extension. You can add more handlers if you like, the interface is like this:

class YAMLHandler
  def read(data)
    YAML.load(data)
  end

  def write(data)
    data.to_yaml
  end    
end

Shinmun uses its own handler for files with md extension:

class PostHandler
  def read(data)
    Post.new(:src => data)
  end

  def write(post)
    post.dump
  end    
end

store = GitStore.new('.')
store.handler['md'] = PostHandler.new

GitStore on GitHub

Download or fork the project on its Github page

Issues· 1 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言