Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FindCommitByChangesetId should look in a specific Tfs branch - enhacements #9

Open
wants to merge 2 commits into
base: find_commit_in_specific_branch
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions GitTfs/Commands/Checkout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using NDesk.Options;
using Sep.Git.Tfs.Core;
using StructureMap;
Expand Down Expand Up @@ -43,10 +44,15 @@ public int Run(string id)
long changesetId;
if(!long.TryParse(id, out changesetId))
throw new GitTfsException("error: wrong format for changeset id...");
//TODO
var sha = _globals.Repository.FindCommitHashByChangesetId(changesetId, "TODO");
if (string.IsNullOrEmpty(sha))
throw new GitTfsException("error: commit not found for this changeset id...");

var shas = _globals.Repository.FindCommitHashesByChangesetId(changesetId);
if (shas.Count == 0)
throw new GitTfsException("error: commit not found for " + changesetId.ToString() + " changeset id...");
if (shas.Count > 1)
throw new GitTfsException("error: found more than one commit for " + changesetId.ToString() + " changeset id...");

var sha = shas.Single();

if (ReturnShaOnly)
{
_stdout.Write(sha);
Expand Down
60 changes: 40 additions & 20 deletions GitTfs/Core/GitRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,18 @@ public GitCommit Commit(LogEntry logEntry)
logEntry.Tree,
parents,
false);
changesetsCache[logEntry.ChangesetId] = commit.Sha;
AddToChangesetCache(logEntry.ChangesetId, commit.Sha);
return new GitCommit(commit);
}

private void AddToChangesetCache(long changesetId, string sha)
{
IList<string> shas;
if (!changesetsCache.TryGetValue(changesetId, out shas))
changesetsCache[changesetId] = shas = new List<string>();
shas.Add(sha);
}

public void UpdateRef(string gitRefName, string shaCommit, string message = null)
{
if (message == null)
Expand Down Expand Up @@ -341,7 +349,7 @@ private void FindTfsParentCommits(List<TfsChangesetInfo> changesets, Commit comm

public TfsChangesetInfo GetTfsChangesetById(string remoteRef, long changesetId, string tfsPath)
{
var commit = FindCommitByChangesetId(changesetId, tfsPath, remoteRef);
var commit = FindCommitsByChangesetId(changesetId, remoteRef).FirstOrDefault(c => c.Message.Contains(tfsPath));
if (commit == null)
return null;
return TryParseChangesetInfo(commit.Message, commit.Sha);
Expand Down Expand Up @@ -520,18 +528,23 @@ public bool CreateBranch(string gitBranchName, string target)
return reference != null;
}

private readonly Dictionary<long, string> changesetsCache = new Dictionary<long, string>();
private readonly Dictionary<long, IList<string>> changesetsCache = new Dictionary<long, IList<string>>();
private bool cacheIsFull = false;

public string FindCommitHashByChangesetId(long changesetId, string tfsPath)
{
var commit = FindCommitByChangesetId(changesetId, tfsPath);
var commit = FindCommitsByChangesetId(changesetId).FirstOrDefault(c => c.Message.Contains(tfsPath));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like a lot this solution which consist in travelling along a lot of commit to construct a list and then filter this list with the tfs path :( Because that's not very good in terms of performance...

I'm pretty shure that we can write a private method that take an optional path and :

  • if the path is not empty, return immediatly if the commit with the good id and path is found
  • if the path is empty, continue to look for this specific changeset id through all the commits

This method could be wrapped in 2 public methods one which accept a tfs path and one another without...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because that's not very good in terms of performance...

Most of the cases there will be only one commit in the list, so no performance issues.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the path is not empty, return immediatly if the commit with the good id and path is found
if the path is empty, continue to look for this specific changeset id through all the commits

We can not do this, because then changesetsCache will be corrupted, eg. some commits will never be found.

if (commit == null)
return null;

return commit.Sha;
}

public ICollection<string> FindCommitHashesByChangesetId(long changesetId)
{
return FindCommitsByChangesetId(changesetId).Select(x => x.Sha).ToList();
}

private static readonly Regex tfsIdRegex = new Regex("^git-tfs-id: .*;C([0-9]+)\r?$", RegexOptions.Multiline | RegexOptions.Compiled | RegexOptions.RightToLeft);

public static bool TryParseChangesetId(string commitMessage, out long changesetId)
Expand All @@ -547,17 +560,18 @@ public static bool TryParseChangesetId(string commitMessage, out long changesetI
return false;
}

private Commit FindCommitByChangesetId(long changesetId, string tfsPath, string remoteRef = null)
private IEnumerable<Commit> FindCommitsByChangesetId(long changesetId, string remoteRef = null)
{
Trace.WriteLine("Looking for changeset " + changesetId + " in git repository...");
Trace.WriteLine("Looking for changeset " + changesetId.ToString() + " in git repository...");

if (remoteRef == null)
{
string sha;
if (changesetsCache.TryGetValue(changesetId, out sha))
return _repository.Lookup<Commit>(sha);
IList<string> shas;
if (changesetsCache.TryGetValue(changesetId, out shas))
return shas.Select(sha => _repository.Lookup<Commit>(sha));

if (cacheIsFull)
return null;
return Enumerable.Empty<Commit>();
}

var reachableFromRemoteBranches = new CommitFilter
Expand All @@ -570,24 +584,30 @@ private Commit FindCommitByChangesetId(long changesetId, string tfsPath, string
reachableFromRemoteBranches.Since = _repository.Branches.Where(p => p.IsRemote && p.CanonicalName.EndsWith(remoteRef));
var commitsFromRemoteBranches = _repository.Commits.QueryBy(reachableFromRemoteBranches);

Commit commit = null;
var commits = new List<Commit>();
foreach (var c in commitsFromRemoteBranches)
{
long id;
if (TryParseChangesetId(c.Message, out id))
{
changesetsCache[changesetId] = c.Sha;
if (id == changesetId && c.Message.Contains(tfsPath))
{
commit = c;
break;
}
AddToChangesetCache(changesetId, c.Sha);

if (id == changesetId)
commits.Add(c);
}
}
if (remoteRef == null && commit == null)

if (remoteRef == null && commits.Count == 0)
cacheIsFull = true; // repository fully scanned
Trace.WriteLine((commit == null) ? " => Commit not found!" : " => Commit found! hash: " + commit.Sha);
return commit;

if (commits.Count == 0)
Trace.WriteLine(" => Commit not found!");
else if (commits.Count == 1)
Trace.WriteLine(" => Commit found! hash: " + commits.First().Sha);
else
Trace.WriteLine(" => Multiple commits found! hashes: " + string.Join(", ", commits.Select(c => c.Sha)));

return commits;
}

public void CreateTag(string name, string sha, string comment, string Owner, string emailOwner, System.DateTime creationDate)
Expand Down
1 change: 1 addition & 0 deletions GitTfs/Core/IGitRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public interface IGitRepository : IGitHelpers
bool CreateBranch(string gitBranchName, string target);
Branch RenameBranch(string oldName, string newName);
string FindCommitHashByChangesetId(long changesetId, string tfsPath);
ICollection<string> FindCommitHashesByChangesetId(long changesetId);
void CreateTag(string name, string sha, string comment, string Owner, string emailOwner, System.DateTime creationDate);
void CreateNote(string sha, string content, string owner, string emailOwner, DateTime creationDate);
void MoveRemote(string oldRemoteName, string newRemoteName);
Expand Down