0

After searching the googles for couple hours I found an answer to my question. I know this post Undo checkout TFS answers my question, however it doesn't answer all the questions I have. I want to achieve the same objective that the post asked about. How to only revert files that have been checked out if nothing was modified in that file? The answer to my question shouldn't be too hard to answer.

So what I'm doing is copying files from a server and overwriting them in my local workspace. I am checking out all the files being copied. However, if a file that was copied is not modified in anyway(server file and destination file are exact same), I'd like to undo the checkout of that file.

I know I'm to use the workspace.Undo() method and the gentleman said it worked for him. However he didn't show how he implemented it.

Here is the code I have with help from the link:

public static void CheckOutFromTFS(string filepath)
{
    var workspaceInfo = Workstation.Current.GetLocalWorkspaceInfo(filepath);
    if (workspaceInfo == null)
    {
        return;
    }
    var server = new TfsTeamProjectCollection(workspaceInfo.ServerUri);
    var workspace = workspaceInfo.GetWorkspace(server);
    workspace.PendEdit(filepath);
}

The answer given was to use the workspace.Undo() method. Do I add this method as the last line in CheckOutFromTFS() like so?

public static void CheckOutFromTFS(string filepath)
{
    var workspaceInfo = Workstation.Current.GetLocalWorkspaceInfo(filepath);
    if (workspaceInfo == null)
    {
        return;
    }
    var server = new TfsTeamProjectCollection(workspaceInfo.ServerUri);
    var workspace = workspaceInfo.GetWorkspace(server);
    workspace.PendEdit(filepath);
    workspace.Undo();
}

Or is it done differently? I'm not sure if this Undo() will only revert files if there are no changes or just revert the checkout entirely and render the PendEdit() useless. Can someone help clarify this for me?

Zoe
  • 23,712
  • 16
  • 99
  • 132
itsNino91
  • 129
  • 15
  • how come you can't just use the IDE if you are hooked into using TFS to do the manual undo changes .. just curious ..because the functionality is already there and working .. – MethodMan Feb 26 '15 at 16:45
  • My plan is to just use this little executable app to copy files over and automatically check out the files that were modified and allow the user to test. I do not want the user to have to open the IDE to manually uncheckout the files. Just trying to save them some time. – itsNino91 Feb 26 '15 at 17:13
  • I was just curious .. thanks for the clarification – MethodMan Feb 26 '15 at 17:14

3 Answers3

0

If you use a local workspace then all file that have no changes will automatically revert to not checked-out. You don't need to do anything at all. This works with VS 2012 or better with TFS 2012 or better. You'll need to convert you workspace to a local workspace first like this

enter image description here

Etienne
  • 974
  • 4
  • 9
  • So tfs is smart enough that after the pendEdit function is run, it will know if the file has been modified and if it hasn't it will automatically uncheckout the file? The user currently must have the workspace mapped locally in order for this app to work. – itsNino91 Mar 03 '15 at 16:53
0

So I found the answer to my question in various posts. I kinda took bits an pieces and combined them together to get my working solution. The use of the Undo() function with passing in the filepath actually does uncheckout the file regardless if it was modified or not. My workspace was also local but VS and TFS couldn't automatically revert those unmodified files for me so I took the below approach.

So what I decided to do was to just use the Team Foundation Power Tools "uu" command to undo the changes to unchanged files in the workspace. I created a batch file and entered the following command: echo y | tfpt uu . /noget /recursive. Since we will not show the shell during execution, I used the "echo y" command to automatically answer the question, "Do you wish to undo these redundant pending changes? (Y/N)". Including /noget is highly recommended since it prevents a forced 'get latest' of all your project's files which depending on the total number can take a extremely long time.

 var startInfo = new System.Diagnostics.ProcessStartInfo
 {
     WorkingDirectory = projectRoot,
     FileName = projectRoot + @"\undoUnchanged.bat",
     UseShellExecute = false,
     CreateNoWindow = true
 };
 Process process = Process.Start(startInfo);
 process.WaitForExit();
 process.Close();

After the script runs and the process.Close() executes you and double check if your unmodified files actually were unchecked out by hitting the refresh button on the Team Explorer window in your project. Hope someone else can find some use in this.

itsNino91
  • 129
  • 15
0

If I understand the question well and you actually need undo through C# code behind, I believe this shoul help you:

Undo checkout TFS

Community
  • 1
  • 1
Mr.B
  • 2,848
  • 20
  • 34