4

How to get path of file which isn't in newest version but is a part of previous changelist in RTC scm.

All I could achieve so far is this:

IFileItemHandle fileItemHandle = (IFileItemHandle) IFileItem.ITEM_TYPE.createItemHandle(change.afterState().getItemId(), change.afterState().getStateId());
file = versionableManager.fetchCompleteState(fileItemHandle, monitor);

if (file instanceof IFolder) {         
    IFolder folder = (IFolder) file;         
    relativePath = getFilePath(file, workspaceConnection.configuration(changeSet.getComponent()), monitor);
    fileName = folder.getName();      
} else {
    relativePath = getFilePath(file, workspaceConnection.configuration(changeSet.getComponent()), monitor);    
    fileName = ((FileItem) file).getName();     
}

Where getFilePath is:

private String getFilePath(IVersionableHandle folder, IConfiguration config, IProgressMonitor monitor, Boolean searchInHistory) throws TeamRepositoryException {
    List lst = new ArrayList<IVersionableHandle>(), ancestors;
    lst.add(folder);
    if (searchInHistory) {
        ancestors = config.determineAncestorsInHistory(lst, monitor);
    } else {
        ancestors = config.locateAncestors(lst, monitor);
    }

    return getFullPath(ancestors);
}

private String getFullPath(List ancestor) throws TeamRepositoryException {
    String directoryPath = "";
    for (Object ancestorObj : ancestor) {
        IAncestorReport ancestorImpl = (IAncestorReport) ancestorObj;
        for (Object nameItemPairObj : ancestorImpl.getNameItemPairs()) {
            INameItemPair nameItemPair = (INameItemPair) nameItemPairObj;
            String pathName = nameItemPair.getName();
            if (pathName != null && !pathName.equals("")) {
                directoryPath = directoryPath + "\\" + pathName;
            }
        }
    }
    return directoryPath;
}

Unfortunately it doesn't work perfectly. If filename is changed in following changelists like on this example:

Changelist 1:     
add file: src/newFile.java

Changelist 2:     
modify file: src/newFile.java

Changelist 3:     
rename file: src/newFile.java -> src/newFile_rename.java

The relative path resolved in first changelist would be:

src/newFile_rename.java

instead of

src/newFile.java

How to make it works good?

Gayu
  • 487
  • 5
  • 21
J33nn
  • 3,034
  • 5
  • 30
  • 46

1 Answers1

1

While the javadoc for IConfiguration.determineAncestorsInHistory doesn't specify, the server side equivalent IScmService.configurationDetermineAncestorsInHistory (which is what ends up being called) says this in the javadoc:

 * @param versionableItemHandles
 *            a list of versionable items; only the item ids are needed;
 *            must not be <code>null</code>

Basically, the determineAncestorsInHistory is not looking at the state id on the file handles, it only looks at the item id.

The particular state of the file that will be considered is determined by the configuration. This is mostly because while the state of the file in the changeset will tell you the name of that file, it does not tell you the name of the parent folder. That will depend on the state of the folder that is present in the workspace at a particular time and could be different for different workspaces

You basically need to get an IConfiguration that represents your workspace at the time the change set was accepted/created.

The way I see to do this is to get the IWorkspaceConnection.changeHistory(component) (this is actually defined on IFlowNodeConnection). You will need to walk back through the histories by calling IChangeHistory.previousHistory(monitor) until you find one that contains your changeset in IChangeHistory.recent(monitor). Once you find the appropriate IChangeHistory use IChangeHistory.configuration() for the determineAncestorsInHistory call.

Note that this configuration represents the state of the workspace at the end of that particular IChangeHistory, so to be completely accurate you would need to inspect the changesets that occur after your change in the IChangeHistory.recent to see if any of them modified your file name (or also any of the containing folders' file names).

(Another alternative is to use the history configuration 1 previous to the history that contains your changeset, and then look at the effects of the changes that happen before your change)

Andrew Niefer
  • 4,279
  • 2
  • 19
  • 22