refactor: simplify collection.toArray()
[egit/eclipse.git] / org.eclipse.egit.core / src / org / eclipse / egit / core / op / SubmoduleUpdateOperation.java
blob77bb15ee0b3248bf86edb5e81ed08b4d00c45a11
1 /******************************************************************************
2 * Copyright (c) 2012, 2015 GitHub Inc and others.
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 * Laurent Delaigue (Obeo) - use of preferred merge strategy
13 * Stephan Hackstedt <stephan.hackstedt@googlemail.com - bug 477695
14 *****************************************************************************/
15 package org.eclipse.egit.core.op;
17 import java.io.IOException;
18 import java.text.MessageFormat;
19 import java.util.ArrayList;
20 import java.util.Collection;
22 import org.eclipse.core.resources.IWorkspace;
23 import org.eclipse.core.resources.IWorkspaceRunnable;
24 import org.eclipse.core.resources.ResourcesPlugin;
25 import org.eclipse.core.runtime.CoreException;
26 import org.eclipse.core.runtime.IProgressMonitor;
27 import org.eclipse.core.runtime.SubMonitor;
28 import org.eclipse.core.runtime.jobs.ISchedulingRule;
29 import org.eclipse.egit.core.Activator;
30 import org.eclipse.egit.core.EclipseGitProgressTransformer;
31 import org.eclipse.egit.core.RepositoryUtil;
32 import org.eclipse.egit.core.internal.CoreText;
33 import org.eclipse.egit.core.internal.util.ProjectUtil;
34 import org.eclipse.jgit.api.CloneCommand;
35 import org.eclipse.jgit.api.Git;
36 import org.eclipse.jgit.api.SubmoduleInitCommand;
37 import org.eclipse.jgit.api.SubmoduleUpdateCommand;
38 import org.eclipse.jgit.api.errors.GitAPIException;
39 import org.eclipse.jgit.lib.AnyObjectId;
40 import org.eclipse.jgit.lib.Repository;
41 import org.eclipse.jgit.merge.MergeStrategy;
42 import org.eclipse.jgit.submodule.SubmoduleWalk;
43 import org.eclipse.team.core.TeamException;
45 /**
46 * Operation that updates a repository's submodules
48 public class SubmoduleUpdateOperation implements IEGitOperation {
50 private final Repository repository;
52 private final Collection<String> paths;
54 /**
55 * Create submodule update operation
57 * @param repository
59 public SubmoduleUpdateOperation(final Repository repository) {
60 this.repository = repository;
61 paths = new ArrayList<>();
64 /**
65 * Add path of submodule to update
67 * @param path
68 * @return this operation
70 public SubmoduleUpdateOperation addPath(final String path) {
71 paths.add(path);
72 return this;
75 @Override
76 public void execute(final IProgressMonitor monitor) throws CoreException {
77 IWorkspaceRunnable action = new IWorkspaceRunnable() {
79 @Override
80 public void run(IProgressMonitor pm) throws CoreException {
81 RepositoryUtil util = Activator.getDefault()
82 .getRepositoryUtil();
83 SubMonitor progress = SubMonitor.convert(pm, 4);
84 progress.setTaskName(MessageFormat.format(
85 CoreText.SubmoduleUpdateOperation_updating,
86 util.getRepositoryName(repository)));
88 Git git = Git.wrap(repository);
90 Collection<String> updated = null;
91 try {
92 SubmoduleInitCommand init = git.submoduleInit();
93 for (String path : paths)
94 init.addPath(path);
95 init.call();
96 progress.worked(1);
98 SubmoduleUpdateCommand update = git.submoduleUpdate();
99 for (String path : paths)
100 update.addPath(path);
101 update.setProgressMonitor(new EclipseGitProgressTransformer(
102 progress.newChild(2)));
103 MergeStrategy strategy = Activator.getDefault()
104 .getPreferredMergeStrategy();
105 if (strategy != null) {
106 update.setStrategy(strategy);
108 update.setCallback(new CloneCommand.Callback() {
110 @Override
111 public void initializedSubmodules(
112 Collection<String> submodules) {
113 // Nothing to do
116 @Override
117 public void cloningSubmodule(String path) {
118 progress.setTaskName(MessageFormat.format(
119 CoreText.SubmoduleUpdateOperation_cloning,
120 util.getRepositoryName(repository), path));
123 @Override
124 public void checkingOut(AnyObjectId commit,
125 String path) {
126 // Nothing to do
129 updated = update.call();
130 SubMonitor refreshMonitor = progress.newChild(1)
131 .setWorkRemaining(updated.size());
132 for (String path : updated) {
133 Repository subRepo = SubmoduleWalk
134 .getSubmoduleRepository(repository, path);
135 if (subRepo != null) {
136 ProjectUtil.refreshValidProjects(
137 ProjectUtil.getValidOpenProjects(subRepo),
138 refreshMonitor.newChild(1));
139 } else {
140 refreshMonitor.worked(1);
143 } catch (GitAPIException e) {
144 throw new TeamException(e.getLocalizedMessage(),
145 e.getCause());
146 } catch (IOException e) {
147 throw new TeamException(e.getLocalizedMessage(),
148 e.getCause());
149 } finally {
150 if (updated != null && !updated.isEmpty()) {
151 repository.notifyIndexChanged(true);
156 ResourcesPlugin.getWorkspace().run(action, getSchedulingRule(),
157 IWorkspace.AVOID_UPDATE, monitor);
160 @Override
161 public ISchedulingRule getSchedulingRule() {
162 return ResourcesPlugin.getWorkspace().getRoot();