Search in sources :

Example 6 with GraficoModelLoader

use of org.archicontribs.modelrepository.grafico.GraficoModelLoader in project archi-modelrepository-plugin by archi-contribs.

the class RestoreCommitAction method run.

@Override
public void run() {
    // Offer to save the model if open and dirty
    IArchimateModel model = getRepository().locateModel();
    if (model != null && IEditorModelManager.INSTANCE.isModelDirty(model)) {
        if (!offerToSaveModel(model)) {
            return;
        }
    }
    boolean response = MessageDialog.openConfirm(fWindow.getShell(), Messages.RestoreCommitAction_0, Messages.RestoreCommitAction_1);
    if (!response) {
        return;
    }
    // Delete the content folders first
    try {
        File modelFolder = new File(getRepository().getLocalRepositoryFolder(), IGraficoConstants.MODEL_FOLDER);
        FileUtils.deleteFolder(modelFolder);
        modelFolder.mkdirs();
        File imagesFolder = new File(getRepository().getLocalRepositoryFolder(), IGraficoConstants.IMAGES_FOLDER);
        FileUtils.deleteFolder(imagesFolder);
        imagesFolder.mkdirs();
    } catch (IOException ex) {
        displayErrorDialog(Messages.RestoreCommitAction_0, ex);
        return;
    }
    // Walk the tree and get the contents of the commit
    try (Repository repository = Git.open(getRepository().getLocalRepositoryFolder()).getRepository()) {
        try (TreeWalk treeWalk = new TreeWalk(repository)) {
            treeWalk.addTree(fCommit.getTree());
            treeWalk.setRecursive(true);
            while (treeWalk.next()) {
                ObjectId objectId = treeWalk.getObjectId(0);
                ObjectLoader loader = repository.open(objectId);
                File file = new File(getRepository().getLocalRepositoryFolder(), treeWalk.getPathString());
                file.getParentFile().mkdirs();
                GraficoUtils.writeObjectToFileWithSystemLineEndings(file, loader);
            }
        }
    } catch (IOException ex) {
        displayErrorDialog(Messages.RestoreCommitAction_0, ex);
        return;
    }
    // Reload the model from the Grafico XML files
    try {
        IArchimateModel graficoModel = new GraficoModelLoader(getRepository()).loadModel();
        // If this is null then it failed because of no model in this commit
        if (graficoModel == null) {
            // Reset
            getRepository().resetToRef(IGraficoConstants.REFS_HEADS_MASTER);
            MessageDialog.openError(fWindow.getShell(), Messages.RestoreCommitAction_0, Messages.RestoreCommitAction_2);
            return;
        }
    } catch (IOException | GitAPIException ex) {
        displayErrorDialog(Messages.RestoreCommitAction_0, ex);
    }
    // Commit changes
    try {
        // $NON-NLS-1$ //$NON-NLS-2$
        getRepository().commitChanges(Messages.RestoreCommitAction_3 + " '" + fCommit.getShortMessage() + "'", false);
        // Save the checksum
        getRepository().saveChecksum();
    } catch (GitAPIException | IOException ex) {
        displayErrorDialog(Messages.RestoreCommitAction_0, ex);
    }
    notifyChangeListeners(IRepositoryListener.HISTORY_CHANGED);
}
Also used : GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) Repository(org.eclipse.jgit.lib.Repository) GraficoModelLoader(org.archicontribs.modelrepository.grafico.GraficoModelLoader) ObjectId(org.eclipse.jgit.lib.ObjectId) ObjectLoader(org.eclipse.jgit.lib.ObjectLoader) IOException(java.io.IOException) IArchimateModel(com.archimatetool.model.IArchimateModel) File(java.io.File) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk)

Aggregations

IOException (java.io.IOException)6 GraficoModelLoader (org.archicontribs.modelrepository.grafico.GraficoModelLoader)6 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)6 IArchimateModel (com.archimatetool.model.IArchimateModel)5 File (java.io.File)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 SimpleCredentialsStorage (org.archicontribs.modelrepository.authentication.SimpleCredentialsStorage)1 CloneInputDialog (org.archicontribs.modelrepository.dialogs.CloneInputDialog)1 ArchiRepository (org.archicontribs.modelrepository.grafico.ArchiRepository)1 MergeConflictHandler (org.archicontribs.modelrepository.grafico.MergeConflictHandler)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 ProgressMonitorDialog (org.eclipse.jface.dialogs.ProgressMonitorDialog)1 Git (org.eclipse.jgit.api.Git)1 MergeResult (org.eclipse.jgit.api.MergeResult)1 RevertCommand (org.eclipse.jgit.api.RevertCommand)1 ObjectId (org.eclipse.jgit.lib.ObjectId)1 ObjectLoader (org.eclipse.jgit.lib.ObjectLoader)1 Repository (org.eclipse.jgit.lib.Repository)1 TreeWalk (org.eclipse.jgit.treewalk.TreeWalk)1