Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f8945dc80d215980267255b7edee387d90f983ff (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
/*******************************************************************************
 * Copyright (C) 2008, 2015 Shawn O. Pearce <spearce@spearce.org>
 *
 * 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:
 *   Tobias Baumann <tobbaumann@gmail.com> - Bug 475836
 *******************************************************************************/
package org.eclipse.egit.ui.internal.history;

import java.io.IOException;

import org.eclipse.jgit.errors.IncorrectObjectTypeException;
import org.eclipse.jgit.errors.MissingObjectException;
import org.eclipse.jgit.lib.AnyObjectId;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revplot.PlotWalk;
import org.eclipse.jgit.revwalk.RevCommit;

class SWTWalk extends PlotWalk {

	private final Repository repo;

	private Ref head;

	private boolean headInitialized;

	SWTWalk(final Repository repo) {
		super(repo);
		this.repo = repo;
	}

	Repository getRepository() {
		return repo;
	}

	@Override
	protected void reset(int retainFlags) {
		headInitialized = false;
		head = null;
		super.reset(retainFlags);
	}

	@Override
	public RevCommit next() throws MissingObjectException,
			IncorrectObjectTypeException, IOException {
		if (!headInitialized) {
			head = determineHead();
			headInitialized = true;
		}
		return super.next();
	}

	/**
	 * Retrieves the HEAD ref if it is symbolic.
	 *
	 * @return the Ref, or {@code null} if HEAD is not a symbolic Ref
	 * @throws IOException
	 *             if the head cannot be read
	 */
	public Ref getHead() throws IOException {
		if (!headInitialized) {
			head = determineHead();
			headInitialized = true;
		}
		return head;
	}

	@Override
	protected RevCommit createCommit(final AnyObjectId id) {
		return new SWTCommit(id, this);
	}

	private Ref determineHead() throws IOException {
		Ref h = repo.exactRef(Constants.HEAD);
		if (h != null && h.isSymbolic()) {
			return h;
		}
		return null;
	}
}

Back to the top