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 v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * Kevin Sawicki (GitHub Inc.) - initial API and implementation
10 *****************************************************************************/
11 package org
.eclipse
.egit
.ui
.internal
.repository
.tree
.command
;
13 import java
.text
.MessageFormat
;
14 import java
.util
.ArrayList
;
15 import java
.util
.List
;
17 import java
.util
.Map
.Entry
;
19 import org
.eclipse
.core
.commands
.ExecutionEvent
;
20 import org
.eclipse
.core
.commands
.ExecutionException
;
21 import org
.eclipse
.core
.resources
.ResourcesPlugin
;
22 import org
.eclipse
.core
.resources
.WorkspaceJob
;
23 import org
.eclipse
.core
.runtime
.CoreException
;
24 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
25 import org
.eclipse
.core
.runtime
.IStatus
;
26 import org
.eclipse
.core
.runtime
.Status
;
27 import org
.eclipse
.core
.runtime
.SubMonitor
;
28 import org
.eclipse
.core
.runtime
.jobs
.Job
;
29 import org
.eclipse
.egit
.core
.op
.SubmoduleUpdateOperation
;
30 import org
.eclipse
.egit
.ui
.Activator
;
31 import org
.eclipse
.egit
.ui
.JobFamilies
;
32 import org
.eclipse
.egit
.ui
.internal
.UIRepositoryUtils
;
33 import org
.eclipse
.egit
.ui
.internal
.UIText
;
34 import org
.eclipse
.egit
.ui
.internal
.repository
.tree
.RepositoryTreeNode
;
35 import org
.eclipse
.egit
.ui
.internal
.repository
.tree
.RepositoryTreeNodeType
;
36 import org
.eclipse
.jgit
.lib
.Repository
;
37 import org
.eclipse
.jgit
.submodule
.SubmoduleWalk
;
38 import org
.eclipse
.swt
.widgets
.Shell
;
41 * Command to update selected submodules
43 public class SubmoduleUpdateCommand
extends
44 SubmoduleCommand
<RepositoryTreeNode
<?
>> {
47 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
48 final Map
<Repository
, List
<String
>> repoPaths
= getSubmodules(getSelectedNodes(event
));
49 if (!repoPaths
.isEmpty()) {
50 // Check for uncommitted changes in submodules.
52 boolean submodulesNodeSelected
= false;
53 List
<Repository
> subRepos
= new ArrayList
<>();
54 // If Submodules node is selected, check all submodules.
55 for (RepositoryTreeNode
<?
> node
: getSelectedNodes(event
)) {
56 if (node
.getType() == RepositoryTreeNodeType
.SUBMODULES
) {
57 submodulesNodeSelected
= true;
58 SubmoduleWalk walk
= SubmoduleWalk
59 .forIndex(node
.getRepository());
61 Repository subRepo
= walk
.getRepository();
62 if (subRepo
!= null) {
63 subRepos
.add(subRepo
);
69 // If Submodule node is not selected, check the selected
71 if (!submodulesNodeSelected
) {
72 for (Entry
<Repository
, List
<String
>> entry
: repoPaths
74 if (entry
.getValue() != null) {
75 for (String path
: entry
.getValue()) {
77 subRepo
= SubmoduleWalk
.getSubmoduleRepository(
78 entry
.getKey(), path
);
79 if (subRepo
!= null) {
80 subRepos
.add(subRepo
);
86 Shell parent
= getActiveShell(event
);
87 for (Repository subRepo
: subRepos
) {
88 String repoName
= Activator
.getDefault().getRepositoryUtil()
89 .getRepositoryName(subRepo
);
90 if (!UIRepositoryUtils
.handleUncommittedFiles(subRepo
,
93 UIText
.SubmoduleUpdateCommand_UncommittedChanges
,
98 } catch (Exception e
) {
99 Activator
.handleError(UIText
.SubmoduleUpdateCommand_UpdateError
,
104 Job job
= new WorkspaceJob(UIText
.SubmoduleUpdateCommand_Title
) {
107 public IStatus
runInWorkspace(IProgressMonitor monitor
) {
108 SubMonitor progress
= SubMonitor
.convert(monitor
,
111 for (Entry
<Repository
, List
<String
>> entry
: repoPaths
113 if (progress
.isCanceled()) {
114 return Status
.CANCEL_STATUS
;
116 SubmoduleUpdateOperation op
= new SubmoduleUpdateOperation(
118 if (entry
.getValue() != null) {
119 for (String path
: entry
.getValue()) {
123 op
.execute(progress
.newChild(1));
125 } catch (CoreException e
) {
127 UIText
.SubmoduleUpdateCommand_UpdateError
, e
);
129 return Status
.OK_STATUS
;
133 public boolean belongsTo(Object family
) {
134 if (JobFamilies
.SUBMODULE_UPDATE
.equals(family
))
136 return super.belongsTo(family
);
140 job
.setRule(ResourcesPlugin
.getWorkspace().getRoot());