2013-08-05 3 views
1

커밋 날짜순으로 주문 된 레포 파일 컬렉션을 보유하고 싶습니다.커밋 날짜까지 Repo 커밋 된 파일 주문

File  Committed 
abc.bat Dec 1 2013 
bar.txt Jan 1 2010 
baz.cmd Nov 8 2010 
cru.zip Feb 9 2012 

커밋 날짜로 주문할 수 있도록 LibGit2Sharp에서 어떻게 처리 할 수 ​​있습니까?

내가 이것을해야하는 이유는 LibGit2Sharp가 .Pull()을 허용하지 않기 때문에 변경 사항이 병합됩니다. 그렇다면 System.IO.DirectoryInfo으로 이동하고 Windows에서 수정 한 날짜로 쿼리하십시오. .Clone() 만 있어도 파일 시스템에서 해당 날짜가 유지되지 않는 것 같습니다.

답변

1

흠. 이 필요에 맞는 상자는 없습니다.

그러나 개정 내역을 거꾸로보고 추가 및 수정을 식별하면 검사중인 커밋에있는 각 파일의 최신 변경 날짜를 수집 할 수 있습니다.

어때?

public void LetUsTryThis() 
{ 
    using (var repo = new Repository(StandardTestRepoPath)) 
    { 
     var referenceCommit = repo.Head.Tip; 

     IEnumerable<KeyValuePair<string, DateTimeOffset>> res = LatestChanges(repo, referenceCommit); 

     foreach (KeyValuePair<string, DateTimeOffset> kvp in res) 
     { 
      Console.WriteLine(string.Format("{0} - {1}", kvp.Key, kvp.Value)); 
     } 
    } 
} 

private IEnumerable<KeyValuePair<string, DateTimeOffset>> LatestChanges(Repository repo, Commit referenceCommit) 
{ 
    IDictionary<string, DateTimeOffset> dic = new Dictionary<string, DateTimeOffset>(); 

    var commitLog = repo.Commits.QueryBy(new CommitFilter { Since = referenceCommit }) 
         .Concat(new[] { default(Commit) }) 
         .Skip(1); 

    var mostRecent = referenceCommit; 

    foreach (Commit current in commitLog) 
    { 
     IEnumerable<KeyValuePair<string, DateTimeOffset>> res = ExtractAdditionsAndModifications(repo, mostRecent, current); 
     AddLatest(dic, res); 

     mostRecent = current; 
    } 

    return dic.OrderByDescending(kvp => kvp.Value); 
} 

private IEnumerable<KeyValuePair<string, DateTimeOffset>> ExtractAdditionsAndModifications(Repository repo, Commit next, Commit current) 
{ 
    IDictionary<string, DateTimeOffset> dic = new Dictionary<string, DateTimeOffset>(); 

    var tc = repo.Diff.Compare(current == null ? null : current.Tree, next.Tree); 

    foreach (TreeEntryChanges treeEntryChanges in tc.Added) 
    { 
     dic.Add(treeEntryChanges.Path, next.Committer.When); 
    } 

    foreach (TreeEntryChanges treeEntryChanges in tc.Modified) 
    { 
     dic.Add(treeEntryChanges.Path, next.Committer.When); 
    } 

    return dic; 
} 

private void AddLatest(IDictionary<string, DateTimeOffset> main, IEnumerable<KeyValuePair<string, DateTimeOffset>> latest) 
{ 
    foreach (var kvp in latest) 
    { 
     if (main.ContainsKey(kvp.Key)) 
     { 
      continue; 
     } 

     main.Add(kvp); 
    } 
} 
+0

@ p.campbell이 답변은 도움이 되었습니까? – nulltoken