Skip to content

Commit b0fa78f

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

File tree

1 file changed

+61
-2
lines changed

1 file changed

+61
-2
lines changed

GitTfs/Core/GitRepository.cs

Lines changed: 61 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,65 @@ 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 c1 = shas.Select(sha => _repository.Lookup<Commit>(sha)).FirstOrDefault(c => c.Message.Contains(tfsPath));
573+
if (c1 != null)
574+
return c1;
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)
601+
{
602+
if (c.Message.Contains(tfsPath))
603+
{
604+
commit = c;
605+
break;
606+
}
607+
}
608+
}
609+
}
610+
611+
if (remoteRef == null && commit == null)
612+
cacheIsFull = true; // repository fully scanned
613+
614+
if (commit == null)
615+
Trace.WriteLine(" => Commit not found!");
616+
else
617+
Trace.WriteLine(" => Commit found! hash: " + commit.Sha);
618+
619+
return commit;
620+
}
621+
563622
private IEnumerable<Commit> FindCommitsByChangesetId(long changesetId, string remoteRef = null)
564623
{
565624
Trace.WriteLine("Looking for changeset " + changesetId.ToString() + " in git repository...");

0 commit comments

Comments
 (0)