Skip to content

Commit

Permalink
archi-modelrepository-plugin object-history
Browse files Browse the repository at this point in the history
  • Loading branch information
janesser committed May 19, 2024
1 parent 68f1773 commit b545137
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class RestoreCommitAction extends AbstractModelAction {

public RestoreCommitAction(IWorkbenchWindow window) {
super(window);
setImageDescriptor(IModelRepositoryImages.ImageFactory.getImageDescriptor(IModelRepositoryImages.ICON_SYNCED));
setImageDescriptor(IModelRepositoryImages.ImageFactory.getImageDescriptor(IModelRepositoryImages.ICON_RESET));
setText(Messages.RestoreCommitAction_0);
setToolTipText(Messages.RestoreCommitAction_0);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.archicontribs.modelrepository.grafico;

import java.io.File;

import org.eclipse.emf.ecore.EObject;

import com.archimatetool.model.FolderType;
import com.archimatetool.model.IFolder;
import com.archimatetool.model.IIdentifier;

public final class GraficoFileConventions {

private GraficoFileConventions() {
// utility class
}

public static File forFolder(File parentFolder, IFolder iFolder) {
return new File(parentFolder, GraficoFileConventions.getNameFor(iFolder));
}

public static File forElement(File modelFolder, IFolder iFolder, EObject elem) {
File elemFolder = new File(modelFolder, GraficoFileConventions.getNameFor(iFolder));
return GraficoFileConventions.forElement(elemFolder, elem);
}

public static File forElement(File elemFolder, EObject elem) {
return new File(elemFolder, elem.getClass().getSimpleName() + "_" + ((IIdentifier)elem).getId() + ".xml"); //$NON-NLS-1$ //$NON-NLS-2$
}

/**
* Generate a proper name for directory creation
*
* @param folder
* @return
*/
public static String getNameFor(IFolder folder) {
return folder.getType() == FolderType.USER ? folder.getId().toString() : folder.getType().toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,10 @@

import com.archimatetool.editor.model.IArchiveManager;
import com.archimatetool.editor.utils.FileUtils;
import com.archimatetool.model.FolderType;
import com.archimatetool.model.IArchimateModel;
import com.archimatetool.model.IDiagramModelImageProvider;
import com.archimatetool.model.IFolder;
import com.archimatetool.model.IFolderContainer;
import com.archimatetool.model.IIdentifier;


/**
Expand Down Expand Up @@ -177,7 +175,7 @@ private void createAndSaveResourceForFolder(IFolderContainer folderContainer, Fi
allFolders.addAll(folderContainer.getFolders());

for(IFolder tmpFolder : allFolders) {
File tmpFolderFile = new File(folder, getNameFor(tmpFolder));
File tmpFolderFile = GraficoFileConventions.forFolder(folder, tmpFolder);
tmpFolderFile.mkdirs();
createAndSaveResource(new File(tmpFolderFile, IGraficoConstants.FOLDER_XML), tmpFolder);
createAndSaveResourceForFolder(tmpFolder, tmpFolderFile);
Expand All @@ -190,25 +188,15 @@ private void createAndSaveResourceForFolder(IFolderContainer folderContainer, Fi
allElements.addAll(((IFolder)folderContainer).getElements());
for(EObject tmpElement : allElements) {
createAndSaveResource(
new File(folder, tmpElement.getClass().getSimpleName() + "_" + ((IIdentifier)tmpElement).getId() + ".xml"), //$NON-NLS-1$ //$NON-NLS-2$
GraficoFileConventions.forElement(folder, tmpElement),
tmpElement);
}
}
if(folderContainer instanceof IArchimateModel) {
createAndSaveResource(new File(folder, IGraficoConstants.FOLDER_XML), folderContainer);
}
}

/**
* Generate a proper name for directory creation
*
* @param folder
* @return
*/
private String getNameFor(IFolder folder) {
return folder.getType() == FolderType.USER ? folder.getId().toString() : folder.getType().toString();
}


/**
* Save the model to Resource
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,5 @@ public static void writeObjectToFileWithLineEnding(File file, ObjectLoader loade
str = str.replaceAll("\\r?\\n", lineEnding); //$NON-NLS-1$
Files.write(Paths.get(file.getAbsolutePath()), str.getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
*/
package org.archicontribs.modelrepository.views.history;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
Expand All @@ -14,6 +16,7 @@
import org.archicontribs.modelrepository.IModelRepositoryImages;
import org.archicontribs.modelrepository.grafico.BranchInfo;
import org.archicontribs.modelrepository.grafico.BranchStatus;
import org.archicontribs.modelrepository.grafico.GraficoFileConventions;
import org.archicontribs.modelrepository.grafico.IArchiRepository;
import org.eclipse.jface.layout.TableColumnLayout;
import org.eclipse.jface.viewers.CellLabelProvider;
Expand All @@ -30,12 +33,15 @@
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.treewalk.filter.PathFilter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;

import com.archimatetool.editor.ui.UIUtils;
import com.archimatetool.model.IArchimateModelObject;
import com.archimatetool.model.IFolder;


/**
Expand Down Expand Up @@ -119,6 +125,15 @@ public void doSetInput(IArchiRepository archiRepo) {
// setSelection(new StructuredSelection(element), true);
//}
}

private static record HistoryInput(IArchiRepository repo, IArchimateModelObject obj) {}

public void doSetInput(IArchiRepository selectedRepository, Object selected) {
if (selected instanceof IArchimateModelObject)
setInput(new HistoryInput(selectedRepository, (IArchimateModelObject)selected));
else
doSetInput(selectedRepository);
}

public void setSelectedBranch(BranchInfo branchInfo) {
if(branchInfo != null && branchInfo.equals(fSelectedBranch)) {
Expand Down Expand Up @@ -155,27 +170,47 @@ List<RevCommit> getCommits(Object parent) {
fLocalCommit = null;
fOriginCommit = null;

if(!(parent instanceof IArchiRepository) || fSelectedBranch == null) {
IArchiRepository repo = null;
File elemFile = null;

if (parent instanceof HistoryInput && ((HistoryInput) parent).obj().eContainer() instanceof IFolder) {
HistoryInput input = (HistoryInput) parent;
repo = input.repo();
IFolder folder = (IFolder) input.obj().eContainer();
elemFile = GraficoFileConventions.forElement(
new File(repo.getLocalRepositoryFolder(), "model"),
folder,
input.obj());
} else if (parent instanceof IArchiRepository)
repo = (IArchiRepository) parent;

if(fSelectedBranch == null) {
return commits;
}

IArchiRepository repo = (IArchiRepository)parent;

// Local Repo was deleted
if(!repo.getLocalRepositoryFolder().exists()) {
if(repo == null || !repo.getLocalRepositoryFolder().exists()) {
return commits;
}

try(Repository repository = Git.open(repo.getLocalRepositoryFolder()).getRepository()) {
// a RevWalk allows to walk over commits based on some filtering that is defined
try(RevWalk revWalk = new RevWalk(repository)) {
if (elemFile != null) {
String elemPath = Path.of(repo.getLocalRepositoryFolder().getAbsolutePath())
.relativize(
Path.of(elemFile.getAbsolutePath())
).toString();
revWalk.setTreeFilter(PathFilter.create(elemPath));
}

// Find the local branch
ObjectId objectID = repository.resolve(fSelectedBranch.getLocalBranchNameFor());
if(objectID != null) {
fLocalCommit = revWalk.parseCommit(objectID);
revWalk.markStart(fLocalCommit);
}

// Find the remote branch
objectID = repository.resolve(fSelectedBranch.getRemoteBranchNameFor());
if(objectID != null) {
Expand Down
Loading

0 comments on commit b545137

Please sign in to comment.