2 * Copyright (C) 2007 David Watson <dwatson@mimvista.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License, version 2.1, as published by the Free Software Foundation.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18 package org
.spearce
.egit
.ui
.internal
.actions
;
20 import java
.io
.IOException
;
21 import java
.io
.UnsupportedEncodingException
;
22 import java
.util
.ArrayList
;
23 import java
.util
.Collections
;
24 import java
.util
.HashSet
;
25 import java
.util
.Iterator
;
26 import java
.util
.List
;
28 import org
.eclipse
.core
.resources
.IFile
;
29 import org
.eclipse
.core
.resources
.IProject
;
30 import org
.eclipse
.core
.resources
.IResource
;
31 import org
.eclipse
.core
.resources
.IResourceVisitor
;
32 import org
.eclipse
.core
.runtime
.CoreException
;
33 import org
.eclipse
.core
.runtime
.Path
;
34 import org
.eclipse
.jface
.action
.IAction
;
35 import org
.eclipse
.jface
.dialogs
.IDialogConstants
;
36 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
37 import org
.eclipse
.jface
.viewers
.ISelection
;
38 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
39 import org
.eclipse
.ui
.IObjectActionDelegate
;
40 import org
.eclipse
.ui
.IWorkbenchPart
;
41 import org
.spearce
.egit
.core
.project
.GitProjectData
;
42 import org
.spearce
.egit
.core
.project
.RepositoryMapping
;
43 import org
.spearce
.egit
.ui
.internal
.dialogs
.CommitDialog
;
44 import org
.spearce
.jgit
.lib
.GitIndex
;
45 import org
.spearce
.jgit
.lib
.IndexDiff
;
46 import org
.spearce
.jgit
.lib
.Repository
;
47 import org
.spearce
.jgit
.lib
.Tree
;
49 public class CommitAction
implements IObjectActionDelegate
{
50 private IWorkbenchPart wp
;
51 private List rsrcList
;
53 public void setActivePart(final IAction act
, final IWorkbenchPart part
) {
57 public void run(IAction act
) {
60 buildIndexHeadDiffList();
63 } catch (CoreException e
) {
65 } catch (IOException e
) {
68 if (files
.isEmpty()) {
69 MessageDialog
.openWarning(wp
.getSite().getShell(),
70 "No files to commit", "No changed items were selected.");
74 CommitDialog commitDialog
= new CommitDialog(wp
.getSite().getShell());
75 commitDialog
.setFileList(files
);
76 if (commitDialog
.open() != IDialogConstants
.OK_ID
)
79 String commitMessage
= commitDialog
.getCommitMessage();
80 System
.out
.println("Commit Message: " + commitMessage
);
81 IFile
[] selectedItems
= commitDialog
.getSelectedItems();
82 for (IFile file
: selectedItems
) {
83 System
.out
.println("\t" + file
);
87 private void buildIndexHeadDiffList() throws IOException
{
88 for (IProject project
: listProjects()) {
89 final GitProjectData projectData
= GitProjectData
.get(project
);
90 if (projectData
!= null) {
91 RepositoryMapping repositoryMapping
= projectData
.getRepositoryMapping(project
);
92 Repository repository
= repositoryMapping
.getRepository();
93 Tree head
= repository
.mapTree("HEAD");
94 GitIndex index
= repository
.getIndex();
95 IndexDiff indexDiff
= new IndexDiff(head
, index
);
98 includeList(project
, indexDiff
.getAdded());
99 includeList(project
, indexDiff
.getChanged());
100 includeList(project
, indexDiff
.getRemoved());
105 private void includeList(IProject project
, HashSet
<String
> added
) {
106 for (String filename
: added
) {
107 Path path
= new Path(filename
);
109 IResource member
= project
.getWorkspace().getRoot().getFile(path
);
111 member
= project
.getFile(path
);
113 if (member
!= null && member
instanceof IFile
) {
114 files
.add((IFile
) member
);
116 System
.out
.println("Couldn't find " + filename
);
118 } catch (Exception t
) { continue;} // if it's outside the workspace, bad things happen
122 private ArrayList
<IProject
> listProjects() {
123 ArrayList
<IProject
> projects
= new ArrayList
<IProject
>();
125 for (Iterator i
= rsrcList
.iterator(); i
.hasNext();) {
126 IResource res
= (IResource
) i
.next();
127 if (!projects
.contains(res
.getProject()))
128 projects
.add(res
.getProject());
133 private ArrayList
<IFile
> files
= new ArrayList
<IFile
>();
135 private void buildList() throws CoreException
{
136 for (final Iterator i
= rsrcList
.iterator(); i
.hasNext();) {
137 IResource resource
= (IResource
) i
.next();
138 final IProject project
= resource
.getProject();
139 final GitProjectData projectData
= GitProjectData
.get(project
);
141 if (projectData
!= null) {
142 // final RepositoryMapping repositoryMapping =
143 // projectData.getRepositoryMapping(project);
144 // final Repository repository =
145 // repositoryMapping.getRepository();
147 if (resource
instanceof IFile
) {
148 tryAddResource((IFile
) resource
, projectData
);
150 resource
.accept(new IResourceVisitor() {
151 public boolean visit(IResource rsrc
)
152 throws CoreException
{
153 if (rsrc
instanceof IFile
) {
154 tryAddResource((IFile
) rsrc
, projectData
);
165 public void tryAddResource(IFile resource
, GitProjectData projectData
) {
166 if (files
.contains(resource
))
170 RepositoryMapping repositoryMapping
= projectData
171 .getRepositoryMapping(resource
.getProject());
173 if (repositoryMapping
.isResourceChanged(resource
))
175 } catch (UnsupportedEncodingException e
) {
177 } catch (IOException e
) {
179 } catch (Exception e
) {
184 public void selectionChanged(IAction act
, ISelection sel
) {
185 final List selection
;
186 if (sel
instanceof IStructuredSelection
&& !sel
.isEmpty()) {
187 selection
= ((IStructuredSelection
) sel
).toList();
189 selection
= Collections
.EMPTY_LIST
;
191 act
.setEnabled(!selection
.isEmpty()) ;
192 rsrcList
= selection
;