问题
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());
回答1:
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:
- Scan the entire local working folder mappings
- Run a
QueryItems
against the local working folder mappings - For any files that exist in the local items but do not exist on the server, pend an add
- For any files that exist in the server items but do not exist on the local filesystem, pend a delete
- 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.)
来源:https://stackoverflow.com/questions/24970734/detect-file-delete-changes-with-tfs-api-automated-without-vs-interaction