How to check whether a local file is the latest ve

2019-06-22 00:02发布

I want to be able to interrogate TfsTeamProjectCollection and determine if there is a newer version of a file on the server. I would like to be able to do this without actually fetching the file.

Is this possible somewhere? I've done some scratching around and so far drawing blanks.

Thanks.

标签: c# tfs tfs-sdk
3条回答
乱世女痞
2楼-- · 2019-06-22 00:03

The easiest way is to QueryHistory between the workspace version and the latest version; if they differ, there exists a newer latest version on the server. Eg:

versionControlServer.QueryHistory(
    serverPath,
    VersionSpec.Latest,
    0,
    RecursionType.Full,
    new WorkspaceVersionSpec(workspace),
    versionFrom,
    null,
    Int32.MaxValue,
    true,
    true);
查看更多
手持菜刀,她持情操
3楼-- · 2019-06-22 00:11

//We have to specify files which are already mapped with local workspace for compare here

            var serverPath = workspace.GetServerItemForLocalItem(Vars.sLocalPath);
            var serverVersion = new DiffItemVersionedFile(versionControlServer, serverPath, VersionSpec.Latest);
            var localVersion = new DiffItemLocalFile(Vars.sLocalPath, System.Text.Encoding.UTF8.CodePage, DateTime.Now, false);

            try
            {
                using (var stream = new MemoryStream())
                using (var writer = new StreamWriter(stream))
                {
                    var diffOptions = new DiffOptions
                    {
                        Flags = DiffOptionFlags.EnablePreambleHandling,
                        OutputType = DiffOutputType.Unified,
                        TargetEncoding = System.Text.Encoding.UTF8,
                        SourceEncoding = System.Text.Encoding.UTF8,
                        StreamWriter = writer
                    };

                    Difference.DiffFiles(versionControlServer, serverVersion, localVersion, diffOptions, serverPath, true);
                    writer.Flush();

                    diff = System.Text.Encoding.UTF8.GetString(stream.ToArray());
                    if (diff != "")
                    {
                        newutils.WriteLogFile("Vars.enumExitCode.Success");
                        iRtnCode = (int)Vars.enumExitCode.Success;
                        return iRtnCode;
                    }
                }
            }
            catch (Exception)
            {

            }
查看更多
我想做一个坏孩纸
4楼-- · 2019-06-22 00:22

This is another way to check whether a given file is latest or not.

string file_path = @"your_file_path";

WorkspaceInfo info = Workstation.Current.GetLocalWorkspaceInfo(file_path);

Workspace ws = info.GetWorkspace(new TfsTeamProjectCollection(info.ServerUri));

GetStatus status = ws.Get( new GetRequest(
                                          new ItemSpec ( file_path, RecursionType.Full ), 
                                          VersionSpec.Latest ), 
                            GetOptions.Preview );

if(status.NoActionNeeded)
     MessegeBox.Show("Latest");
else
     MessegeBox.Show("Not Latest");

STEPS

1) We need to get the Workspace that contains the file path. We use

Workstation.GetLocalWorkspaceInfo Method (String)

to get WorkspaceInfo object which contains properties of the Workspace that contains the specified file.

We can use this WorkspaceInfo object to get the instance of that workspace by using

WorkspaceInfo.GetWorkspace Method (TfsTeamProjectCollection)

2) Now we need to perform a Get Operation with the workspace object.

Workspace.Get Method (GetRequest[], GetOptions)

The second parameter, GetOptions has six possible member values. Each with a purpose.

Since you don't need to download the file,

we will use the member value Preview which Executes a get without modifying the disk.

3) The Get operation returns a GetStatus object which represents the status of a Workspace.Get operation.

This contains information about how many operations, conflicts, errors, and so on occurred when the Get operation was being processed.

GetStatus object has a number of properties. We use property called NoActionNeeded which gets a flag that indicates whether no failures and no operations occurred.

The flag value will be True if no operations or errors occurred. ie, the file is already the latest version. Otherwise the flag will be False which means the file is not the latest version available in TFS.

查看更多
登录 后发表回答