It's all about the answers!

Ask a question

How to get Line of code changes


Titas Dutta (1514) | asked Jan 02 '18, 5:36 a.m.

I am able get change set details using below code:


List<ChangeImpl> changeSetList = ((IChangeSet) repo.itemManager().
        fetchCompleteItem((IChangeSetHandle)changeSetHandle, IItemManager.DEFAULT, null)).changes();
        IScmService scmService = (IScmService) ((IClientLibraryContext) repo).getServiceInterface(IScmService.class);
       
        for(ChangeImpl chSet : changeSetList){
        System.out.println("Change Set Details: "+chSet);
        IVersionableHandle after = chSet.afterState();
                IVersionable item = (IVersionable) scmService.fetchState(after, null, null);
                if (item instanceof IFileItem) {
                IFileItem fileItem = (IFileItem) item;
                String readContent = readFileContent(item, repo);
                System.out.println("READ CONTENT :: "+readContent);
                }    
        }

How to get number of line of code added or modified or deleted last in the repository? Is their any method to get it for a specific file?

Accepted answer


permanent link
Luca Martinucci (1.0k397112) | answered Jan 02 '18, 8:14 a.m.

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.


Titas Dutta selected this answer as the correct answer

Comments
Titas Dutta commented Jan 04 '18, 4:42 a.m.

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? 

Your answer


Register or 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.