ChallengeFind the version

time to read 2 min | 316 words

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
	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?

More posts in "Challenge" series:

  1. (03 Jan 2020) Spot the bug in the stream–answer
  2. (15 Feb 2010) Where is the optimization?