1 /*******************************************************************************
2 * Copyright (C) 2010, Jens Baumgart <jens.baumgart@sap.com>
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *******************************************************************************/
9 package org
.eclipse
.egit
.core
.op
;
12 import java
.io
.IOException
;
13 import java
.util
.ArrayList
;
14 import java
.util
.Collection
;
15 import java
.util
.IdentityHashMap
;
17 import org
.eclipse
.core
.resources
.IFile
;
18 import org
.eclipse
.core
.resources
.IProject
;
19 import org
.eclipse
.core
.resources
.IResource
;
20 import org
.eclipse
.core
.runtime
.CoreException
;
21 import org
.eclipse
.core
.runtime
.IAdaptable
;
22 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
23 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
24 import org
.eclipse
.core
.runtime
.jobs
.ISchedulingRule
;
25 import org
.eclipse
.core
.runtime
.jobs
.MultiRule
;
26 import org
.eclipse
.egit
.core
.Activator
;
27 import org
.eclipse
.egit
.core
.CoreText
;
28 import org
.eclipse
.egit
.core
.project
.RepositoryMapping
;
29 import org
.eclipse
.jgit
.lib
.GitIndex
;
30 import org
.eclipse
.jgit
.lib
.Repository
;
31 import org
.eclipse
.jgit
.lib
.GitIndex
.Entry
;
35 public class AddToIndexOperation
implements IEGitOperation
{
36 private final Collection
<IResource
> rsrcList
;
37 private final Collection
<IFile
> notAddedFiles
;
39 private final IdentityHashMap
<RepositoryMapping
, Object
> mappings
;
42 * Create a new operation to add files to the Git index
45 * collection of {@link IResource}s which should be added to the
46 * relevant Git repositories.
48 public AddToIndexOperation(final Collection
<IResource
> rsrcs
) {
50 mappings
= new IdentityHashMap
<RepositoryMapping
, Object
>();
51 notAddedFiles
= new ArrayList
<IFile
>();
55 * @see org.eclipse.egit.core.op.IEGitOperation#execute(org.eclipse.core.runtime.IProgressMonitor)
57 public void execute(IProgressMonitor m
) throws CoreException
{
58 IProgressMonitor monitor
;
60 monitor
= new NullProgressMonitor();
63 Collection
<GitIndex
> changedIndexes
= new ArrayList
<GitIndex
>();
64 // GitIndex can not be updated if it contains staged entries
65 Collection
<GitIndex
> indexesWithStagedEntries
= new ArrayList
<GitIndex
>();
67 for (Object obj
: rsrcList
) {
68 obj
= ((IAdaptable
) obj
).getAdapter(IResource
.class);
69 if (obj
instanceof IFile
)
70 addToIndex((IFile
) obj
, changedIndexes
,
71 indexesWithStagedEntries
);
74 if (!changedIndexes
.isEmpty()) {
75 for (GitIndex idx
: changedIndexes
) {
80 } catch (RuntimeException e
) {
81 throw new CoreException(Activator
.error(CoreText
.AddToIndexOperation_failed
, e
));
82 } catch (IOException e
) {
83 throw new CoreException(Activator
.error(CoreText
.AddToIndexOperation_failed
, e
));
85 for (final RepositoryMapping rm
: mappings
.keySet())
86 rm
.fireRepositoryChanged();
93 * @see org.eclipse.egit.core.op.IEGitOperation#getSchedulingRule()
95 public ISchedulingRule
getSchedulingRule() {
96 return new MultiRule(rsrcList
.toArray(new IResource
[rsrcList
.size()]));
100 * @return returns the files that could not be added to the index
101 * because there are unmerged entries
103 public Collection
<IFile
> getNotAddedFiles() {
104 return notAddedFiles
;
107 private void addToIndex(IFile file
,
108 Collection
<GitIndex
> changedIndexes
,
109 Collection
<GitIndex
> indexesWithUnmergedEntries
) throws IOException
{
110 IProject project
= file
.getProject();
111 RepositoryMapping map
= RepositoryMapping
.getMapping(project
);
112 Repository repo
= map
.getRepository();
113 GitIndex index
= null;
114 index
= repo
.getIndex();
115 Entry entry
= index
.getEntry(map
.getRepoRelativePath(file
));
118 if (indexesWithUnmergedEntries
.contains(index
)) {
119 notAddedFiles
.add(file
);
122 if (!canUpdateIndex(index
)) {
123 indexesWithUnmergedEntries
.add(index
);
124 notAddedFiles
.add(file
);
128 if (entry
.isModified(map
.getWorkDir())) {
129 entry
.update(new File(map
.getWorkDir(), entry
.getName()));
130 if (!changedIndexes
.contains(index
))
131 changedIndexes
.add(index
);
136 * The method checks if the given index can be updated. The index can be
137 * updated if it does not contain entries with stage !=0.
140 * @return true if the given index can be updated
142 private static boolean canUpdateIndex(GitIndex index
) {
143 Entry
[] members
= index
.getMembers();
144 for (int i
= 0; i
< members
.length
; i
++) {
145 if (members
[i
].getStage() != 0)