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
.resources
.IWorkspaceRunnable
;
21 import org
.eclipse
.core
.runtime
.CoreException
;
22 import org
.eclipse
.core
.runtime
.IAdaptable
;
23 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
24 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
25 import org
.eclipse
.egit
.core
.Activator
;
26 import org
.eclipse
.egit
.core
.CoreText
;
27 import org
.eclipse
.egit
.core
.project
.RepositoryMapping
;
28 import org
.eclipse
.jgit
.lib
.GitIndex
;
29 import org
.eclipse
.jgit
.lib
.Repository
;
30 import org
.eclipse
.jgit
.lib
.GitIndex
.Entry
;
34 public class AddToIndexOperation
implements IWorkspaceRunnable
{
35 private final Collection rsrcList
;
36 private final Collection
<IFile
> notAddedFiles
;
38 private final IdentityHashMap
<RepositoryMapping
, Object
> mappings
;
41 * Create a new operation to add files to the Git index
44 * collection of {@link IResource}s which should be added to the
45 * relevant Git repositories.
47 public AddToIndexOperation(final Collection rsrcs
) {
49 mappings
= new IdentityHashMap
<RepositoryMapping
, Object
>();
50 notAddedFiles
= new ArrayList
<IFile
>();
57 * org.eclipse.core.resources.IWorkspaceRunnable#run(org.eclipse.core.runtime
60 public void run(IProgressMonitor m
) throws CoreException
{
61 IProgressMonitor monitor
;
63 monitor
= new NullProgressMonitor();
66 Collection
<GitIndex
> changedIndexes
= new ArrayList
<GitIndex
>();
67 // GitIndex can not be updated if it contains staged entries
68 Collection
<GitIndex
> indexesWithStagedEntries
= new ArrayList
<GitIndex
>();
70 for (Object obj
: rsrcList
) {
71 obj
= ((IAdaptable
) obj
).getAdapter(IResource
.class);
72 if (obj
instanceof IFile
)
73 addToIndex((IFile
) obj
, changedIndexes
,
74 indexesWithStagedEntries
);
77 if (!changedIndexes
.isEmpty()) {
78 for (GitIndex idx
: changedIndexes
) {
83 } catch (RuntimeException e
) {
84 throw Activator
.error(CoreText
.AddToIndexOperation_failed
, e
);
85 } catch (IOException e
) {
86 throw Activator
.error(CoreText
.AddToIndexOperation_failed
, e
);
88 for (final RepositoryMapping rm
: mappings
.keySet())
89 rm
.fireRepositoryChanged();
96 * @return returns the files that could not be added to the index
97 * because there are unmerged entries
99 public Collection
<IFile
> getNotAddedFiles() {
100 return notAddedFiles
;
103 private void addToIndex(IFile file
,
104 Collection
<GitIndex
> changedIndexes
,
105 Collection
<GitIndex
> indexesWithUnmergedEntries
) throws IOException
{
106 IProject project
= file
.getProject();
107 RepositoryMapping map
= RepositoryMapping
.getMapping(project
);
108 Repository repo
= map
.getRepository();
109 GitIndex index
= null;
110 index
= repo
.getIndex();
111 Entry entry
= index
.getEntry(map
.getRepoRelativePath(file
));
114 if (indexesWithUnmergedEntries
.contains(index
)) {
115 notAddedFiles
.add(file
);
118 if (!canUpdateIndex(index
)) {
119 indexesWithUnmergedEntries
.add(index
);
120 notAddedFiles
.add(file
);
124 if (entry
.isModified(map
.getWorkDir())) {
125 entry
.update(new File(map
.getWorkDir(), entry
.getName()));
126 if (!changedIndexes
.contains(index
))
127 changedIndexes
.add(index
);
132 * The method checks if the given index can be updated. The index can be
133 * updated if it does not contain entries with stage !=0.
136 * @return true if the given index can be updated
138 private static boolean canUpdateIndex(GitIndex index
) {
139 Entry
[] members
= index
.getMembers();
140 for (int i
= 0; i
< members
.length
; i
++) {
141 if (members
[i
].getStage() != 0)