1 /*******************************************************************************
2 * Copyright (C) 2010, Jens Baumgart <jens.baumgart@sap.com>
3 * Copyright (C) 2010, Stefan Lay <stefan.lay@sap.com>
5 * All rights reserved. This program and the accompanying materials
6 * are made available under the terms of the Eclipse Public License 2.0
7 * which accompanies this distribution, and is available at
8 * https://www.eclipse.org/legal/epl-2.0/
10 * SPDX-License-Identifier: EPL-2.0
11 *******************************************************************************/
12 package org
.eclipse
.egit
.core
.op
;
14 import java
.util
.Arrays
;
15 import java
.util
.Collection
;
16 import java
.util
.HashMap
;
19 import org
.eclipse
.core
.resources
.IResource
;
20 import org
.eclipse
.core
.runtime
.CoreException
;
21 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
22 import org
.eclipse
.core
.runtime
.SubMonitor
;
23 import org
.eclipse
.core
.runtime
.jobs
.ISchedulingRule
;
24 import org
.eclipse
.egit
.core
.Activator
;
25 import org
.eclipse
.egit
.core
.internal
.CoreText
;
26 import org
.eclipse
.egit
.core
.internal
.job
.RuleUtil
;
27 import org
.eclipse
.egit
.core
.project
.RepositoryMapping
;
28 import org
.eclipse
.jgit
.api
.AddCommand
;
29 import org
.eclipse
.jgit
.api
.Git
;
30 import org
.eclipse
.jgit
.api
.errors
.GitAPIException
;
31 import org
.eclipse
.jgit
.lib
.Repository
;
35 public class AddToIndexOperation
implements IEGitOperation
{
36 private final Collection
<?
extends IResource
> rsrcList
;
39 * Create a new operation to add files to the Git index
42 * collection of {@link IResource}s which should be added to the
43 * relevant Git repositories.
45 public AddToIndexOperation(final Collection
<?
extends IResource
> rsrcs
) {
50 * Create a new operation to add files to the Git index
53 * array of {@link IResource}s which should be added to the
54 * relevant Git repositories.
56 public AddToIndexOperation(IResource
[] resources
) {
57 rsrcList
= Arrays
.asList(resources
);
61 * @see org.eclipse.egit.core.op.IEGitOperation#execute(org.eclipse.core.runtime.IProgressMonitor)
64 public void execute(IProgressMonitor monitor
) throws CoreException
{
65 SubMonitor progress
= SubMonitor
.convert(monitor
, rsrcList
.size() * 2);
67 Map
<RepositoryMapping
, AddCommand
> addCommands
= new HashMap
<RepositoryMapping
, AddCommand
>();
69 for (IResource obj
: rsrcList
) {
70 addToCommand(obj
, addCommands
);
74 progress
.setWorkRemaining(addCommands
.size());
75 for (AddCommand command
: addCommands
.values()) {
79 } catch (RuntimeException e
) {
80 throw new CoreException(Activator
.error(CoreText
.AddToIndexOperation_failed
, e
));
81 } catch (GitAPIException e
) {
82 throw new CoreException(Activator
.error(CoreText
.AddToIndexOperation_failed
, e
));
87 * @see org.eclipse.egit.core.op.IEGitOperation#getSchedulingRule()
90 public ISchedulingRule
getSchedulingRule() {
91 return RuleUtil
.getRuleForRepositories(rsrcList
.toArray(new IResource
[0]));
94 private void addToCommand(IResource resource
, Map
<RepositoryMapping
, AddCommand
> addCommands
) {
95 RepositoryMapping map
= RepositoryMapping
.getMapping(resource
);
99 AddCommand command
= addCommands
.get(map
);
100 if (command
== null) {
101 Repository repo
= map
.getRepository();
102 try (Git git
= new Git(repo
)) {
105 addCommands
.put(map
, command
);
107 String filepattern
= map
.getRepoRelativePath(resource
);
108 if ("".equals(filepattern
)) //$NON-NLS-1$
109 filepattern
= "."; //$NON-NLS-1$
110 command
.addFilepattern(filepattern
);