It's all about the answers!

Ask a question

How to read a changed file content in changeset

silencehill wu (5022632) | asked Nov 27 '12, 11:02 a.m.

Now I am writing a server precondition using RTC-SDK.

When I deliver a changeset, I have get the changeset. And file path have been get like below.

 IVersionableHandle versionableHandle = change.afterState();
     ServiceConfigurationProvider configProvider = ServiceConfigurationProvider.FACTORY.create(
       data.getDestWorkspace(), changeSet.getComponent());
     IVersionable item = (IVersionable) scmService.fetchState(versionableHandle, null, null);
     IAncestorReport reports[] = scmService.configurationLocateAncestors(
       configProvider, new IVersionableHandle[] {versionableHandle}, null, null);

    String path = toFullPath(item, reports[0].getNameItemPairs()) 

But Now I want to read this changed file content when modifiy operation. Because I want to to make sure a specific value in this changed file is not changed. So the question is:

1. How can I read content  of this file in local workspace and  content in remote workspace. So I can compare difference between the two file content.

2. Otherwise, if any other method to get changed content directly to avoid comparing two file content? 

Please give an advise.


silencehill wu commented Nov 29 '12, 3:32 a.m. | edited Dec 07 '12, 10:25 a.m.

Otherwise, how can I read a file and wirte the content of file into a outputstream, so I can check the change value.

One answer

permanent link
Chris McGee (50511117) | answered Nov 30 '12, 4:22 p.m.
When you create an advisor (precondition) on the server you can extend AbstractService and implement IOperationAdvisor.

In your method you are provided an AdvisableOperation object that you can use to getOperationData(). The operation data in this case can be basted to IScmDeltaSource.

Once you have the IScmDeltaSource it provides information such as getDeltas() with the changes that are being delivered. The getDeltas() method takes a file such as "IChangeHistoryModificationDelta.class," which will filter out anything other than deltas that are on the change history of the stream. Each change history modification delta has an update report (getUpdateReport()). That update report will provide a list of affected components (getAffectedComponents()). The individual updates for a component are retrieved using the update report's getUpdatesForComponent().

Once you have an IItemUpdateReport it represents a change to a versionable and provides the two states: getPreviousState(), getNewCurrentState(). Each of these returns an IVersionableHandle. You can test to see that the IVersionableHandle is an instanceof IFileItemHandle to make sure that this is a change to a file and not a symbolic link or directory.

With the IFileItemHandle you must fetch the complete item state so that you can find the content hash to get the contents of the file. Call getService(IScmItemService.class) to get the IScmItemService and you can fetch the complete item state using scmService.fetchState() and cast to IFileItem. The content hash comes from calling IFileItem.getContent().getHash().

With the IFileItem and the content hash you can use the IServerSideVersionedContentService to retrieve the byte stream of the contents of the file. Calling getService(IServerSideVersionedContentService.class) will retrieve the versioned content service. You can call versionedContentService.fetchContentWithReceiverTrusted(fileItem, contentHash, versionedContentReceiver) to retrieve the file contents. The versionedContentReceiver is your own class that will accept an input stream with the file contents.

Please note that the file contents could be encoded in a variety of ways (UTF-8, ISO8859-1, ...). You can find out the encoding of the file using fileItem.getContent().getCharacterEncoding(). If it is null then you can assume the default character encoding (java.nio.charset.Charset.defaultCharset()). With the encoding type string you can construct an InputStreamReader with the input stream and the character encoding so that you can read the characters in the stream and check the contents.

If you want your advisor to report the errors this can be done using the IAdvisorInfoCollector object provided to you in the run method to createProblemInfo() and createExceptionInfo() and collect add those to the collector using the addInfo().

I hope that this is helpful.

Chris McGee commented Dec 03 '12, 11:02 a.m.

There's a typo. "Basted" should be "casted."

Kevin Mayfield commented Feb 14 '13, 7:52 a.m.

Don't forget to add the IServerSideVersionedContentService to the prerequisites in plugin.xml

Your answer

Register or to post your answer.