Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6ee9a552814250fffa3f744b31235aefc97e50b8 (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
/*******************************************************************************
 * Copyright (C) 2007, Dave Watson <dwatson@mimvista.com>
 * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
 * Copyright (C) 2006, 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 v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/
package org.eclipse.egit.core.op;

import java.io.File;
import java.io.IOException;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRunnable;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.team.core.TeamException;
import org.eclipse.jgit.errors.CheckoutConflictException;
import org.eclipse.jgit.lib.Commit;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.GitIndex;
import org.eclipse.jgit.lib.RefLogWriter;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.Tree;
import org.eclipse.jgit.lib.WorkDirCheckout;

/**
 * This class implements checkouts of a specific revision. A check
 * is made that this can be done without data loss.
 */
public class BranchOperation implements IWorkspaceRunnable {

	private final Repository repository;

	private final String refName;

	/**
	 * Construct a {@link BranchOperation} object.
	 * @param repository
	 * @param refName Name of git ref to checkout
	 */
	public BranchOperation(Repository repository, String refName) {
		this.repository = repository;
		this.refName = refName;
	}

	private Tree oldTree;

	private GitIndex index;

	private Tree newTree;

	private Commit oldCommit;

	private Commit newCommit;



	public void run(IProgressMonitor monitor) throws CoreException {
		lookupRefs();
		monitor.worked(1);

		mapObjects();
		monitor.worked(1);

		checkoutTree();
		monitor.worked(1);

		writeIndex();
		monitor.worked(1);

		updateHeadRef();
		monitor.worked(1);

		writeHeadReflog();
		monitor.worked(1);

		refreshProjects();
		monitor.worked(1);

		monitor.done();
	}

	private void refreshProjects() {
		final IProject[] projects = ResourcesPlugin.getWorkspace().getRoot()
				.getProjects();
		final File parentFile = repository.getWorkDir();
		for (IProject p : projects) {
			final File file = p.getLocation().toFile();
			if (file.getAbsolutePath().startsWith(parentFile.getAbsolutePath())) {
				try {
					System.out.println("Refreshing " + p);
					p.refreshLocal(IResource.DEPTH_INFINITE, null);
				} catch (CoreException e) {
					e.printStackTrace();
				}
			}
		}
	}

	private void writeHeadReflog() throws TeamException {
		try {
			RefLogWriter.writeReflog(repository, oldCommit.getCommitId(),
					newCommit.getCommitId(), "checkout: moving to " + refName,
					Constants.HEAD);
		} catch (IOException e) {
			throw new TeamException("Writing HEAD's reflog", e);
		}
	}

	private void updateHeadRef() throws TeamException {
		try {
			repository.writeSymref(Constants.HEAD, refName);
		} catch (IOException e) {
			throw new TeamException("Updating HEAD to ref: " + refName, e);
		}
	}

	private void writeIndex() throws TeamException {
		try {
			index.write();
		} catch (IOException e) {
			throw new TeamException("Writing index", e);
		}
	}

	private void checkoutTree() throws TeamException {
		try {
			new WorkDirCheckout(repository, repository.getWorkDir(), oldTree,
					index, newTree).checkout();
		} catch (CheckoutConflictException e) {
			TeamException teamException = new TeamException(e.getMessage());
			throw teamException;
		} catch (IOException e) {
			throw new TeamException("Problem while checking out:", e);
		}
	}

	private void mapObjects() throws TeamException {
		try {
			oldTree = oldCommit.getTree();
			index = repository.getIndex();
			newTree = newCommit.getTree();
		} catch (IOException e) {
			throw new TeamException("Mapping trees", e);
		}
	}

	private void lookupRefs() throws TeamException {
		try {
			newCommit = repository.mapCommit(refName);
		} catch (IOException e) {
			throw new TeamException("Mapping commit: " + refName, e);
		}

		try {
			oldCommit = repository.mapCommit(Constants.HEAD);
		} catch (IOException e) {
			throw new TeamException("Mapping commit HEAD commit", e);
		}
	}

}

Back to the top