Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 830a09adf2701f0826ef6d8e10ae563776e79045 (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
/*******************************************************************************
 * Copyright (C) 2010, Mathias Kinzler <mathias.kinzler@sap.com>
 *
 * 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.test.op;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.ByteArrayInputStream;
import java.io.File;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.egit.core.op.RebaseOperation;
import org.eclipse.egit.core.test.GitTestCase;
import org.eclipse.egit.core.test.TestRepository;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.RebaseResult;
import org.eclipse.jgit.api.RebaseCommand.Operation;
import org.eclipse.jgit.api.RebaseResult.Status;
import org.eclipse.jgit.api.errors.WrongRepositoryStateException;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevWalk;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

public class RebaseOperationTest extends GitTestCase {

	private static final String TOPIC = Constants.R_HEADS + "topic";

	private static final String MASTER = Constants.R_HEADS + "master";

	TestRepository testRepository;

	Repository repository;

	Git git;

	@Override
	@Before
	public void setUp() throws Exception {
		super.setUp();
		testRepository = new TestRepository(gitDir);
		repository = testRepository.getRepository();
		// create first commit containing a dummy file
		testRepository
				.createInitialCommit("testRebaseOperation\n\nfirst commit\n");
		git = new Git(repository);
	}

	@Test
	@Ignore
	// currently not working as expected; see also TODO in RebaseCommand
	public void testUpToDate() throws Exception {
		IFile file = project.createFile("theFile.txt", "Hello, world"
				.getBytes("UTF-8"));
		// first commit in master: add theFile.txt
		RevCommit first = testRepository.addAndCommit(project.project,
				new File(file.getLocationURI()), "Adding theFile.txt");

		testRepository.createBranch(MASTER, TOPIC);

		// checkout topic
		testRepository.checkoutBranch(TOPIC);

		file = project.createFile("theSecondFile.txt", "Hello, world"
				.getBytes("UTF-8"));
		// topic commit: add second file
		RevCommit topicCommit = testRepository.addAndCommit(project.project,
				new File(file.getLocationURI()), "Adding theSecondFile.txt");

		// parent of topic commit should be first master commit before rebase
		assertEquals(first, topicCommit.getParent(0));

		// rebase topic onto master
		RebaseOperation op = new RebaseOperation(testRepository.getRepository(),
				testRepository.getRepository().exactRef(MASTER));
		op.execute(null);

		RebaseResult res = op.getResult();
		assertEquals(RebaseResult.Status.UP_TO_DATE, res.getStatus());

		try (RevWalk rw = new RevWalk(repository)) {
			RevCommit newTopic = rw.parseCommit(repository.resolve(TOPIC));
			assertEquals(topicCommit, newTopic);
			assertEquals(first, newTopic.getParent(0));
		}
	}

	@Test
	public void testNoConflict() throws Exception {
		IFile file = project.createFile("theFile.txt", "Hello, world"
				.getBytes("UTF-8"));
		// first commit in master: add theFile.txt
		RevCommit first = testRepository.addAndCommit(project.project,
				new File(file.getLocationURI()), "Adding theFile.txt");

		testRepository.createBranch(MASTER, TOPIC);

		file.setContents(new ByteArrayInputStream("master".getBytes("UTF-8")),
				0, null);
		// second commit in master: modify theFile.txt
		RevCommit second = git.commit().setAll(true).setMessage(
				"Modify theFile.txt").call();
		assertEquals(first, second.getParent(0));

		// checkout topic
		testRepository.checkoutBranch(TOPIC);

		file = project.createFile("theSecondFile.txt", "Hello, world"
				.getBytes("UTF-8"));
		// topic commit: add second file
		RevCommit topicCommit = testRepository.addAndCommit(project.project,
				new File(file.getLocationURI()), "Adding theSecondFile.txt");

		// parent of topic commit should be first master commit before rebase
		assertEquals(first, topicCommit.getParent(0));

		// rebase topic onto master
		RebaseOperation op = new RebaseOperation(testRepository.getRepository(),
				testRepository.getRepository().exactRef(MASTER));
		op.execute(null);

		RebaseResult res = op.getResult();
		assertEquals(RebaseResult.Status.OK, res.getStatus());

		try (RevWalk rw = new RevWalk(repository)) {
			RevCommit newTopic = rw.parseCommit(repository.resolve(TOPIC));
			assertEquals(second, newTopic.getParent(0));
		}
	}

	@Test
	public void testStopAndAbortOnConflict() throws Exception {
		IFile file = project.createFile("theFile.txt", "Hello, world"
				.getBytes("UTF-8"));
		// first commit in master: add theFile.txt
		RevCommit first = testRepository.addAndCommit(project.project,
				new File(file.getLocationURI()), "Adding theFile.txt");

		testRepository.createBranch(MASTER, TOPIC);

		file.setContents(new ByteArrayInputStream("master".getBytes("UTF-8")),
				0, null);
		// second commit in master: modify theFile.txt
		RevCommit second = git.commit().setAll(true).setMessage(
				"Modify theFile.txt").call();
		assertEquals(first, second.getParent(0));

		// checkout topic
		testRepository.checkoutBranch(TOPIC);

		// set conflicting content in topic
		file.setContents(new ByteArrayInputStream("topic".getBytes("UTF-8")),
				0, null);
		// topic commit: add second file
		RevCommit topicCommit = testRepository.addAndCommit(project.project,
				new File(file.getLocationURI()), "Changing theFile.txt again");

		// parent of topic commit should be first master commit before rebase
		assertEquals(first, topicCommit.getParent(0));

		// rebase topic onto master
		RebaseOperation op = new RebaseOperation(testRepository.getRepository(),
				testRepository.getRepository().exactRef(MASTER));
		op.execute(null);

		RebaseResult res = op.getResult();
		assertEquals(RebaseResult.Status.STOPPED, res.getStatus());

		// let's try to abort this here
		RebaseOperation abort = new RebaseOperation(repository, Operation.ABORT);
		abort.execute(null);
		RebaseResult abortResult = abort.getResult();
		assertEquals(Status.ABORTED, abortResult.getStatus());

		assertEquals(topicCommit, repository.resolve(Constants.HEAD));
	}

	@Test
	public void testExceptionWhenRestartingStoppedRebase() throws Exception {
		IFile file = project.createFile("theFile.txt", "Hello, world"
				.getBytes("UTF-8"));
		// first commit in master: add theFile.txt
		RevCommit first = testRepository.addAndCommit(project.project,
				new File(file.getLocationURI()), "Adding theFile.txt");

		testRepository.createBranch(MASTER, TOPIC);

		file.setContents(new ByteArrayInputStream("master".getBytes("UTF-8")),
				0, null);
		// second commit in master: modify theFile.txt
		RevCommit second = git.commit().setAll(true).setMessage(
				"Modify theFile.txt").call();
		assertEquals(first, second.getParent(0));

		// checkout topic
		testRepository.checkoutBranch(TOPIC);

		// set conflicting content in topic
		file.setContents(new ByteArrayInputStream("topic".getBytes("UTF-8")),
				0, null);
		// topic commit: add second file
		RevCommit topicCommit = testRepository.addAndCommit(project.project,
				new File(file.getLocationURI()), "Changing theFile.txt again");

		// parent of topic commit should be first master commit before rebase
		assertEquals(first, topicCommit.getParent(0));

		// rebase topic onto master
		RebaseOperation op = new RebaseOperation(repository,
				repository.exactRef(MASTER));
		op.execute(null);

		RebaseResult res = op.getResult();
		assertEquals(RebaseResult.Status.STOPPED, res.getStatus());

		try {
			// let's try to start again, we should get a wrapped
			// WrongRepositoryStateException
			op = new RebaseOperation(repository, repository.exactRef(MASTER));
			op.execute(null);
			fail("Expected Exception not thrown");
		} catch (CoreException e) {
			Throwable cause = e.getCause();
			assertTrue(cause instanceof WrongRepositoryStateException);
		}
	}
}

Back to the top