How to get Line of code changes
I am able get change set details using below code:
Accepted answer
Titas,
I have done something similar using RTC plain Java API.
Suppose that you have a IChangeSet object cs, you can retrieve the IFileItem objects for the previous and current versions of each file modified in the change set:
for (Object o: cs.changes()) {
IChange change = (IChange) o;
if (change.kind() == IChange.MODIFY) {
IVersionableHandle afterHandle = change.afterState();
IVersionableHandle beforeHandle = change.beforeState();
//skip folders
if( !beforeHandle.getItemType().getName().equals("Folder") && !afterHandle.getItemType().getName().equals("Folder")){
IVersionable afterVersionable = wm.versionableManager().fetchCompleteState(afterHandle, new SysoutProgressMonitor());
IFileItem fileAfter = (IFileItem) afterVersionable;
//skip metadata
if(!fileAfter.getName().substring(0,3).equals("org") && !fileAfter.getName().equals(".project")){
IVersionable beforeVersionable = wm.versionableManager().fetchCompleteState(beforeHandle, new SysoutProgressMonitor());
IFileItem fileBefore = (IFileItem) beforeVersionable;
//....
}
}
}
}
Then, you write on disk the "before" and the "after" version of each file:
//write before and after versions on disk
// ITeamRepository repo;
IFileContentManager contentManager = FileSystemCore.getContentManager(repo);
PrintWriter beforeOnDisk = new PrintWriter(outputDir+"before", "UTF-8");
InputStream isBefore = contentManager.retrieveContentStream(fileBefore, fileBefore.getContent(), null);
BufferedReader brBefore = new BufferedReader(new InputStreamReader(isBefore));
String lineBefore;
while ((lineBefore = brBefore.readLine()) != null) {
beforeOnDisk.println(lineBefore);
}
beforeOnDisk.close();
File beforef = new File(outputDir+"before");
PrintWriter afterOnDisk = new PrintWriter(outputDir+"after", "UTF-8");
String lineAfter;
InputStream isAfter = contentManager.retrieveContentStream(fileAfter, fileAfter.getContent(), null);
BufferedReader brAfter = new BufferedReader(new InputStreamReader(isAfter));
while ((lineAfter = brAfter.readLine()) != null) {
afterOnDisk.println(lineAfter);
}
afterOnDisk.close();
File afterM = new File(outputDir+"after");
Now, it is up to you to decide which tool you use to compare the "before" and "after" versions and get the number of added/deleted/modified lines for each file.
Comments
Thanks Luca..It's worked but if I have 100 files committed or more than that then the process would take more time or get slow down for finding number of line committed(added,modified,removed). Is there any other way like method calls which returns the history of a file? Or through OSLC / REST API call can we get the same?