簡體   English   中英

如何檢查本地文件是否是tfs中的最新版本?

[英]How to check whether a local file is the latest version in tfs?

我希望能夠查詢TfsTeamProjectCollection並確定服務器上是否有更新版本的文件。 我希望能夠在不實際獲取文件的情況下執行此操作。

這可能在某個地方嗎? 我做了一些刮擦,到目前為止畫空白。

謝謝。

最簡單的方法是在工作區版本和最新版本之間使用QueryHistory ; 如果它們不同,則服務器上存在較新的最新版本。 例如:

versionControlServer.QueryHistory(
    serverPath,
    VersionSpec.Latest,
    0,
    RecursionType.Full,
    new WorkspaceVersionSpec(workspace),
    versionFrom,
    null,
    Int32.MaxValue,
    true,
    true);

這是檢查給定文件是否是最新文件的另一種方法。

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");

腳步

1)我們需要獲取包含文件路徑的Workspace 我們用

Workstation.GetLocalWorkspaceInfo Method (String)

獲取包含包含指定文件的Workspace屬性的WorkspaceInfo對象。

我們可以使用此WorkspaceInfo對象通過使用獲取該WorkspaceInfo的實例

WorkspaceInfo.GetWorkspace Method (TfsTeamProjectCollection)

2)現在我們需要使用workspace對象執行Get Operation。

Workspace.Get Method (GetRequest[], GetOptions)

第二個參數GetOptions有六個可能的成員值。 每個都有目的。

由於您不需要下載文件,

我們將使用成員值PreviewExecutes a get without modifying the disk.

3) Get操作返回一個GetStatus對象,該對象表示Workspace.Get操作的狀態。

其中包含有關處理Get操作時發生的操作,沖突,錯誤等的信息。

GetStatus對象有許多屬性。 我們使用名為NoActionNeeded屬性獲取一個標志,該標志指示是否沒有發生故障和操作。

如果未發生任何操作或錯誤,則標志值將為True 即,該文件已經是最新版本。 否則標志將為False ,這意味着該文件不是TFS中可用的最新版本。

//我們必須指定已經與本地工作空間映射的文件,以便進行比較

            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)
            {

            }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM