2

I have a local folder within the tfs workspace and use the TFS-API.

Every night i do delete this folder and and after that script a database as .sql files. If now something was added or edited, the changes will be found and get into the pending changes to checkin.

The question is how i can detect with the TFS-API that a file is missing (intentionally, because no longer in database and becaus of that no longer scripted). The first step is obvious, delete all files and script all in the empty folder.

I use workspace.PendEdit at the beginning which makes me able to override the files from external. After the scripting of database is done, i will do a workspace.PendEdit and workspace.PendAdd.

This works as expected. But workspace.PendDelete does not find deleted files, and therefore cannot adding theses as deleted to the pending changes.

There is a commeandline tool from power tools for tfs which has a online flag which should do that, what i wan.

My Question: is it possible to do, what i described with the tfs-api? If this wont working, has anybody expierence with this online-flag?


Simple Example

I have two files in a local folder: 1.sql and 2.sql These two files are checked in initially.

I delete 2.sql local (without knowledge of tfs) I add one file: 3.sql I edit 1. sql

Now, i use workspace.PendEdit and workspace PendAdd to detect edited and added files. These files will be detected just fine.

And here is the problem: the deletion oder the missing of 2.sql will not be detected and therefore cannot be deleted on the server.

So: how to detect missing files / locally deleted files without knowledge of tfs?

I hope this clarifies my question.

Pseudo code example:

DeleteWorkspaceFolderContent(); // Because i do not delete but regenerate my Sql scripts.
GenerateSqlScriptsToWorkspaceFolder(); // .sql files are generated to the same folder, they were deleted before

// Now at this point, i did workspace.PendAdd(localPath, true); and workspace.PendEdit(localPath, RecursionType.Full); which worked like a sharm, so new .sql files will be pended for adding and edited (real changed scripts) pended for "change-Checkin".

// At this point i tought workspace.PendDelete(new [] { localPath }, RecurstionType.Fulll); does the same: Seeing that a file is locally missing and then pend this files for deletion on the server. But this does not happen. No pending changes are added.

var pendingChanges = workspace.GetPendingChanges(localPath, RecursionType.Full);

workspace.CheckIn(pendingChanges, "Per TFS-API, " + DateTime.Now.ToString());
Kiquenet
  • 14,494
  • 35
  • 148
  • 243
Peter Bucher
  • 295
  • 2
  • 13
  • Hi Edward. I havent seen the updateDisk argument before. But as discribed in MSDN:"True to delete files from the local disk." it doesnt seem to do, what i want. I want the tfs to detect, whether a file was deleted outside the tfs tooling area. Or detect missing files (files where are available on server, but no longer on disk). I will try with this parameter, but maybe you now got the idea, what i wanna to archieve? – Peter Bucher Jul 28 '14 at 09:12
  • `PendDelete` does not require the file to exist on disk. Can you post some code that illustrates this failure? – Edward Thomson Jul 28 '14 at 13:37
  • Oh, I see. You don't even know what it is that you *want* to delete. – Edward Thomson Jul 28 '14 at 13:59
  • Exactly. I would have to do my own diff to ever know what i then could explicit call to delete on the server with workspace.Delete() or something like this. Iam searching for something equivalent to "missing files" in SVN or another vcs. I have added some pseudo code on my question above. – Peter Bucher Jul 28 '14 at 14:09

1 Answers1

4

What you really want to do is use a Local Workspace and let TFS deal with this for you. It will detect modifications, additions and removals of files and pend them for you automatically.

If you want to use the TFS API for this, then you will need to examine your "candidate pending changes" (the ones that the TFS client has determined exist on disk) and then promote them to actual pending changes. For example:

PendingChange[] candidates;
workspace.GetPendingChangesWithCandidates(
    new ItemSpec[] { new ItemSpec(@"C:\Local\Path", RecursionType.Full) },
    false,
    out candidates);

foreach (PendingChange pc in candidates)
{
    if ((pc.ChangeType & ChangeType.Delete) == ChangeType.Delete)
    {
        workspace.PendDelete(pc.LocalItem);
    }
    else if ((pc.ChangeType & ChangeType.Add) == ChangeType.Add)
    {
        workspace.PendAdd(pc.LocalItem);
    }
    else
    {
        workspace.PendEdit(pc.LocalItem);
    }
}

PendingChange[] changes = workspace.GetPendingChanges();

/* Now you can review and CheckIn your changes. */

If you can't do that (because you're running old versions of TFS or Visual Studio, or for some other reason) then you should just use the TFS Power Tools and run tfpt online.

If you really, really want to use the TFS API for this, then you'll simply have to do what the tfpt online command does:

  1. Scan the entire local working folder mappings
  2. Run a QueryItems against the local working folder mappings
  3. For any files that exist in the local items but do not exist on the server, pend an add
  4. For any files that exist in the server items but do not exist on the local filesystem, pend a delete
  5. For any other item, determine if it is writable. If so, pend an edit. (Note, you may wish to check the MD5 hash of the file to determine if it was actually edited or just marked writable.)
Edward Thomson
  • 74,857
  • 14
  • 158
  • 187
  • I use VS2012 and TFS2012. Do you say: If i use a local workspace and do a simple "checkin" via TFS-API, it should do all what i want, the way i excpect it? – Peter Bucher Jul 28 '14 at 14:17
  • @PeterBucher There's one additional step to "promote" your "candidate" pending changes to actual pending changes, I updated the answer to include some sample code. – Edward Thomson Jul 28 '14 at 14:33
  • OK. One question left: The sample code applies also to server workspace or only to local workspace? – Peter Bucher Jul 28 '14 at 14:37
  • @PeterBucher Only to local workspaces are able to scan the filesystem; server workspaces must have all changes made explicitly. – Edward Thomson Jul 28 '14 at 14:39
  • Allright, thank you! I will test this with local workspace and the api-code. will report later on. – Peter Bucher Jul 28 '14 at 14:46
  • Nice, it seems to work as expected (i think local workspace is more equal to other systems like git or mercurial). You have one mistake in your code (Line 3): replace ", true" with ", RecursionType.Full". – Peter Bucher Jul 28 '14 at 15:18
  • Hi Edward Thomson. It had seemed working. But i had to realize, that PendingChanges candidates are found and are pended correctly (PendEdit, PendDelete, etc... BUT with the .GetPendingChanges() Call, no PendingChange are returned. I have no idea why. And directly Checkin the candidates gives me a "file xyz is not checked out" message, which i dont understand why. Maybe you have an idea? Thanks. – Peter Bucher Aug 18 '14 at 14:37