Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0ff0f3b3feafa768d7b193495486ad352613d5b2 (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
/*******************************************************************************
 * Copyright (C) 2010, Stefan Lay <stefan.lay@sap.com>
 *
 * 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
 *******************************************************************************/

package org.eclipse.egit.core.test.op;

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

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.egit.core.op.AddToIndexOperation;
import org.eclipse.egit.core.op.CommitOperation;
import org.eclipse.egit.core.test.GitTestCase;
import org.eclipse.egit.core.test.TestRepository;
import org.eclipse.egit.core.test.TestUtils;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.treewalk.TreeWalk;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class CommitOperationTest extends GitTestCase {

	private static List<IFile> EMPTY_FILE_LIST = new ArrayList<IFile>();

	private List<IResource> resources = new ArrayList<IResource>();

	TestRepository testRepository;

	Repository repository;

	@Override
	@Before
	public void setUp() throws Exception {
		super.setUp();
		gitDir = new File(project.getProject()
				.getLocationURI().getPath(), Constants.DOT_GIT);
		testRepository = new TestRepository(gitDir);
		repository = testRepository.getRepository();
		testRepository.connect(project.getProject());
	}

	@Override
	@After
	public void tearDown() throws Exception {
		testRepository.dispose();
		repository = null;
		super.tearDown();
	}

	@Test
	public void testCommitAddedToIndexDeletedInWorkspace() throws Exception {
		testUtils.addFileToProject(project.getProject(), "foo/a.txt", "some text");
		resources.add(project.getProject().getFolder("foo"));
		new AddToIndexOperation(resources).execute(null);
		CommitOperation commitOperation = new CommitOperation(null, null, TestUtils.AUTHOR, TestUtils.COMMITTER, "first commit");
		commitOperation.setCommitAll(true);
		commitOperation.setRepository(repository);
		commitOperation.execute(null);

		testUtils.addFileToProject(project.getProject(), "zar/b.txt", "some text");
		resources.add(project.getProject().getFolder("zar"));
		new AddToIndexOperation(resources).execute(null);
		IFile zarFile = project.getProject().getFile("zar/b.txt");
		IPath zarFilePath = zarFile.getLocation();
		// delete file and refresh. Deleting using the resource would trigger
		// GitMoveDeleteHook which removes the file from the index
		assertTrue("could not delete file " + zarFilePath.toOSString(),
				zarFilePath.toFile().delete());
		zarFile.refreshLocal(0, null);

		assertFalse(project.getProject().getFile("zar/b.txt").exists());

		IFile[] filesToCommit = new IFile[] { project.getProject().getFile("zar/b.txt") };
		commitOperation = new CommitOperation(filesToCommit, null, TestUtils.AUTHOR, TestUtils.COMMITTER, "first commit");
		commitOperation.setRepository(repository);
		try {
			commitOperation.execute(null);
			// TODO this is very ugly. CommitCommand should be extended
			// not to throw an JGitInternalException in case of an empty
			// commit
			fail("expected CoreException");
		} catch (CoreException e) {
			assertEquals("No changes", e.getCause().getMessage());
		}

		try (TreeWalk treeWalk = new TreeWalk(repository)) {
			treeWalk.addTree(repository.resolve("HEAD^{tree}"));
			assertTrue(treeWalk.next());
			assertEquals("foo", treeWalk.getPathString());
			treeWalk.enterSubtree();
			assertTrue(treeWalk.next());
			assertEquals("foo/a.txt", treeWalk.getPathString());
			assertFalse(treeWalk.next());
		}
	}

	@Test
	public void testCommitAll() throws Exception {
		IFile file1 = testUtils.addFileToProject(project.getProject(),
				"sub/a.txt", "some text");
		testUtils.addFileToProject(project.getProject(),
				"sub/b.txt", "some text");

		resources.add(project.getProject().getFolder("sub"));
		new AddToIndexOperation(resources).execute(null);
		CommitOperation commitOperation = new CommitOperation(null, null, TestUtils.AUTHOR,
				TestUtils.COMMITTER, "first commit");
		commitOperation.setCommitAll(true);
		commitOperation.setRepository(repository);
		commitOperation.execute(null);

		Iterator<RevCommit> commits;
		try (Git git = new Git(repository)) {
			commits = git.log().call().iterator();
		}
		RevCommit firstCommit = commits.next();
		assertTrue(firstCommit.getCommitTime() > 0);

		assertEquals("first commit", firstCommit.getFullMessage());

		testUtils.changeContentOfFile(project.getProject(), file1, "changed text");

		commitOperation = new CommitOperation(null, null, TestUtils.AUTHOR,
				TestUtils.COMMITTER, "second commit");
		commitOperation.setCommitAll(true);
		commitOperation.setRepository(repository);
		commitOperation.execute(null);

		try (Git git = new Git(repository)) {
			commits = git.log().call().iterator();
		}
		RevCommit secondCommit = commits.next();
		assertTrue(secondCommit.getCommitTime() > 0);

		assertEquals("second commit", secondCommit.getFullMessage());
		secondCommit.getParent(0).equals(firstCommit);
		assertEquals("The Author", secondCommit.getAuthorIdent().getName());
		assertEquals("The.author@some.com", secondCommit.getAuthorIdent().getEmailAddress());
		assertEquals("The Commiter", secondCommit.getCommitterIdent().getName());
		assertEquals("The.committer@some.com", secondCommit.getCommitterIdent().getEmailAddress());
	}

	@Test
	public void testCommitEmptiedTree() throws Exception {
		// Set up a directory structure
		testUtils.addFileToProject(project.getProject(),
				"sub1/a.txt", "some text");
		testUtils.addFileToProject(project.getProject(),
				"sub2/b.txt", "some text");
		resources.add(project.getProject().getFolder("sub1"));
		resources.add(project.getProject().getFolder("sub2"));
		new AddToIndexOperation(resources).execute(null);
		CommitOperation commitOperation = new CommitOperation(null, null, TestUtils.AUTHOR,
				TestUtils.COMMITTER, "first commit");
		commitOperation.setCommitAll(true);
		commitOperation.setRepository(repository);
		commitOperation.execute(null);

		Iterator<RevCommit> commits;
		try (Git git = new Git(repository)) {
			commits = git.log().call().iterator();
		}
		RevCommit secondCommit = commits.next();
		try (TreeWalk treeWalk = new TreeWalk(repository)) {
			treeWalk.addTree(secondCommit.getTree().getId());
			treeWalk.setRecursive(true);
			treeWalk.setPostOrderTraversal(true);
			assertTrue(treeWalk.next());
			assertEquals("sub1/a.txt", treeWalk.getPathString());
			assertTrue(treeWalk.next());
			assertEquals("sub1", treeWalk.getPathString());
			assertTrue(treeWalk.next());
			assertEquals("sub2/b.txt", treeWalk.getPathString());
			assertTrue(treeWalk.next());
			assertEquals("sub2", treeWalk.getPathString());
			assertFalse(treeWalk.next());
		}
		project.getProject().getFolder("sub2").delete(IResource.FORCE, null);
		IFile[] filesToCommit = { project.getProject().getFile("sub2/b.txt") };
		ArrayList<IFile> notIndexed = new ArrayList<IFile>();
		notIndexed.add(filesToCommit[0]);
		ArrayList<IFile> notTracked = new ArrayList<IFile>();
		commitOperation = new CommitOperation(filesToCommit, notTracked, TestUtils.AUTHOR, TestUtils.COMMITTER, "second commit");
		commitOperation.setCommitAll(false);
		commitOperation.execute(null);

		try (Git git = new Git(repository)) {
			commits = git.log().call().iterator();
		}
		secondCommit = commits.next();
		try (TreeWalk treeWalk = new TreeWalk(repository)) {
			treeWalk.addTree(secondCommit.getTree().getId());
			treeWalk.setRecursive(true);
			treeWalk.setPostOrderTraversal(true);
			assertTrue(treeWalk.next());
			assertEquals("sub1/a.txt", treeWalk.getPathString());
			assertTrue(treeWalk.next());
			assertEquals("sub1", treeWalk.getPathString());
			assertFalse(treeWalk.next());
		}
	}

	@Test
	public void testCommitUntracked() throws Exception {
		IFile fileA = testUtils.addFileToProject(project.getProject(),
				"foo/a.txt", "some text");
		IFile fileB = testUtils.addFileToProject(project.getProject(),
				"foo/b.txt", "some text");
		testUtils.addFileToProject(project.getProject(), "foo/c.txt",
				"some text");
		IFile[] filesToCommit = { fileA, fileB };
		CommitOperation commitOperation = new CommitOperation(filesToCommit,
				Arrays.asList(filesToCommit), TestUtils.AUTHOR,
				TestUtils.COMMITTER, "first commit");
		commitOperation.execute(null);
		testUtils.assertRepositoryContainsFiles(repository, getRepoRelativePaths(filesToCommit));
	}

	private String[] getRepoRelativePaths(IFile[] files) {
		ArrayList<String> result = new ArrayList<String>();
		for (IFile file:files)
			result.add(file.getProjectRelativePath().toString());
		return result.toArray(new String[result.size()]);
	}

	@Test
	public void testCommitStaged() throws Exception {
		IFile fileA = testUtils.addFileToProject(project.getProject(),
				"foo/a.txt", "some text");
		IFile fileB = testUtils.addFileToProject(project.getProject(),
				"foo/b.txt", "some text");
		IFile[] filesToCommit = { fileA, fileB };
		CommitOperation commitOperation = new CommitOperation(filesToCommit,
				Arrays.asList(filesToCommit), TestUtils.AUTHOR,
				TestUtils.COMMITTER, "first commit");
		commitOperation.execute(null);
		testUtils.changeContentOfFile(project.getProject(), fileA,
				"new content of A");
		testUtils.changeContentOfFile(project.getProject(), fileB,
				"new content of B");
		resources.add(fileA);
		resources.add(fileB);
		new AddToIndexOperation(resources).execute(null);
		commitOperation = new CommitOperation(filesToCommit, EMPTY_FILE_LIST,
				TestUtils.AUTHOR, TestUtils.COMMITTER, "second commit");
		commitOperation.execute(null);

		testUtils.assertRepositoryContainsFilesWithContent(repository,
				"foo/a.txt", "new content of A", "foo/b.txt",
				"new content of B");
	}

	@Test
	public void testCommitIndexSubset() throws Exception {
		IFile fileA = testUtils.addFileToProject(project.getProject(),
				"foo/a.txt", "some text");
		IFile fileB = testUtils.addFileToProject(project.getProject(),
				"foo/b.txt", "some text");
		IFile[] filesToCommit = { fileA, fileB };
		CommitOperation commitOperation = new CommitOperation(filesToCommit,
				Arrays.asList(filesToCommit), TestUtils.AUTHOR,
				TestUtils.COMMITTER, "first commit");
		commitOperation.execute(null);
		testUtils.changeContentOfFile(project.getProject(), fileA,
				"new content of A");
		testUtils.changeContentOfFile(project.getProject(), fileB,
				"new content of B");
		resources.add(fileA);
		resources.add(fileB);
		new AddToIndexOperation(resources).execute(null);
		IFile[] filesToCommit2 = { fileA };
		commitOperation = new CommitOperation(filesToCommit2, EMPTY_FILE_LIST,
				TestUtils.AUTHOR, TestUtils.COMMITTER, "second commit");
		commitOperation.execute(null);

		testUtils.assertRepositoryContainsFilesWithContent(repository,
				"foo/a.txt", "new content of A", "foo/b.txt", "some text");
	}

	@Test
	public void testCommitWithStaging() throws Exception {
		IFile fileA = testUtils.addFileToProject(project.getProject(),
				"foo/a.txt", "some text");
		IFile fileB = testUtils.addFileToProject(project.getProject(),
				"foo/b.txt", "some text");
		IFile[] filesToCommit = { fileA, fileB };
		CommitOperation commitOperation = new CommitOperation(filesToCommit,
				Arrays.asList(filesToCommit), TestUtils.AUTHOR,
				TestUtils.COMMITTER, "first commit");
		commitOperation.execute(null);

		testUtils.changeContentOfFile(project.getProject(), fileA,
				"new content of A");
		testUtils.changeContentOfFile(project.getProject(), fileB,
				"new content of B");
		resources.add(fileA);
		resources.add(fileB);
		commitOperation = new CommitOperation(filesToCommit,
				EMPTY_FILE_LIST, TestUtils.AUTHOR,
				TestUtils.COMMITTER, "first commit");
		commitOperation.execute(null);

		testUtils.assertRepositoryContainsFilesWithContent(repository,
				"foo/a.txt", "new content of A", "foo/b.txt",
				"new content of B");
	}

}

Back to the top