Skip to content

Commit a1cb1c2

Browse files
committed
Add FindCommitByChangesetIdAndPath method
1 parent 19da72e commit a1cb1c2

File tree

1 file changed

+58
-2
lines changed

1 file changed

+58
-2
lines changed

GitTfs/Core/GitRepository.cs

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ private void FindTfsParentCommits(List<TfsChangesetInfo> changesets, Commit comm
349349

350350
public TfsChangesetInfo GetTfsChangesetById(string remoteRef, long changesetId, string tfsPath)
351351
{
352-
var commit = FindCommitsByChangesetId(changesetId, remoteRef).FirstOrDefault(c => c.Message.Contains(tfsPath));
352+
var commit = FindCommitByChangesetIdAndPath(changesetId, tfsPath, remoteRef);
353353
if (commit == null)
354354
return null;
355355
return TryParseChangesetInfo(commit.Message, commit.Sha);
@@ -533,7 +533,7 @@ public bool CreateBranch(string gitBranchName, string target)
533533

534534
public string FindCommitHashByChangesetId(long changesetId, string tfsPath)
535535
{
536-
var commit = FindCommitsByChangesetId(changesetId).FirstOrDefault(c => c.Message.Contains(tfsPath));
536+
var commit = FindCommitByChangesetIdAndPath(changesetId, tfsPath);
537537
if (commit == null)
538538
return null;
539539

@@ -560,6 +560,62 @@ public static bool TryParseChangesetId(string commitMessage, out long changesetI
560560
return false;
561561
}
562562

563+
private Commit FindCommitByChangesetIdAndPath(long changesetId, string tfsPath, string remoteRef = null)
564+
{
565+
Trace.WriteLine("Looking for changeset " + changesetId.ToString() + " in git repository...");
566+
567+
if (remoteRef == null)
568+
{
569+
IList<string> shas;
570+
if (changesetsCache.TryGetValue(changesetId, out shas))
571+
{
572+
var commitFromCache = shas.Select(sha => _repository.Lookup<Commit>(sha)).FirstOrDefault(c => c.Message.Contains(tfsPath));
573+
if (commitFromCache != null)
574+
return commitFromCache;
575+
}
576+
577+
if (cacheIsFull)
578+
return null;
579+
}
580+
581+
var reachableFromRemoteBranches = new CommitFilter
582+
{
583+
Since = _repository.Branches.Where(p => p.IsRemote),
584+
SortBy = CommitSortStrategies.Time
585+
};
586+
587+
if (remoteRef != null)
588+
reachableFromRemoteBranches.Since = _repository.Branches.Where(p => p.IsRemote && p.CanonicalName.EndsWith(remoteRef));
589+
590+
var commitsFromRemoteBranches = _repository.Commits.QueryBy(reachableFromRemoteBranches);
591+
592+
Commit commit = null;
593+
foreach (var c in commitsFromRemoteBranches)
594+
{
595+
long id;
596+
if (TryParseChangesetId(c.Message, out id))
597+
{
598+
AddToChangesetCache(changesetId, c.Sha);
599+
600+
if (id == changesetId && c.Message.Contains(tfsPath))
601+
{
602+
commit = c;
603+
break;
604+
}
605+
}
606+
}
607+
608+
if (remoteRef == null && commit == null)
609+
cacheIsFull = true; // repository fully scanned
610+
611+
if (commit == null)
612+
Trace.WriteLine(" => Commit not found!");
613+
else
614+
Trace.WriteLine(" => Commit found! hash: " + commit.Sha);
615+
616+
return commit;
617+
}
618+
563619
private IEnumerable<Commit> FindCommitsByChangesetId(long changesetId, string remoteRef = null)
564620
{
565621
Trace.WriteLine("Looking for changeset " + changesetId.ToString() + " in git repository...");

0 commit comments

Comments
 (0)