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
.egit
.core
.Activator
;
25 import org
.eclipse
.egit
.core
.CoreText
;
26 import org
.eclipse
.egit
.core
.project
.RepositoryMapping
;
27 import org
.eclipse
.jgit
.lib
.GitIndex
;
28 import org
.eclipse
.jgit
.lib
.Repository
;
29 import org
.eclipse
.jgit
.lib
.GitIndex
.Entry
;
33 public class AddToIndexOperation
implements IEGitOperation
{
34 private final Collection rsrcList
;
35 private final Collection
<IFile
> notAddedFiles
;
37 private final IdentityHashMap
<RepositoryMapping
, Object
> mappings
;
40 * Create a new operation to add files to the Git index
43 * collection of {@link IResource}s which should be added to the
44 * relevant Git repositories.
46 public AddToIndexOperation(final Collection rsrcs
) {
48 mappings
= new IdentityHashMap
<RepositoryMapping
, Object
>();
49 notAddedFiles
= new ArrayList
<IFile
>();
53 * @see org.eclipse.egit.core.op.IEGitOperation#execute(org.eclipse.core.runtime.IProgressMonitor)
55 public void execute(IProgressMonitor m
) throws CoreException
{
56 IProgressMonitor monitor
;
58 monitor
= new NullProgressMonitor();
61 Collection
<GitIndex
> changedIndexes
= new ArrayList
<GitIndex
>();
62 // GitIndex can not be updated if it contains staged entries
63 Collection
<GitIndex
> indexesWithStagedEntries
= new ArrayList
<GitIndex
>();
65 for (Object obj
: rsrcList
) {
66 obj
= ((IAdaptable
) obj
).getAdapter(IResource
.class);
67 if (obj
instanceof IFile
)
68 addToIndex((IFile
) obj
, changedIndexes
,
69 indexesWithStagedEntries
);
72 if (!changedIndexes
.isEmpty()) {
73 for (GitIndex idx
: changedIndexes
) {
78 } catch (RuntimeException e
) {
79 throw new CoreException(Activator
.error(CoreText
.AddToIndexOperation_failed
, e
));
80 } catch (IOException e
) {
81 throw new CoreException(Activator
.error(CoreText
.AddToIndexOperation_failed
, e
));
83 for (final RepositoryMapping rm
: mappings
.keySet())
84 rm
.fireRepositoryChanged();
91 * @return returns the files that could not be added to the index
92 * because there are unmerged entries
94 public Collection
<IFile
> getNotAddedFiles() {
98 private void addToIndex(IFile file
,
99 Collection
<GitIndex
> changedIndexes
,
100 Collection
<GitIndex
> indexesWithUnmergedEntries
) throws IOException
{
101 IProject project
= file
.getProject();
102 RepositoryMapping map
= RepositoryMapping
.getMapping(project
);
103 Repository repo
= map
.getRepository();
104 GitIndex index
= null;
105 index
= repo
.getIndex();
106 Entry entry
= index
.getEntry(map
.getRepoRelativePath(file
));
109 if (indexesWithUnmergedEntries
.contains(index
)) {
110 notAddedFiles
.add(file
);
113 if (!canUpdateIndex(index
)) {
114 indexesWithUnmergedEntries
.add(index
);
115 notAddedFiles
.add(file
);
119 if (entry
.isModified(map
.getWorkDir())) {
120 entry
.update(new File(map
.getWorkDir(), entry
.getName()));
121 if (!changedIndexes
.contains(index
))
122 changedIndexes
.add(index
);
127 * The method checks if the given index can be updated. The index can be
128 * updated if it does not contain entries with stage !=0.
131 * @return true if the given index can be updated
133 private static boolean canUpdateIndex(GitIndex index
) {
134 Entry
[] members
= index
.getMembers();
135 for (int i
= 0; i
< members
.length
; i
++) {
136 if (members
[i
].getStage() != 0)