1 /*******************************************************************************
2 * Copyright (c) 2010 SAP AG.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * Mathias Kinzler (SAP AG) - initial implementation
10 *******************************************************************************/
11 package org
.eclipse
.egit
.ui
.internal
.repository
;
14 import java
.io
.IOException
;
15 import java
.net
.URISyntaxException
;
16 import java
.util
.ArrayList
;
17 import java
.util
.Arrays
;
18 import java
.util
.Collections
;
19 import java
.util
.Comparator
;
20 import java
.util
.List
;
22 import java
.util
.Map
.Entry
;
24 import org
.eclipse
.egit
.ui
.Activator
;
25 import org
.eclipse
.egit
.ui
.UIText
;
26 import org
.eclipse
.egit
.ui
.internal
.repository
.tree
.BranchesNode
;
27 import org
.eclipse
.egit
.ui
.internal
.repository
.tree
.ErrorNode
;
28 import org
.eclipse
.egit
.ui
.internal
.repository
.tree
.FetchNode
;
29 import org
.eclipse
.egit
.ui
.internal
.repository
.tree
.FileNode
;
30 import org
.eclipse
.egit
.ui
.internal
.repository
.tree
.FolderNode
;
31 import org
.eclipse
.egit
.ui
.internal
.repository
.tree
.LocalBranchesNode
;
32 import org
.eclipse
.egit
.ui
.internal
.repository
.tree
.PushNode
;
33 import org
.eclipse
.egit
.ui
.internal
.repository
.tree
.RefNode
;
34 import org
.eclipse
.egit
.ui
.internal
.repository
.tree
.RemoteBranchesNode
;
35 import org
.eclipse
.egit
.ui
.internal
.repository
.tree
.RemoteNode
;
36 import org
.eclipse
.egit
.ui
.internal
.repository
.tree
.RemotesNode
;
37 import org
.eclipse
.egit
.ui
.internal
.repository
.tree
.RepositoryTreeNode
;
38 import org
.eclipse
.egit
.ui
.internal
.repository
.tree
.SymbolicRefNode
;
39 import org
.eclipse
.egit
.ui
.internal
.repository
.tree
.SymbolicRefsNode
;
40 import org
.eclipse
.egit
.ui
.internal
.repository
.tree
.TagNode
;
41 import org
.eclipse
.egit
.ui
.internal
.repository
.tree
.TagsNode
;
42 import org
.eclipse
.egit
.ui
.internal
.repository
.tree
.WorkingDirNode
;
43 import org
.eclipse
.jface
.viewers
.ITreeContentProvider
;
44 import org
.eclipse
.jface
.viewers
.Viewer
;
45 import org
.eclipse
.jgit
.lib
.Constants
;
46 import org
.eclipse
.jgit
.lib
.Ref
;
47 import org
.eclipse
.jgit
.lib
.RefDatabase
;
48 import org
.eclipse
.jgit
.lib
.Repository
;
49 import org
.eclipse
.jgit
.transport
.RemoteConfig
;
52 * Content Provider for the Git Repositories View
54 public class RepositoriesViewContentProvider
implements ITreeContentProvider
{
56 @SuppressWarnings("unchecked")
57 public Object
[] getElements(Object inputElement
) {
59 List
<RepositoryTreeNode
> nodes
= (List
<RepositoryTreeNode
>) inputElement
;
60 Collections
.sort(nodes
);
61 return nodes
.toArray();
64 public void dispose() {
68 public void inputChanged(Viewer viewer
, Object oldInput
, Object newInput
) {
72 public Object
[] getChildren(Object parentElement
) {
74 RepositoryTreeNode node
= (RepositoryTreeNode
) parentElement
;
75 Repository repo
= node
.getRepository();
77 switch (node
.getType()) {
81 List
<RepositoryTreeNode
<Repository
>> nodes
= new ArrayList
<RepositoryTreeNode
<Repository
>>();
83 nodes
.add(new LocalBranchesNode(node
, repo
));
84 nodes
.add(new RemoteBranchesNode(node
, repo
));
86 return nodes
.toArray();
90 List
<RepositoryTreeNode
<Ref
>> refs
= new ArrayList
<RepositoryTreeNode
<Ref
>>();
93 for (Entry
<String
, Ref
> refEntry
: repo
.getRefDatabase()
94 .getRefs(Constants
.R_HEADS
).entrySet()) {
95 if (!refEntry
.getValue().isSymbolic())
96 refs
.add(new RefNode(node
, repo
, refEntry
.getValue()));
98 } catch (IOException e
) {
99 handleException(e
, node
);
102 return refs
.toArray();
105 case REMOTEBRANCHES
: {
106 List
<RepositoryTreeNode
<Ref
>> refs
= new ArrayList
<RepositoryTreeNode
<Ref
>>();
109 for (Entry
<String
, Ref
> refEntry
: repo
.getRefDatabase()
110 .getRefs(Constants
.R_REMOTES
).entrySet()) {
111 if (!refEntry
.getValue().isSymbolic())
112 refs
.add(new RefNode(node
, repo
, refEntry
.getValue()));
114 } catch (IOException e
) {
115 handleException(e
, node
);
118 return refs
.toArray();
121 List
<RepositoryTreeNode
<Ref
>> refs
= new ArrayList
<RepositoryTreeNode
<Ref
>>();
124 for (Entry
<String
, Ref
> refEntry
: repo
.getRefDatabase()
125 .getRefs(Constants
.R_TAGS
).entrySet()) {
126 refs
.add(new TagNode(node
, repo
, refEntry
.getValue()));
128 } catch (IOException e
) {
129 handleException(e
, node
);
132 return refs
.toArray();
136 List
<RepositoryTreeNode
<Ref
>> refs
= new ArrayList
<RepositoryTreeNode
<Ref
>>();
139 for (Entry
<String
, Ref
> refEntry
: repo
.getRefDatabase()
140 .getRefs(RefDatabase
.ALL
).entrySet()) {
141 if (refEntry
.getValue().isSymbolic())
142 refs
.add(new SymbolicRefNode(node
, repo
, refEntry
145 } catch (IOException e
) {
146 handleException(e
, node
);
149 return refs
.toArray();
153 List
<RepositoryTreeNode
<String
>> remotes
= new ArrayList
<RepositoryTreeNode
<String
>>();
155 Repository rep
= node
.getRepository();
157 Set
<String
> configNames
= rep
.getConfig().getSubsections(
158 RepositoriesView
.REMOTE
);
160 for (String configName
: configNames
) {
161 remotes
.add(new RemoteNode(node
, repo
, configName
));
164 return remotes
.toArray();
169 List
<RepositoryTreeNode
<?
extends Object
>> nodeList
= new ArrayList
<RepositoryTreeNode
<?
extends Object
>>();
171 nodeList
.add(new BranchesNode(node
, repo
));
172 nodeList
.add(new TagsNode(node
, repo
));
173 nodeList
.add(new SymbolicRefsNode(node
, repo
));
174 nodeList
.add(new WorkingDirNode(node
, repo
));
175 nodeList
.add(new RemotesNode(node
, repo
));
177 return nodeList
.toArray();
181 List
<RepositoryTreeNode
<File
>> children
= new ArrayList
<RepositoryTreeNode
<File
>>();
183 if (node
.getRepository().getConfig().getBoolean(
184 "core", "bare", false)) //$NON-NLS-1$ //$NON-NLS-2$
185 return children
.toArray();
186 File workingDir
= repo
.getWorkDir();
187 if (workingDir
== null || !workingDir
.exists())
190 File
[] childFiles
= workingDir
.listFiles();
191 Arrays
.sort(childFiles
, new Comparator
<File
>() {
192 public int compare(File o1
, File o2
) {
193 if (o1
.isDirectory()) {
194 if (o2
.isDirectory()) {
195 return o1
.compareTo(o2
);
198 } else if (o2
.isDirectory()) {
201 return o1
.compareTo(o2
);
204 for (File file
: childFiles
) {
205 if (file
.isDirectory()) {
206 children
.add(new FolderNode(node
, repo
, file
));
208 children
.add(new FileNode(node
, repo
, file
));
212 return children
.toArray();
216 List
<RepositoryTreeNode
<File
>> children
= new ArrayList
<RepositoryTreeNode
<File
>>();
218 File parent
= ((File
) node
.getObject());
220 File
[] childFiles
= parent
.listFiles();
221 Arrays
.sort(childFiles
, new Comparator
<File
>() {
222 public int compare(File o1
, File o2
) {
223 if (o1
.isDirectory()) {
224 if (o2
.isDirectory()) {
225 return o1
.compareTo(o2
);
228 } else if (o2
.isDirectory()) {
231 return o1
.compareTo(o2
);
234 for (File file
: childFiles
) {
235 if (file
.isDirectory()) {
236 children
.add(new FolderNode(node
, repo
, file
));
238 children
.add(new FileNode(node
, repo
, file
));
242 return children
.toArray();
247 List
<RepositoryTreeNode
<String
>> children
= new ArrayList
<RepositoryTreeNode
<String
>>();
249 String remoteName
= (String
) node
.getObject();
252 rc
= new RemoteConfig(node
.getRepository().getConfig(),
254 } catch (URISyntaxException e
) {
255 handleException(e
, node
);
256 return children
.toArray();
259 if (!rc
.getURIs().isEmpty())
260 children
.add(new FetchNode(node
, node
.getRepository(), rc
261 .getURIs().get(0).toPrivateString()));
263 if (!rc
.getPushURIs().isEmpty())
264 if (rc
.getPushURIs().size() == 1)
265 children
.add(new PushNode(node
, node
.getRepository(), rc
266 .getPushURIs().get(0).toPrivateString()));
268 children
.add(new PushNode(node
, node
.getRepository(), rc
269 .getPushURIs().get(0).toPrivateString()
270 + "...")); //$NON-NLS-1$
272 return children
.toArray();
297 private void handleException(Exception e
, RepositoryTreeNode parentNode
) {
298 Activator
.handleError(e
.getMessage(), e
, false);
299 // add a node indicating that there was an Exception
300 new ErrorNode(parentNode
, parentNode
.getRepository(),
301 UIText
.RepositoriesViewContentProvider_ExceptionNodeText
);
304 public Object
getParent(Object element
) {
306 return ((RepositoryTreeNode
) element
).getParent();
309 public boolean hasChildren(Object element
) {
310 Object
[] children
= getChildren(element
);
311 return children
!= null && children
.length
> 0;