简体   繁体   中英

How to detect that TFS Project Source Code has been modified

I have a Visual Studio C# project in TFS that compiles into a DLL. I need to know if any of the files inside the project has been modified in a period of time. Something like this:

"from date 01/01/2011 to today,  check if the project has been modified."

What is the best approach to do this using the TFS API in C#?

My idea: Get all the project items from TFS, for each item check it's change history. Filter the history according to the given range period. If there are changes in that period, then that file was modified, hence the project has been altered. Is this correct?

Could it be possible to do the same but not searching between a period of time, but in a range of changesets? For example:

"from changeset 1071 to latest,  check if the project has been modified."

You can use the QueryHistory method for these sorts of questions:

var tfs = new TfsTeamProjectCollection(new Uri(@"http://tfs.example.com:8080"));
var vc = tfs.GetService<VersionControlServer>();

// DateTime fro, to;
var changesets = from cs in vc.QueryHistory(
    "$/YourProject",
    VersionSpec.Latest,
    0,    /* deletion ID, 0 otherwise */
    RecursionType.Full,
    null, /* user, null for all */
    null, /* from changeset, null because these aren't by date */
    null, /* to changeset, null because these aren't by date   */
    Int32.MaxValue, /* get them all */
    false, /* just metadata */
    false /* just the current stuff */
    )
    where cs.CreationDate >= fro && cs.CreationDate <= to
    select cs;

Once you have the list of changesets, you could query them for the files they are associated with. At this point, if you really really wanted to exclude false positives, I would tack on Roslyn support for reading Projects and Solutions and use that to filter interesting file names.

The problem you'll have here is that the project might contain files that are not in the same root folder. It's common in large solutions to include files that lie outside of the project directory.

Another problem you'll face is that not all files in source control under a project directory actually have to be included in the project.

Now, if you don't care about these cases, the simplest way to do this is by using the tf command line, tf folderdiff. You can pass it two paths (which can both be on the server) and though the documentation doesn't specify it clearly, you can tack a versionspec to it like so:

 tf folderdiff $/Path/To/Project;C1071 $/Path/To/Project;T /recursive

The examples for what you can tack on are documented here . It includes dates, Changeset numbers, Label names etc.

If you want to know how this is done, consider viewing the tf.exe utility in Reflector.NET or JustDecompile (or a similar application). They use the same API as you would. Or simply invoke the tf folderdiff command directly using the Process class.

I have a blog post that explains a little more about the tf folderdiff command and how to use it.

The class that holds the magic is here:

 internal class FolderDiff : IEnumerable<FolderDiffFolder>, IEnumerable, IDisposable

 Name:     Microsoft.TeamFoundation.VersionControl.Controls.FolderDiff 
 Assembly: Microsoft.TeamFoundation.VersionControl.Controls, Version=11.0.0.0 

It is internal, so you can peek to see how it's done, but there is no easy access to an API to get the same result.

I'm not quite sure why @Alex deleted his answer, as it's basically a good start.

As he suggested, you can run tf.exe to get the history on a file or entire folder hierarchy, and you can limit the results to lie between two versions, changesets or dates. So if your project is "normal" and pretty much all resides under a single root folder, a simple approach is just to tf history on the root folder to get a list of the files that have changed. You will have to parse the output to work out which files are affected, but that's a pretty easy task - even easier if all you want to know is "has anything at all changed?".

As @jessehouwig says, there may be files that you wish to consider from other locations, and files within the project folder that you don't want to consider. In this case you may wish to write a batch file or application that calls tf.exe multiple times to gather information in a more specific way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM