Avoid refresh on up-to-date pull operation
[egit/eclipse.git] / org.eclipse.egit.core / src / org / eclipse / egit / core / op / AddToIndexOperation.java
blob6c8451a5d3aa18abbcc44160b8047a63a4db5bd8
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;
17 import java.util.Map;
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;
33 /**
35 public class AddToIndexOperation implements IEGitOperation {
36 private final Collection<? extends IResource> rsrcList;
38 /**
39 * Create a new operation to add files to the Git index
41 * @param rsrcs
42 * collection of {@link IResource}s which should be added to the
43 * relevant Git repositories.
45 public AddToIndexOperation(final Collection<? extends IResource> rsrcs) {
46 rsrcList = rsrcs;
49 /**
50 * Create a new operation to add files to the Git index
52 * @param resources
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);
60 /* (non-Javadoc)
61 * @see org.eclipse.egit.core.op.IEGitOperation#execute(org.eclipse.core.runtime.IProgressMonitor)
63 @Override
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>();
68 try {
69 for (IResource obj : rsrcList) {
70 addToCommand(obj, addCommands);
71 progress.worked(1);
74 progress.setWorkRemaining(addCommands.size());
75 for (AddCommand command : addCommands.values()) {
76 command.call();
77 progress.worked(1);
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));
86 /* (non-Javadoc)
87 * @see org.eclipse.egit.core.op.IEGitOperation#getSchedulingRule()
89 @Override
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);
96 if (map == null) {
97 return;
99 AddCommand command = addCommands.get(map);
100 if (command == null) {
101 Repository repo = map.getRepository();
102 try (Git git = new Git(repo)) {
103 command = git.add();
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);