refactor: simplify collection.toArray()
[egit/eclipse.git] / org.eclipse.egit.core / src / org / eclipse / egit / core / op / SubmoduleSyncOperation.java
blob9f2e12352f8b5c027f478912edd067a489e3b480
1 /******************************************************************************
2 * Copyright (c) 2012 GitHub Inc.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License 2.0
5 * which accompanies this distribution, and is available at
6 * https://www.eclipse.org/legal/epl-2.0/
8 * SPDX-License-Identifier: EPL-2.0
10 * Contributors:
11 * Kevin Sawicki (GitHub Inc.) - initial API and implementation
12 *****************************************************************************/
13 package org.eclipse.egit.core.op;
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.Map;
19 import org.eclipse.core.resources.IWorkspace;
20 import org.eclipse.core.resources.IWorkspaceRunnable;
21 import org.eclipse.core.resources.ResourcesPlugin;
22 import org.eclipse.core.runtime.CoreException;
23 import org.eclipse.core.runtime.IProgressMonitor;
24 import org.eclipse.core.runtime.jobs.ISchedulingRule;
25 import org.eclipse.jgit.api.Git;
26 import org.eclipse.jgit.api.SubmoduleSyncCommand;
27 import org.eclipse.jgit.api.errors.GitAPIException;
28 import org.eclipse.jgit.lib.Repository;
29 import org.eclipse.team.core.TeamException;
31 /**
32 * Operation that syncs a repository's submodule configurations
34 public class SubmoduleSyncOperation implements IEGitOperation {
36 private final Repository repository;
38 private final Collection<String> paths;
40 /**
41 * Create submodule sync operation
43 * @param repository
45 public SubmoduleSyncOperation(final Repository repository) {
46 this.repository = repository;
47 paths = new ArrayList<>();
50 /**
51 * Add path of submodule to update
53 * @param path
54 * @return this operation
56 public SubmoduleSyncOperation addPath(final String path) {
57 paths.add(path);
58 return this;
61 @Override
62 public void execute(final IProgressMonitor monitor) throws CoreException {
63 IWorkspaceRunnable action = new IWorkspaceRunnable() {
65 @Override
66 public void run(IProgressMonitor pm) throws CoreException {
67 Map<String, String> updates = null;
68 try {
69 SubmoduleSyncCommand sync = Git.wrap(repository)
70 .submoduleSync();
71 for (String path : paths) {
72 sync.addPath(path);
74 updates = sync.call();
75 } catch (GitAPIException e) {
76 throw new TeamException(e.getLocalizedMessage(),
77 e.getCause());
78 } finally {
79 if (updates != null && !updates.isEmpty()) {
80 repository.notifyIndexChanged(true);
85 ResourcesPlugin.getWorkspace().run(action, getSchedulingRule(),
86 IWorkspace.AVOID_UPDATE, monitor);
89 @Override
90 public ISchedulingRule getSchedulingRule() {
91 return null;