如何检测在LibGit2Sharp中创建了哪个分支 [英] How to detect what commit a branch has been created from in LibGit2Sharp
问题描述
分支
的实例,你如何计算它最初创建的提交内容? 头部
引用的对象。 头
是一个文本文件,主要位于 .git / refs / heads
层次结构下。这个文本文件包含当前指向的 commit
这个头部
的散列。类似地,分支
带有 Tip
属性,它指向 Commit $ c $> c>。
使用git存储库并执行诸如提交,重置,重新绑定等操作时,头
文件用不同的散列更新,指向不同的提交。
头
提交。 分支
。 使用git创建新分支时,会创建一个新的reflog。
给定一个现有的分支 backup $> c $ c
$ cat .git / refs / heads / backup
7dec18258252769d99a4ec9c82225e47d58f958c
创建一个新分支将创建并提供它的reflog
$ git分支new_branch_from_branch备份
$ git reflog new_branch_from_branch
7dec182 new_branch_from_branch @ {0}:分支:从备份创建
当然,直接从提交创建分支时也可行
$ git分支new_branch_from_sha 191adce
$ git reflog new_branch_from_sha
191adce new_branch_from_sha @ {0}:分支:创建自191adce
LibGit2Sharp也公开reflog。例如,以下代码将枚举特定分支
的日志条目。
var branch = repository.Head; //或repository.Branches [my_branch] ...
foreach(ReflogEntry e in repository.Refs.Log(branch.CanonicalName))
{
Console.WriteLine ({0} - {1}:{2},
e.From.ToString(7),e.To.ToString(7),e.Message);
$ b因此好消息 ,reflog可能包含你所追求的; - )
但是...
- 您必须通过在每条消息中搜索分支:从模式
创建正确的条目
- 如果您的分支太旧了,reflog中较旧的条目可能已被内置 git gc 管家流程(默认情况下,reflog条目保留90天),并且现在可能会丢失最初的创建自条目
注意:截至今天,LibGit2Sharp不会在创建或移除分支时创建条目。但是,目前正在通过 @dahlbyk 作为 拉取请求#499
So given an instance of LibGit2Sharp Branch
how do you work out what commit that it was initially created from?
解决方案 A Branch
is merely an object depicting a git head
reference. A head
is a text file, mostly living under the .git/refs/heads
hierarchy. This text file contains the hash of the commit
this head
currently points to. Similarly, a Branch
bears a Tip
property which points to a Commit
.
When working with git repositories and performing actions such as committing, resetting, rebasing... the head
file is updated with different hashes, pointing to different commits.
A head
keeps no track of previous pointed at commits. Neither does the Branch
.
With git, when creating a new branch, a new reflog is created. Git takes care of adding a first entry with a message identifying the object the branch has been created from.
Given an existing branch backup
$ cat .git/refs/heads/backup
7dec18258252769d99a4ec9c82225e47d58f958c
Creating a new branch will create and feed its reflog
$ git branch new_branch_from_branch backup
$ git reflog new_branch_from_branch
7dec182 new_branch_from_branch@{0}: branch: Created from backup
Of course, that also works when directly creating a branch from a commit
$ git branch new_branch_from_sha 191adce
$ git reflog new_branch_from_sha
191adce new_branch_from_sha@{0}: branch: Created from 191adce
LibGit2Sharp exposes the reflog as well. For instance, the following code will enumerate the log entries for a specific Branch
.
var branch = repository.Head; // or repository.Branches["my_branch"]...
foreach (ReflogEntry e in repository.Refs.Log(branch.CanonicalName))
{
Console.WriteLine("{0} - {1} : {2}",
e.From.ToString(7), e.To.ToString(7), e.Message);
}
So "good news", the reflog may contain what you're after ;-)
but...
- you'll have to find out the correct entry by yourself by searching within each message the "branch: Created from" pattern
- If your branch is too old, older entries in the reflog may have been removed by the built-in git gc housekeeping process (by default reflog entries are kept for 90 days) and the initial "Created from" entry may now be lost
Note: As of today, LibGit2Sharp doesn't create an entry when creating or removing a branch. However, this is currently tackled by the amazing @dahlbyk as part of Pull Request #499
这篇关于如何检测在LibGit2Sharp中创建了哪个分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!