Rugged::Repository提供的方法有哪些

56次阅读
没有评论

共计 8794 个字符,预计需要花费 22 分钟才能阅读完成。

这篇“Rugged::Repository 提供的方法有哪些”文章的知识点大部分人都不太理解,所以丸趣 TV 小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“Rugged::Repository 提供的方法有哪些”文章吧。

1. 概念

 a.rugged 是基于 ruby 语言开发的 libgit2 访问库, 提供了很好的速度和可移植性并且拥有 ruby 语言优美的特性.

 b.libgit2 是一套 git 核心方法的纯 c 实现, 被设计兼具速度和可移植性.

2. 安装

  前提: 首先当然按照 ruby 环境以及 gem ruby 包管理器(此处不介绍, 可 google), 环境 ubuntu

  安装命令如下:

 $ gem install rugged

  可能问题: 包的依赖问题, 亦可据错误 google 之

3. 文档

 $ gem server

  可打开 gem 内置的 web 服务器, 可以浏览所有安装的软件和它们的说明文档, 打开 http://localhost:8808

4.rugged 库内部模块和常用类

  主要有一个 module Rugged, 其中拥有 Rugged::Repository,Rugged::Commit,Rugged::Branch,

Rugged::BranchCollections,Rugged::Object,Rugged:Tag,Rugged::Config,Rugged::Remote,

Rugged::Index,Rugged::SettingsRugged::Reference,Rugged::Patch,Rugged::Tree,Rugged::Diff,

Rugged::Error,Rugged::Credentials 等相关模块和类, 后面会一一介绍.

5. 简单上手一试

require  rugged 
#导入 rugged 模块
 
repo = Rugged::Repository.new(/home/lry/workspace/ruby_test/git_test/.git)
#从参数给定的 git 路径打开 git repository, 返回 Rugged::Repository 对象
 
puts Rugged::Repository.discover(/home/lry/workspace/ruby_test/git_test/.git)
# =   /home/lry/workspace/ruby_test/git_test/.git 
 
puts repo.exists?(e6a40e934434e11d1e8dad08cb95a49e9d5b2aec)
#判断给定的 sha1 oid 表示的对象是否存在于 repository 中,sha1 可通过在 git status 查看
# =  true
 
puts repo.bare?
# =  false
puts repo.empty?
#由于我的工作目录中存在已提交文件, 所以一下为空
# =  false
puts repo.head_detached?
# =  false
 
puts repo.path
# =   /home/lry/workspace/ruby_test/git_test/.git/ 
puts repo.workdir
# =   /home/lry/workspace/ruby_test/git_test/ 
 
# The HEAD of the repository.
puts ref = repo.head
# =  # Rugged::Reference:0x00000002520218 
puts ref.name
# =   refs/heads/master 
ref.target
# =  # Rugged::Commit:0x00000002520150 
object = repo.read(e6a40e934434e11d1e8dad08cb95a49e9d5b2aec)
# =  # Rugged::Commit:0x00000002520150 
puts object.len
# =  171
puts object.data
# =   tree 203809dc435dd4e78d203cbf766a17581d77b2fa
 author ###
 committer ###
 
 add readme 
puts object.type
# =  :commit

6.Rugged 的类结构组织

  首先说明一下 git,git 存储每一个文件,而不是像 svn 一样纪录版本之间的差别。

 Repository 包含很多 Reference,Reference 可以是是 Reference 或者 Branch 或者 Tag 或者 AnnotationTag,Reference 指向一个 target,Reference 类型 type 为:symbolic 或者:direct, 如果 type 为:direct 类型,则

target 为 Commit,否则为其他 Reference。

 Reference 如 refs/heads/master, refs/remotes/origin/master, refs/heads/zql/master, refs/tags/v1.0

 Branch 如 refs/heads/master, refs/heads/zql/master,refs/remotes/origin/master

 Tag 如 refs/tags/v1.0

 AnnotationTag 指加了 message 的 tag

 Commit 即提交,一个提交即代表一个版本,每个 Commit 包含了该版本的所有文件,如下图所示

a) Version 1 包含文件 A,B,C

b) 修改了文件 A,C;进化为 Version 2, 包含文件 A1,B,C1

c) 修改了文件 C1;进化为 Version 3,包含文件 A1,B,C2

d) 修改了文件 A1,B;进化为 Version 4,包含文件 A2,B1,C2

e) 修改了文件 B1,C2;进化为 Version 5, 包含文件 A2,B2,C3

上述代表了 git 的文件变迁历史。进一步说明,Commit 指向一个 Tree,Tree 项目有 Trees 和 Blobs 以及 Commits,Commit 指向的 Tree 为根树,Tree 代表一个目录,当然根树代表顶级目录,路径为;Blob 代表文件;Commit 代表子模块。每个目录下也可以拥有子目录和文件以及子模块;如下图所示:

每个 Commit 的 parents 指向其父提交,也即是其上一个提交(当然也可以有多个,如 merge),如上述 Version 5 的父提交时 Version 4。下图是 Commit 的组织方式:

值得注意的是,文件 (目录) 信息和内容是分开存储的,文件信息包含文件的 oid,通过 oid 再去仓库通过 idx 去 pack 查找文件内容。

7. 相关模块和类详解

 1). 当然, 最重要的类就是 Rugged::Repository 了, 使用它我们可以操作磁盘上的 git repository.

 a. 初始化方法    

 bare(path[, alternates]) → repository

  打开一个 bare git repository, 参数 path 为的.git 的路径(包括.git). 返回 Rugged::Repository 表示该 repository 的对象

 init_at(path, is_bare = false) → repository
  示例:Rugged::Repository.init_at(~/repository , :bare) 
 #=  # Rugged::Repository:0x108849488

  若不存在则初始化 repository, 否则重新初始化, 参数 path 为.git 的上一级路径(不包括.git), 参数 is_bare 表示是否以 bare 初始化 repository, 默认为 false. 返回 Rugged::Repository 表示该 repository 的对象

 new(path, options = {}) → repository
  示例:Rugged::Repository.new(~/test/.git) 
 #=  # Rugged::Repository:0x108849488 
 Rugged::Repository.new(path, :alternates =  [ ./other/repo/.git/objects])

  打开一个 repository, 参数 path 为.git 的路径或者其上一级路径,options 表示可选参数, 接.git 中子目录的路径. 返回 Rugged::Repository 表示该 repository 的对象. 如果需要创建一个 repository, 请使用 Rugged::init_at 代替.

 clone_at(url, local_path[, options]) → repository
  示例:Repository.clone_at( https://github.com/libgit2/rugged.git ,  ./some/dir , 
 { transfer_progress: lambda { |total_objects, indexed_objects, received_objects, 
 local_objects, total_deltas, indexed_deltas, received_bytes|
 # ...}
 })

    从远程地址得到一个 git 的拷贝.url 为项目仓库的地址,local_path 为本地拷贝的目录. 返回 Rugged::Repository 表示该 repository 的对象.

 options 有如下 Hash 选项:

:bare

Iftrue, the clone will be created as a bare repository. Defaults tofalse.

:checkout_branch

The name of a branch to checkout. Defaults to the remote’sHEAD.

:remote

The name to give to the“origin”remote. Defaults to origin .

:ignore_cert_errors

If set totrue, errors while validating the remote’s host certificate will be ignored.

:credentials

The credentials to use for the clone operation. Can be either an instance of one of the Rugged::Credentials types, or a proc returning one of the former. The proc will be called with theurl, theusernamefrom the url (if applicable) and a list of applicable credential types.

:progress

A callback that will be executed with the textual progress received from the remote. This is the text send over the progress side-band (ie. the“counting objects”output).

:transfer_progress

A callback that will be executed to report clone progress information. It will be passed the amount oftotal_objects,indexed_objects,received_objects,local_objects,total_deltas,indexed_deltas, andreceived_bytes.

:update_tips

A callback that will be executed each time a reference was updated locally. It will be passed therefname,old_oidandnew_oid.

  b. 基本方法

 discover(path = nil, across_fs = true) → repository

  由 path 传递的路径向上搜索.git 文件夹, 然后打开并生成一个 Rugged::Repository 对象, 如果 path 为 nil, 则以当前路径为起始点

 hash_data(str, type) → oid
 Repository.hash_data(hello world , :commit) #=   de5ba987198bcf2518885f0fc1350e5172cded78 
 Repository.hash_data(hello_world , :tag) #=   9d09060c850defbc7711d08b57def0d14e742f4e

  返回 str 的 hash 值. 将 str 作为原始数据加上 type 相应的头部进行散列. 返回该结果的 hash 值字符串

 hash_file(path, type) → oid

  返回 path 指向的文件的 hash 值. 返回 sha1 值的字符串

 create_branch(name, sha_or_ref =  HEAD)

    在仓库中创建分支,name 指定分支名,sha_ora_ref 目标分支, 可以为 oid,reference name 或者 Rugged::Object 实例. 返回 Rugged::Branch 对象

 branches()

  返回当前 repository 中的分支, 返回 Rugged::Branch 的 BranchCollection 对象集合

 checkout(target, options = {})

  切换到由 target 指定的 branch, reference or commit.

 checkout_head([options]) → nil

  切换到 HEAD

 ref(ref_name) 
  示例:repo.ref  refs/heads/master 
 # =  # Rugged::Reference:2199125780 {name:  refs/heads/master ,
 target:  25b5d3b40c4eadda8098172b26c68cf151109799 }

  查找 ref_name 制定的 Rugged::Reference 对象

 ref_names(glob = nil) 
 references()# 得到仓库中的所有 Referece,ReferenceCollection 表示
 refs(glob = nil)
 head → ref

  获取指向 repository Head 的 Rugged::Reference 对象

 head = str

  设置 repository 的 Head

 index → idx
 index = idx

  获取或设置 repository 的默认的 index,idx 为 Rugged::Index 对象

 namespace → str
 namespace = new_namespace

  设置或设置 repository 的活动命名空间

 path → path

  获取完整的, 标准的.git 的路径

 workdir → path or nil
 workdir = path

  获取或设置 repository 的工作目录

 config → cfg
 config = cfg

  获取和设置配置.cfg 为 Rugged::Config 对象

 lookup(oid)

  查找一个 SHA1, 返回继承 Rugged::Object 四个类中的某一个类的对象

 exists?(oid) → true or false

  是否给定的 SHA1 OID (represented as a 40-character string)存在于 repository

 include?(oid) → true or false
 repo.include?(d8786bfc97485e8d7b19b21fb88c8ef1f199fc3f) #=  true

  是否给定的 SHA1 OID (represented as a 40-character string)存在于 repository

 tags()

  获取 repository 的所有 tag. 返回 Rugged::Tag 的集合 Rugged::TagCollection 对象

 remotes()

  获取 repository 的所有 remotes. 返回 Rugged::Remote 的集合 Rugged::RemoteCollection 对象

 push(remote_or_url, *args)

  推送 refspecs 到 remote_or_url. 返回 refspecs 为键值, 如果成功值为 nil, 失败为错误消息

 last_commit()

  获取最后一次 commit, 返回 Rugged::Commit 对象

 read(oid) → str

  读取 oid 对象标识的对象原始数据

 read_header(oid) → hash

  读取 repository 的 Head 信息. 返回一个 Hash 对象, 可能的键值对如下

 :type = A Symbol denoting the object’s type. 可能的值:tree,:blob,:commit 或:tag.

 :len = 上述对象的长度

 rev_parse(spec)

  通过 revision 字符串查找对象. 返回继承 Rugged::Object 四个类中的某一个类的对象

 rev_parse_oid(spec)

  通过 revision 字符串查找 oid. 返回匹配 revision 的 oid

 write(buffer, type) → oid

  将 buffer 中的数据作为被给定类型 type 写入 repository’s object database.

 type 可取值为:tag,:commit,:treeor:blob.

  返回新建对象的 oid

 blob_at(revision, path)

  获取指定路径 path 的 revision 的 blob.

 revision – The String SHA1.

 path     – The String file path.

  返回字符串

 diff(left, right, opts = {}) 
 diff_workdir(left, opts = {})

  指定两个版本的 diff

 merge_base(oid1, oid2, ...) 
 merge_base(ref1, ref2, ...) 
 merge_base(commit1, commit2, ...)

  找到合并的 base

 merge_commits(our_commit, their_commit, options = {}) → index

  合并操作.our_commitandtheir_commitcan either be Rugged::Commit objects, or OIDs resolving to the former. 返回合并后的 Rugged::Index 对象

 each_id { |id| block } 
 each_id → Iterator

  对在 repository 中发现的每一个 object ID 执行块迭代,id 为 40 个字符的字符串

 status { |file, status_data| block } 
 status(path) → status_data
  示例:repo.status { |file, status_data| puts  #{file} has status: #{status_data.inspect}  }
 repo.status(src/diff.c) #=  [:index_new, :worktree_new]

  获取工作目录中文件的状态.

  c. 操作对象数据库

 read(oid)
  示例:object = repo.read(a0ae5566e3c8a3bddffab21022056f0b5e03ef07)
 # =  # Rugged::OdbObject:0x109a64780

  通过 oid 读取 Rugged::OdbObject 对象

 Rugged::OdbObject.data()
 Rugged::OdbObject.len()
 Rugged::OdbObject.type()
 Rugged::OdbObject.oid()
  示例:object.len# =  237
 object.data
 # =   tree 76f23f186076fc291742816721ea8c3e95567241\nparent 8e3c5c52b8f29da0adc7e8be8a037cbeaea6de6b\nauthor Vicent Mart\303\255  tanoku@gmail.com  1333859005 +0200\ncommitter Vicent Mart\303\255  tanoku@gmail.com  1333859005 +0200\n\nAdd `Repository#blob_at`\n 
 object.type# =  :commit
 object.oid#=   d8786bfc97485e8d7b19b21fb88c8ef1f199fc3f

以上就是关于“Rugged::Repository 提供的方法有哪些”这篇文章的内容,相信大家都有了一定的了解,希望丸趣 TV 小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注丸趣 TV 行业资讯频道。

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-08-04发表,共计8794字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)