Ayende @ Rahien

Unnatural acts on source code

Challenge: Find the version

Okay, here is something that I had to deal with today. I had to implement the dated-rev-report command in SvnBridge. This command sends a date to the server, and get the first revision id that was committed before to that date. Because code is unambiguous, if we were using SQL, it would have been:

CREATE PROC DatedRevReport 
	@requestedDate DATETIME
AS
	SELECT TOP 1 RevisionId
	FROM Revisions
	WHERE CommittedDate < @requestedDate
	ORDER BY CommittedDate DESC

Simple, right? Except that the interface that I have to TFS is not SQL (and I would be pretty sad if it was, so that is a good thing). I have to go through the TFS API in order to get that result.

Here is the API that I have to work with (actually, that is a drastically simplified API, but it contains all the stuff required to solve this issue).

public interface ISourceControl
{
	int GetLatestId();
	LogItem QueryLog(VersionSpec from, VersionSpec to, int countToReturn);
}

public class LogItem
{
	public SourceItemHistory[] History;
}

public class History
{
	public int ChangeSetId;
	public DateTime CommitDateTime;
}

public class DateVersionSpec : VersionSpec
{
	public DateTime Date;
}

public class ChangesetVersionSpec : VersionSpec
{
	public int ChangesetId;
}

Your task, if you agree to accept it, is to build implement the following method:

public int GetVersionForDate(DateTime date)
{
	// your implementation here
}

One important consideration is that you should reduce remote calls. I managed to get it down to 4 remote calls for the worst case scenario. It could have been 3, but TFS has some... finicky date handling.

So, how would you solve the issue?

Comments

Ravi Terala
03/24/2008 05:39 PM by
Ravi Terala

Talk directly to the TFS cube instead?

Rik Hemsley
03/24/2008 06:54 PM by
Rik Hemsley

public int GetVersionForDate(DateTime date)

{

return sourceControl.QueryLog

(

    new DateVersionSpec(date),

    new ChangesetVersionSpec(DateTime.Now),

    1

).History[0].ChangeSetId - 1;

}

  • corner case handling

Is that only 1 call or am I overestimating the TFS API?

Ayende Rahien
03/24/2008 07:00 PM by
Ayende Rahien

You are missing a couple of corner cases, yes.

Handling requests for dates before the repository started, and handling dates after the last commit.

Both would cause exceptions in the API

Rik Hemsley
03/24/2008 07:07 PM by
Rik Hemsley

Does it throw useful exceptions (e.g. something like DateRangeException rather than BadCallException)? If so, I'd be tempted to catch them and report them to the caller or user. It would be more 'correct' to ask TFS for the information first, but if cutting down on wire traffic (or new TCP connections - it can't be that bad, can it?) is that important then I think it's fair to break the "exceptions are for stuff you couldn't anticipate" rule.

I suppose the repository's first commit isn't going to change, so you can cache that, at least.

Ayende Rahien
03/24/2008 07:15 PM by
Ayende Rahien

In one case, it throw something useful (date before anything in the repos), in the other, it just do random things.

In your code, it would return the second most recent commit

Rik Hemsley
03/24/2008 07:46 PM by
Rik Hemsley

I think I might now have the same worst case as yourself.

class TFSRepository

{

private const TFSFirstRevisionId = 1; // Assumption.


private DateTime initialRevisionDate;


private DateTime InitialRevisionDate()

{

    if (initialRevisionDate == null)

    {

         initialRevisionDate = sourceControl.QueryLog(

             new ChangesetVersionSpec(TFSFirstRevisionId), 

             new ChangesetVersionSpec(TFSFirstRevisionId),

             1

        ).History[0].CommitDateTime;

    }


    return initialRevisionDate;

}


private void AssertRepositoryInitialized(int latestRevisionId)

{

    if (latestRevisionId < TFSFirstRevisionId)

        throw new RepositoryNotYetBornException();

}


private DateTime RevisionDateTime(int revisionId)

{

    DateTime currentRevisionDate = sourceControl.QueryLog

    (

           new ChangesetVersionSpec(revisionId),

           new ChangesetVersionSpec(revisionId),

           1

    ).History[0].CommitDateTime;

}


public int GetVersionForDate(DateTime date)

{

    int currentRevisionId = sourceControl.GetLatestId();


    AssertRepositoryInitialized(currentRevisionId);


    DateTime initialRevisionDateTime = RevisionDateTime(TFSFirstRevisionId);


    DateTime currentRevisionDateTime = RevisionDateTime(currentRevisionId);


   if (date < InitialRevisionDateTime)

       throw new DateBeforeFirstRevisionException(date);


   if (date > currentRevisionDateTime)

       throw new DateAfterCurrentRevisionException(date);


    return sourceControl.QueryLog

    (

        new DateVersionSpec(date),

        new ChangesetVersionSpec(currentRevisionDateTime),

    ).History[0].ChangeSetId - 1;

}

}

Ayende Rahien
03/24/2008 07:56 PM by
Ayende Rahien

Rik, take a look at:

http://www.codeplex.com/SvnBridge/SourceControl/FileView.aspx?itemId=94304&changeSetId=17051

GetVersionForDate()

Amazing

Rik Hemsley
03/24/2008 07:58 PM by
Rik Hemsley

Wow, you incorporated my code quickly! ;)

pb
03/25/2008 12:32 AM by
pb

What's up with this? A 2d return array that always has only one element in each array?

BranchRelative[][] branches =

                        SourceControlService.QueryBranches(serverUrl,

                                                           credentials,

                                                           null,

                                                           new ItemSpec[] { spec },

                                                           branchChangeset);

                    string oldName =

                        branches[0][branches[0].GetUpperBound(0)].BranchFromItem.item.Substring(rootPath.Length);
Ayende Rahien
03/25/2008 07:30 AM by
Ayende Rahien

pb,

I will make no comment about TFS at this time.

Comments have been closed on this topic.