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

How to get Line of code changes

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?

0 votes


Accepted answer

Permanent link

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

1 vote

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? 

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,938

Question asked: Jan 02 '18, 5:36 a.m.

Question was seen: 1,625 times

Last updated: Jan 04 '18, 4:42 a.m.

Confirmation Cancel Confirm