CreateChangesetAsync-如何在不知道编码或文件类型(仅文件路径)的情况下检入现有文件

时间:2018-10-15 13:27:29

标签: rest tfs

我尝试遵循有关如何创建具有多个文件的变更集的示例:[查看链接] [1]

尽管我在TFVCItem和ItemContent阶段有些卡住,但我不知道如何提取内容和编码我的文件。

我正在尝试编写一些代码,以检入filePath给我的文件并在给定位置检入。

有人会在乎如何帮助我吗?

这是我到目前为止提出的:

  Public Function CreateChangeset(ByVal projectName As String,
                                      ByVal files As Dictionary(Of String, String),
                                      ByVal comment As String) As TfvcChangesetRef

    Dim c = TFSConnection.GetClient(Of TfvcHttpClient)
    Dim newChangetset = New TfvcChangeset

    Dim changes = New List(Of TfvcChange)

    For Each fKP In files
        Dim fileSource = fKP.Key
        Dim fileTarget = fKP.Value

        Dim newChange = New TfvcChange

        newChange.ChangeType = VersionControlChangeType.Add

        Dim newItem = New TfvcItem
        newItem.Path = $"&/{projectName}/{fileTarget}"
        newItem.ContentMetadata = New FileContentMetadata

        '' TODO: How to extract the correct encoding, and type?...
        'newItem.ContentMetadata.Encoding = GetFileEncoding(fileSource)
        'newItem.ContentMetadata.ContentType = "text/plain"
        'newChange.Item = newItem

        '' TODO: How to extract the correct content, and type?...
        'Dim newContent = New ItemContent
        'newContent.Content = "Blabla"
        'newContent.ContentType = ItemContentType.RawText
        'newChange.NewContent = newContent

        changes.Add(newChange)
    Next
    newChangetset.Changes = changes
    newChangetset.Comment = comment

    Dim changesetRef = c.CreateChangesetAsync(newChangetset).Result

    Return changesetRef
End Function

更新: 好的,所以我设法使其正常工作,但是我仍然不确定如何正确设置ContentType。

我可以在ItemContentType.RawText和ItemContentType.Base64Encoded之间进行选择,但是我不确定何时使用其中一个。

以下是新代码似乎有效:

    Public Function CreateChangeset(ByVal projectName As String,
                                ByVal files As Dictionary(Of String, String),
                                ByVal comment As String) As TfvcChangesetRef

    Dim c = TFSConnection.GetClient(Of TfvcHttpClient)
    Dim newChangetset = New TfvcChangeset

    Dim changes = New List(Of TfvcChange)

    For Each fKP In files

        ' Extract and build our target and source paths.
        Dim fileSource = fKP.Key
        Dim fileTarget = fKP.Value
        Dim fileName = IO.Path.GetFileName(fileSource)

        Dim newChange = New TfvcChange

        ' Create the new TFVC item which will be checked-in.
        Dim newItem = New TfvcItem
        newItem.Path = $"$/{projectName}/{fileTarget}/{fileName}"
        newItem.ContentMetadata = New FileContentMetadata

        ' Try to extract the item from the server.
        Dim serverItem = c.GetItemAsync(newItem.Path).Result

        If serverItem Is Nothing Then
            ' If the file is not on the server, then its a new file.
            newChange.ChangeType = VersionControlChangeType.Add
        Else
            ' Indicate that we are dealing with a file modification 
            ' and specify which version we are editing.
            newChange.ChangeType = VersionControlChangeType.Edit
            newItem.ChangesetVersion = serverItem.ChangesetVersion
        End If

        ' Read the file content to a stream.
        Using reader = New StreamReader(fileSource,
                                        Text.Encoding.Default,
                                        True) ' This last parameter allows to extract the correct encoding.
            Dim fileContent As String = String.Empty

            ' Read all the file content to a string so that we can store 
            ' it in the itemcontent.
            ' NOTE: reading it also allows to retrieve the correct file enconding.
            If reader.Peek() >= 0 Then
                fileContent = reader.ReadToEnd
            End If

            ' Set the file enconding and MIME Type.
            newItem.ContentMetadata.Encoding = reader.CurrentEncoding.WindowsCodePage
            newItem.ContentMetadata.ContentType = System.Web.MimeMapping.GetMimeMapping(fileSource)
            newChange.Item = newItem

            ' Set the file content.
            Dim newContent = New ItemContent
            newContent.Content = fileContent

            ' TODO: What should be the logic to set the Content Type? Not too sure...
            ' If newItem.ContentMetadata.ContentType.StartsWith("text/") Then
            newContent.ContentType = ItemContentType.RawText
            '  Else
            '  newContent.ContentType = ItemContentType.Base64Encoded
            '  End If

            ' Store the content to the change.
            newChange.NewContent = newContent
        End Using

        changes.Add(newChange)
    Next

    newChangetset.Changes = changes
    newChangetset.Comment = comment

    Dim changesetRef = c.CreateChangesetAsync(newChangetset).Result

    Return changesetRef
End Function

0 个答案:

没有答案