Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a6bef45228089675744d275fb26081bfec3b2508 (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
/*******************************************************************************
 * Copyright (C) 2007, Dave Watson <dwatson@mimvista.com>
 * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
 * Copyright (C) 2008, 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.egit.core.CoreText;
import org.eclipse.jgit.lib.Commit;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.GitIndex;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.RefUpdate;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.Tag;
import org.eclipse.jgit.lib.Tree;
import org.eclipse.jgit.lib.WorkDirCheckout;
import org.eclipse.osgi.util.NLS;
import org.eclipse.team.core.TeamException;

/**
 * A class for changing a ref and possibly index and workdir too.
 */
public class ResetOperation implements IWorkspaceRunnable {
	/**
	 * Kind of reset
	 */
	public enum ResetType {
		/**
		 * Just change the ref. The index and workdir are not changed.
		 */
		SOFT,

		/**
		 * Change the ref and the index. The workdir is not changed.
		 */
		MIXED,

		/**
		 * Change the ref, the index and the workdir
		 */
		HARD
	}

	private final Repository repository;
	private final String refName;
	private final ResetType type;

	private Commit commit;
	private Tree newTree;
	private GitIndex index;

	/**
	 * Construct a {@link ResetOperation}
	 *
	 * @param repository
	 * @param refName
	 * @param type
	 */
	public ResetOperation(Repository repository, String refName, ResetType type) {
		this.repository = repository;
		this.refName = refName;
		this.type = type;
	}

	public void run(IProgressMonitor monitor) throws CoreException {
		monitor.beginTask(NLS.bind(CoreText.ResetOperation_performingReset,
				type.toString().toLowerCase(), refName), 7);

		mapObjects();
		monitor.worked(1);

		writeRef();
		monitor.worked(1);

		if (type != ResetType.SOFT) {
			if (type == ResetType.MIXED)
				resetIndex();
			else
				readIndex();
			writeIndex();
		}
		monitor.worked(1);

		if (type == ResetType.HARD) {
			checkoutIndex();
		}
		monitor.worked(1);

		if (type != ResetType.SOFT) {
			refreshIndex();
		}
		monitor.worked(1);

		monitor.worked(1);

		refreshProjects();

		monitor.done();
	}

	private void refreshIndex() throws TeamException {
//		File workdir = repository.getDirectory().getParentFile();
//		for (Entry e : newIndex.getMembers()) {
//			try {
//				e.update(new File(workdir, e.getName()));
//			} catch (IOException ignore) {}
//		}
		try {
			index.write();
		} catch (IOException e1) {
			throw new TeamException(CoreText.ResetOperation_writingIndex, e1);
		}
	}

	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);  //$NON-NLS-1$
					p.refreshLocal(IResource.DEPTH_INFINITE, null);
				} catch (CoreException e) {
					e.printStackTrace();
				}
			}
		}
	}

	private void mapObjects() throws TeamException {
		final ObjectId commitId;
		try {
			commitId = repository.resolve(refName);
		} catch (IOException e) {
			throw new TeamException(NLS.bind(
					CoreText.ResetOperation_lookingUpRef, refName), e);
		}
		try {
			commit = repository.mapCommit(commitId);
		} catch (IOException e) {
			try {
				Tag t = repository.mapTag(refName, commitId);
				commit = repository.mapCommit(t.getObjId());
			} catch (IOException e2) {
				throw new TeamException(NLS.bind(
						CoreText.ResetOperation_lookingUpCommit, commitId), e2);
			}
		}

	}

	private void writeRef() throws TeamException {
		try {
			final RefUpdate ru = repository.updateRef(Constants.HEAD);
			ru.setNewObjectId(commit.getCommitId());
			String name = refName;
			if (name.startsWith("refs/heads/"))  //$NON-NLS-1$
				name = name.substring(11);
			if (name.startsWith("refs/remotes/"))  //$NON-NLS-1$
				name = name.substring(13);
			String message = "reset --" //$NON-NLS-1$
					+ type.toString().toLowerCase() + " " + name; //$NON-NLS-1$
			ru.setRefLogMessage(message, false);
			if (ru.forceUpdate() == RefUpdate.Result.LOCK_FAILURE)
				throw new TeamException(NLS.bind(
						CoreText.ResetOperation_cantUpdate, ru.getName()));
		} catch (IOException e) {
			throw new TeamException(NLS.bind(
					CoreText.ResetOperation_updatingFailed, Constants.HEAD), e);
		}
	}

	private void readIndex() throws TeamException {
		try {
			newTree = commit.getTree();
			index = repository.getIndex();
		} catch (IOException e) {
			throw new TeamException(CoreText.ResetOperation_readingIndex, e);
		}
	}

	private void resetIndex() throws TeamException {
		try {
			newTree = commit.getTree();
			index = repository.getIndex();
			index.readTree(newTree);
		} catch (IOException e) {
			throw new TeamException(CoreText.ResetOperation_readingIndex, e);
		}
	}

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

	private void checkoutIndex() throws TeamException {
		final File parentFile = repository.getWorkDir();
		try {
			WorkDirCheckout workDirCheckout =
				new WorkDirCheckout(repository, parentFile, index, newTree);
			workDirCheckout.setFailOnConflict(false);
			workDirCheckout.checkout();
		} catch (IOException e) {
			throw new TeamException(
					CoreText.ResetOperation_mappingTreeForCommit, e);
		}
	}
}

Back to the top