11

特定のファイルを含むコミットのリストを取得するにはどうすればよいですか。つまり、git log pathforに相当しLibGit2Sharpます。

それは実装されていませんか、それとも私が見逃している方法はありますか?

4

4 に答える 4

5

私は、LibGit2Sharp を使用して、同じ機能をアプリケーションに組み込むことに取り組んでいました。

ファイルを含むすべてのコミットをリストする以下のコードを書きました。GitCommit クラスは含まれていませんが、単なるプロパティのコレクションです。

私の意図は、SVN ログと同様に、ファイルが変更されたコミットのみをコードにリストすることでしたが、その部分はまだ書いていません。

コードは最適化されていないことに注意してください。これは私の最初の試みにすぎませんが、役立つことを願っています。

/// <summary>
/// Loads the history for a file
/// </summary>
/// <param name="filePath">Path to file</param>
/// <returns>List of version history</returns>
public List<IVersionHistory> LoadHistory(string filePath)
{
    LibGit2Sharp.Repository repo = new Repository(this.pathToRepo);

    string path = filePath.Replace(this.pathToRepo.Replace(System.IO.Path.DirectorySeparatorChar + ".git", string.Empty), string.Empty).Substring(1);
    List<IVersionHistory> list = new List<IVersionHistory>();

    foreach (Commit commit in repo.Head.Commits)
    {
        if (this.TreeContainsFile(commit.Tree, path) && list.Count(x => x.Date == commit.Author.When) == 0)
        {
            list.Add(new GitCommit() { Author = commit.Author.Name, Date = commit.Author.When, Message = commit.MessageShort} as IVersionHistory);
        }
    }

    return list;
}

/// <summary>
/// Checks a GIT tree to see if a file exists
/// </summary>
/// <param name="tree">The GIT tree</param>
/// <param name="filename">The file name</param>
/// <returns>true if file exists</returns>
private bool TreeContainsFile(Tree tree, string filename)
{
    if (tree.Any(x => x.Path == filename))
    {
        return true;
    }
    else
    {
        foreach (Tree branch in tree.Where(x => x.Type == GitObjectType.Tree).Select(x => x.Target as Tree))
        {
            if (this.TreeContainsFile(branch, filename))
            {
                return true;
            }
        }
    }

    return false;
}
于 2012-10-29T15:53:58.417 に答える
4

LibGit2Sharpは C ライブラリlibgit2から来ています ... そもそも含まれていませんでしgit logた ;)

それでも、 LibGit2Sharp には独自のgit log機能があります。
そのページにgit logFiltersが含まれていますが、 Filter はパスでフィルター処理していないようです (「参照をクエリ中にスタッシュを除外する方法」で詳しく説明されているように)。
そのため、現時点では実装されていないようです。

于 2012-10-29T13:13:39.317 に答える
1

dmckの回答と非常に似ていますが、より最新です

private bool TreeContainsFile(Tree tree, string filePath)
{
    //filePath is the relative path of your file to the root directory
    if (tree.Any(x => x.Path == filePath))
    {
        return true;
    }

    return tree.Where(x => x.GetType() == typeof (TreeEntry))
        .Select(x => x.Target)
        .OfType<Tree>()
        .Any(branch => TreeContainsFile(branch, filePath));
}
于 2016-07-06T06:17:35.287 に答える