Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6514bdd8500995387aa8649290bf79903ef914f7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
/*******************************************************************************
 * Copyright (c) 2010, 2013 SAP AG and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *    Mathias Kinzler (SAP AG) - initial implementation
 *    Laurent Goubet <laurent.goubet@obeo.fr - 404121
 *******************************************************************************/
package org.eclipse.egit.ui.internal.repository;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.DirectoryIteratorException;
import java.nio.file.DirectoryStream;
import java.nio.file.FileVisitOption;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.WeakHashMap;

import org.eclipse.core.commands.IStateListener;
import org.eclipse.core.commands.State;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.WorkspaceJob;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.egit.core.RepositoryCache;
import org.eclipse.egit.core.RepositoryUtil;
import org.eclipse.egit.ui.Activator;
import org.eclipse.egit.ui.internal.CommonUtils;
import org.eclipse.egit.ui.internal.UIText;
import org.eclipse.egit.ui.internal.repository.tree.AdditionalRefNode;
import org.eclipse.egit.ui.internal.repository.tree.AdditionalRefsNode;
import org.eclipse.egit.ui.internal.repository.tree.BranchHierarchyNode;
import org.eclipse.egit.ui.internal.repository.tree.BranchesNode;
import org.eclipse.egit.ui.internal.repository.tree.ErrorNode;
import org.eclipse.egit.ui.internal.repository.tree.FetchNode;
import org.eclipse.egit.ui.internal.repository.tree.FileNode;
import org.eclipse.egit.ui.internal.repository.tree.FolderNode;
import org.eclipse.egit.ui.internal.repository.tree.LocalNode;
import org.eclipse.egit.ui.internal.repository.tree.PushNode;
import org.eclipse.egit.ui.internal.repository.tree.RefNode;
import org.eclipse.egit.ui.internal.repository.tree.RemoteNode;
import org.eclipse.egit.ui.internal.repository.tree.RemoteTrackingNode;
import org.eclipse.egit.ui.internal.repository.tree.RemotesNode;
import org.eclipse.egit.ui.internal.repository.tree.RepositoryNode;
import org.eclipse.egit.ui.internal.repository.tree.RepositoryTreeNode;
import org.eclipse.egit.ui.internal.repository.tree.StashNode;
import org.eclipse.egit.ui.internal.repository.tree.StashedCommitNode;
import org.eclipse.egit.ui.internal.repository.tree.SubmodulesNode;
import org.eclipse.egit.ui.internal.repository.tree.TagNode;
import org.eclipse.egit.ui.internal.repository.tree.TagsNode;
import org.eclipse.egit.ui.internal.repository.tree.WorkingDirNode;
import org.eclipse.egit.ui.internal.repository.tree.command.ToggleBranchHierarchyCommand;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.events.ListenerHandle;
import org.eclipse.jgit.events.RefsChangedEvent;
import org.eclipse.jgit.events.RefsChangedListener;
import org.eclipse.jgit.lib.ConfigConstants;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.RefDatabase;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevObject;
import org.eclipse.jgit.revwalk.RevTag;
import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.submodule.SubmoduleWalk;
import org.eclipse.jgit.transport.RemoteConfig;
import org.eclipse.jgit.transport.URIish;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.commands.ICommandService;

/**
 * Content Provider for the Git Repositories View
 */
public class RepositoriesViewContentProvider implements ITreeContentProvider,
		IStateListener {

	private static final Object[] NO_CHILDREN = new Object[0];

	private final RepositoryCache repositoryCache = org.eclipse.egit.core.Activator
			.getDefault().getRepositoryCache();

	private final State commandState;

	private boolean branchHierarchyMode = false;

	private boolean showUnbornHead = false;

	private Map<Repository, Map<String, Ref>> branchRefs = new WeakHashMap<>();

	private Map<Repository, ListenerHandle> refsChangedListeners = new WeakHashMap<>();

	/**
	 * Constructs a new {@link RepositoriesViewContentProvider} that doesn't
	 * show an unborn branch as HEAD.
	 */
	public RepositoriesViewContentProvider() {
		this(false);
	}

	/**
	 * Constructs a new {@link RepositoriesViewContentProvider}.
	 *
	 * @param showUnbornHead
	 *            whether to show HEAD even if it is an unborn branch
	 */
	public RepositoriesViewContentProvider(boolean showUnbornHead) {
		super();
		this.showUnbornHead = showUnbornHead;
		ICommandService srv = CommonUtils.getService(PlatformUI.getWorkbench(), ICommandService.class);
		commandState = srv.getCommand(
				ToggleBranchHierarchyCommand.ID)
				.getState(ToggleBranchHierarchyCommand.TOGGLE_STATE);
		commandState.addListener(this);
		try {
			this.branchHierarchyMode = ((Boolean) commandState.getValue())
					.booleanValue();
		} catch (Exception e) {
			Activator.handleError(e.getMessage(), e, false);
		}
	}

	@Override
	@SuppressWarnings("unchecked")
	public Object[] getElements(Object inputElement) {

		List<RepositoryTreeNode> nodes = new ArrayList<>();
		List<String> directories = new ArrayList<>();
		RepositoryUtil repositoryUtil = Activator.getDefault()
				.getRepositoryUtil();

		if (inputElement instanceof Collection) {
			for (Object next : ((Collection) inputElement)) {
				if (next instanceof RepositoryTreeNode) {
					nodes.add((RepositoryTreeNode) next);
				} else if (next instanceof String) {
					directories.add((String) next);
				}
			}
		} else if (inputElement instanceof IWorkspaceRoot) {
			directories.addAll(repositoryUtil.getConfiguredRepositories());
		}

		for (String directory : directories) {
			try {
				File gitDir = new File(directory);
				if (gitDir.exists()) {
					RepositoryNode rNode = new RepositoryNode(null,
							repositoryCache.lookupRepository(gitDir));
					nodes.add(rNode);
				} else
					repositoryUtil.removeDir(gitDir);
			} catch (IOException e) {
				// ignore for now
			}
		}

		Collections.sort(nodes);
		return nodes.toArray();
	}

	@Override
	public void dispose() {
		commandState.removeListener(this);
		for (ListenerHandle handle : refsChangedListeners.values())
			handle.remove();
		refsChangedListeners.clear();
	}

	@Override
	public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
		// nothing
	}

	@Override
	public Object[] getChildren(Object parentElement) {

		RepositoryTreeNode node = (RepositoryTreeNode) parentElement;
		Repository repo = node.getRepository();

		switch (node.getType()) {

		case BRANCHES: {
			List<RepositoryTreeNode> nodes = new ArrayList<>();
			nodes.add(new LocalNode(node, repo));
			nodes.add(new RemoteTrackingNode(node, repo));
			return nodes.toArray();
		}

		case LOCAL:
			return getBranchChildren(node, repo, Constants.R_HEADS);

		case REMOTETRACKING:
			return getBranchChildren(node, repo, Constants.R_REMOTES);

		case BRANCHHIERARCHY: {
			return getBranchHierarchyChildren((BranchHierarchyNode) node, repo,
					node);
		}

		case TAGS:
			return getTagsChildren(node, repo);

		case ADDITIONALREFS: {
			List<RepositoryTreeNode<Ref>> refs = new ArrayList<>();
			try {
				for (Entry<String, Ref> refEntry : getRefs(repo, RefDatabase.ALL).entrySet()) {
					String name=refEntry.getKey();
					if (!(name.startsWith(Constants.R_HEADS) || name.startsWith(Constants.R_TAGS)|| name.startsWith(Constants.R_REMOTES)))
						refs.add(new AdditionalRefNode(node, repo, refEntry
								.getValue()));
				}
				for (Ref r : repo.getRefDatabase().getAdditionalRefs()) {
					refs.add(new AdditionalRefNode(node, repo, r));
				}
				if (showUnbornHead) {
					Ref head = repo.exactRef(Constants.HEAD);
					if (head != null && head.isSymbolic()
							&& head.getObjectId() == null) {
						refs.add(new AdditionalRefNode(node, repo, head));
					}
				}
			} catch (Exception e) {
				return handleException(e, node);
			}
			return refs.toArray();
		}

		case REMOTES: {
			List<RepositoryTreeNode<String>> remotes = new ArrayList<>();

			Repository rep = node.getRepository();

			Set<String> configNames = rep.getConfig().getSubsections(
					RepositoriesView.REMOTE);

			for (String configName : configNames) {
				remotes.add(new RemoteNode(node, repo, configName));
			}

			return remotes.toArray();
		}

		case REPO: {
			List<RepositoryTreeNode<? extends Object>> nodeList = new ArrayList<>();
			nodeList.add(new BranchesNode(node, repo));
			nodeList.add(new TagsNode(node, repo));
			nodeList.add(new AdditionalRefsNode(node, repo));
			final boolean bare = repo.isBare();
			if (!bare)
				nodeList.add(new WorkingDirNode(node, repo));
			nodeList.add(new RemotesNode(node, repo));
			if(!bare && hasStashedCommits(repo))
				nodeList.add(new StashNode(node, repo));
			if (!bare && hasConfiguredSubmodules(repo))
				nodeList.add(new SubmodulesNode(node, repo));

			return nodeList.toArray();
		}

		case WORKINGDIR:
			if (repo.isBare()) {
				return NO_CHILDREN;
			}
			return getDirectoryChildren(node, repo.getWorkTree());

		case FOLDER:
			return getDirectoryChildren(node, (File) node.getObject());

		case REMOTE: {
			List<RepositoryTreeNode<String>> children = new ArrayList<>();

			String remoteName = (String) node.getObject();
			RemoteConfig rc;
			try {
				rc = new RemoteConfig(node.getRepository().getConfig(),
						remoteName);
			} catch (URISyntaxException e) {
				return handleException(e, node);
			}

			if (!rc.getURIs().isEmpty())
				children.add(new FetchNode(node, node.getRepository(), rc
						.getURIs().get(0).toPrivateString()));

			int uriCount = rc.getPushURIs().size();
			if (uriCount == 0 && !rc.getURIs().isEmpty())
				uriCount++;

			// show push if either a fetch or push URI is specified and
			// at least one push specification
			if (uriCount > 0) {
				URIish firstUri;
				if (!rc.getPushURIs().isEmpty())
					firstUri = rc.getPushURIs().get(0);
				else
					firstUri = rc.getURIs().get(0);

				if (uriCount == 1)
					children.add(new PushNode(node, node.getRepository(),
							firstUri.toPrivateString()));
				else
					children.add(new PushNode(node, node.getRepository(),
							firstUri.toPrivateString() + "...")); //$NON-NLS-1$
			}
			return children.toArray();

		}

		case SUBMODULES:
			List<RepositoryNode> children = new ArrayList<>();
			try {
				SubmoduleWalk walk = SubmoduleWalk.forIndex(node
						.getRepository());
				while (walk.next()) {
					Repository subRepo = walk.getRepository();
					if (subRepo != null) {
						Repository cachedRepo = null;
						try {
							cachedRepo = repositoryCache
								.lookupRepository(subRepo.getDirectory());
						} finally {
							subRepo.close();
						}
						if (cachedRepo != null)
							children.add(new RepositoryNode(node, cachedRepo));
					}
				}
			} catch (IOException e) {
				handleException(e, node);
			}
			return children.toArray();
		case STASH:
			List<StashedCommitNode> stashNodes = new ArrayList<>();
			int index = 0;
			try {
				for (RevCommit commit : Git.wrap(repo).stashList().call())
					stashNodes.add(new StashedCommitNode(node, repo, index++,
							commit));
			} catch (Exception e) {
				handleException(e, node);
			}
			return stashNodes.toArray();
		case FILE:
			// fall through
		case REF:
			// fall through
		case PUSH:
			// fall through
		case TAG:
			// fall through
		case FETCH:
			// fall through
		case ERROR:
			// fall through
		case STASHED_COMMIT:
			// fall through
		case ADDITIONALREF:
			return null;

		}

		return null;

	}

	private Object[] getBranchChildren(RepositoryTreeNode node, Repository repo,
			String prefix) {
		if (branchHierarchyMode) {
			return getBranchHierarchyChildren(
					new BranchHierarchyNode(node, repo, new Path(prefix)), repo,
					node);
		} else {
			try {
				return getRefs(repo, prefix).values().stream()
						.filter(ref -> !ref.isSymbolic())
						.map(ref -> new RefNode(node, repo, ref)).toArray();
			} catch (IOException e) {
				return handleException(e, node);
			}
		}
	}

	private Object[] getBranchHierarchyChildren(BranchHierarchyNode hierNode,
			Repository repo, RepositoryTreeNode parentNode) {
		try {
			List<RepositoryTreeNode> children = new ArrayList<>();
			for (IPath path : hierNode.getChildPaths()) {
				children.add(new BranchHierarchyNode(parentNode, repo, path));
			}
			for (Ref ref : hierNode.getChildRefs()) {
				children.add(new RefNode(parentNode, repo, ref));
			}
			return children.toArray();
		} catch (IOException e) {
			return handleException(e, parentNode);
		}
	}

	private Object[] getDirectoryChildren(RepositoryTreeNode parentNode,
			File parent) {
		Repository repo = parentNode.getRepository();
		List<RepositoryTreeNode<File>> children = new ArrayList<>();

		try {
			Files.walkFileTree(parent.toPath(),
					EnumSet.noneOf(FileVisitOption.class), 1,
					new SimpleFileVisitor<java.nio.file.Path>() {
						@Override
						public FileVisitResult visitFile(
								java.nio.file.Path file,
								BasicFileAttributes attrs) throws IOException {
							if (attrs.isDirectory()) {
								children.add(new FolderNode(parentNode, repo,
										file.toFile()));
							} else {
								children.add(new FileNode(parentNode, repo,
										file.toFile()));
							}
							return FileVisitResult.CONTINUE;
						}

						@Override
						public FileVisitResult visitFileFailed(
								java.nio.file.Path file, IOException exc)
								throws IOException {
							// Just ignore it
							return FileVisitResult.CONTINUE;
						}
					});
		} catch (IOException e) {
			// Ignore
		}

		return children.toArray();
	}

	private Object[] getTagsChildren(RepositoryTreeNode parentNode,
			Repository repo) {
		List<RepositoryTreeNode<Ref>> nodes = new ArrayList<>();

		try (RevWalk walk = new RevWalk(repo)) {
			walk.setRetainBody(true);
			for (Ref tagRef : getRefs(repo, Constants.R_TAGS).values()) {
				ObjectId objectId = tagRef.getLeaf().getObjectId();
				RevObject revObject = walk.parseAny(objectId);
				RevObject peeledObject = walk.peel(revObject);
				TagNode tagNode = createTagNode(parentNode, repo, tagRef,
						revObject, peeledObject);
				nodes.add(tagNode);
			}
		} catch (IOException e) {
			return handleException(e, parentNode);
		}

		return nodes.toArray();
	}

	private TagNode createTagNode(RepositoryTreeNode parentNode,
			Repository repo, Ref ref, RevObject revObject,
			RevObject peeledObject) {
		boolean annotated = (revObject instanceof RevTag);
		if (peeledObject instanceof RevCommit) {
			RevCommit commit = (RevCommit) peeledObject;
			String id = commit.getId().name();
			String message = commit.getShortMessage();
			return new TagNode(parentNode, repo, ref, annotated, id, message);
		} else {
			return new TagNode(parentNode, repo, ref, annotated, "", ""); //$NON-NLS-1$ //$NON-NLS-2$
		}
	}

	private Object[] handleException(Exception e, RepositoryTreeNode parentNode) {
		Activator.handleError(e.getMessage(), e, false);
		// add a node indicating that there was an Exception
		String message = e.getMessage();
		if (message == null)
			return new Object[] { new ErrorNode(parentNode, parentNode
					.getRepository(),
					UIText.RepositoriesViewContentProvider_ExceptionNodeText) };
		else
			return new Object[] { new ErrorNode(parentNode, parentNode
					.getRepository(), message) };
	}

	@Override
	public Object getParent(Object element) {
		if (element instanceof RepositoryTreeNode)
			return ((RepositoryTreeNode) element).getParent();
		return null;
	}

	@Override
	public boolean hasChildren(Object element) {
		// for some of the nodes we can optimize this call
		RepositoryTreeNode node = (RepositoryTreeNode) element;
		Repository repo = node.getRepository();
		switch (node.getType()) {
		case BRANCHES:
		case REPO:
		case ADDITIONALREFS:
		case SUBMODULES:
			return true;
		case TAGS:
			return hasTagsChildren(repo);
		case WORKINGDIR:
			return !repo.isBare() && hasDirectoryChildren(repo.getWorkTree());
		case FOLDER:
			return !repo.isBare()
					&& hasDirectoryChildren((File) node.getObject());
		case FILE:
			return false;
		default:
			Object[] children = getChildren(element);
			return children != null && children.length > 0;
		}
	}

	private boolean hasDirectoryChildren(File file) {
		try (DirectoryStream<java.nio.file.Path> dir = Files
				.newDirectoryStream(file.toPath())) {
			return dir.iterator().hasNext();
		} catch (DirectoryIteratorException | IOException e) {
			return false;
		}
	}

	/**
	 * As long as the ref database has not been read, assume there are tags, and
	 * start reading the database in the background. This should avoid long
	 * blocking during startup.
	 *
	 * @param repo
	 * @return whether the tags node has children.
	 */
	private boolean hasTagsChildren(Repository repo) {
		try {
			if (branchRefs.get(repo) == null) {
				WorkspaceJob job = new WorkspaceJob(
						UIText.RepositoriesViewContentProvider_ReadReferencesJob) {

					@Override
					public IStatus runInWorkspace(IProgressMonitor monitor)
							throws CoreException {
						try {
							// trigger reading the reference database
							getRefs(repo, Constants.R_TAGS);
						} catch (IOException e) {
							return Status.CANCEL_STATUS;
						}
						return Status.OK_STATUS;
					}
				};
				job.setSystem(true);
				job.schedule();
				return true;
			}
			return !getRefs(repo, Constants.R_TAGS).isEmpty();
		} catch (IOException e) {
			return true;
		}
	}

	@Override
	public void handleStateChange(State state, Object oldValue) {
		try {
			this.branchHierarchyMode = ((Boolean) state.getValue())
					.booleanValue();
		} catch (Exception e) {
			Activator.handleError(e.getMessage(), e, false);
		}
	}

	private synchronized Map<String, Ref> getRefs(final Repository repo, final String prefix) throws IOException {
		Map<String, Ref> allRefs = branchRefs.get(repo);
		if (allRefs == null) {
			allRefs = repo.getRefDatabase().getRefs(RefDatabase.ALL);
			branchRefs.put(repo, allRefs);
			if (refsChangedListeners.get(repo) == null) {
				RefsChangedListener listener = new RefsChangedListener() {
					@Override
					public void onRefsChanged(RefsChangedEvent event) {
						synchronized (RepositoriesViewContentProvider.this) {
							branchRefs.remove(repo);
						}
					}
				};
				refsChangedListeners.put(repo, repo.getListenerList()
						.addRefsChangedListener(listener));
			}
		}
		if (prefix.equals(RefDatabase.ALL))
			return allRefs;

		Map<String, Ref> filtered = new HashMap<>();
		for (Map.Entry<String, Ref> entry : allRefs.entrySet()) {
			if (entry.getKey().startsWith(prefix))
				filtered.put(entry.getKey(), entry.getValue());
		}
		return filtered;
	}

	/**
	 * Does the repository have any submodule configurations?
	 * <p>
	 * This method checks for a '.gitmodules' file at the root of the working
	 * directory or any 'submodule' sections in the repository's config file
	 *
	 * @param repository
	 * @return true if submodules, false otherwise
	 */
	private boolean hasConfiguredSubmodules(final Repository repository) {
		if (new File(repository.getWorkTree(), Constants.DOT_GIT_MODULES)
				.isFile())
			return true;
		return !repository.getConfig()
				.getSubsections(ConfigConstants.CONFIG_SUBMODULE_SECTION)
				.isEmpty();
	}

	/**
	 * Does the repository have any stashed commits?
	 * <p>
	 * This method checks for a {@link Constants#R_STASH} ref in the given
	 * repository
	 *
	 * @param repository
	 * @return true if stashed commits, false otherwise
	 */
	private boolean hasStashedCommits(final Repository repository) {
		try {
			return repository.exactRef(Constants.R_STASH) != null;
		} catch (IOException e) {
			return false;
		}
	}

	/**
	 * Tells whether this content provider is using a hierarchical branch
	 * layout.
	 *
	 * @return {@code true} if this content provider uses a hierarchical branch
	 *         layout; {@code false} otherwise
	 */
	public boolean isHierarchical() {
		return branchHierarchyMode;
	}
}

Back to the top