Jazz Forum Welcome to the Jazz Community Forum Connect and collaborate with IBM Engineering experts and users

How to read a changed file content in changeset

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.

Thanks!!

0 votes

Comments

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
When you create an advisor (precondition) on the server you can extend AbstractService and implement IOperationAdvisor.

In your IOperationAdvisor.run() 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.

1 vote

Comments

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

Don't forget to add the IServerSideVersionedContentService to the prerequisites in plugin.xml
https://jazz.net/forum/questions/102417/abstractscmadvisor-how-do-you-read-changed-file-content/102578

Your answer

Register or log in to post your answer.

Dashboards and work items are no longer publicly available, so some links may be invalid. We now provide similar information through other means. Learn more here.

Search context
Follow this question

By Email: 

Once you sign in you will be able to subscribe for any updates here.

By RSS:

Answers
Answers and Comments
Question details
× 10,940

Question asked: Nov 27 '12, 11:02 a.m.

Question was seen: 5,517 times

Last updated: Feb 14 '13, 7:52 a.m.

Confirmation Cancel Confirm