aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/BlameTest.java
blob: ea304335835ee850db8294fc53f1597f81214789 (plain)
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
/*
 * Copyright (C) 2019 Thomas Wolf <thomas.wolf@paranor.ch> and others
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Distribution License v. 1.0 which is available at
 * https://www.eclipse.org/org/documents/edl-v10.php.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */
package org.eclipse.jgit.pgm;

import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;

import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.MergeResult;
import org.eclipse.jgit.lib.CLIRepositoryTestCase;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.revwalk.RevCommit;
import org.junit.Test;

public class BlameTest extends CLIRepositoryTestCase {

	@Test
	public void testBlameNoHead() throws Exception {
		try (Git git = new Git(db)) {
			writeTrashFile("inIndex.txt", "index");
			git.add().addFilepattern("inIndex.txt").call();
		}
		assertThrows("no such ref: HEAD", Die.class,
				() -> execute("git blame inIndex.txt"));
	}

	@Test
	public void testBlameCommitted() throws Exception {
		try (Git git = new Git(db)) {
			git.commit().setMessage("initial commit").call();
			writeTrashFile("committed.txt", "committed");
			git.add().addFilepattern("committed.txt").call();
			git.commit().setMessage("commit").call();
		}
		assertStringArrayEquals(
				"1ad3399c (GIT_COMMITTER_NAME 2009-08-15 20:12:58 -0330 1) committed",
				execute("git blame committed.txt"));
	}

	@Test
	public void testBlameStaged() throws Exception {
		try (Git git = new Git(db)) {
			git.commit().setMessage("initial commit").call();
			writeTrashFile("inIndex.txt", "index");
			git.add().addFilepattern("inIndex.txt").call();
		}
		assertStringArrayEquals(
				"00000000 (Not Committed Yet 2009-08-15 20:12:58 -0330 1) index",
				execute("git blame inIndex.txt"));
	}

	@Test
	public void testBlameUnstaged() throws Exception {
		try (Git git = new Git(db)) {
			git.commit().setMessage("initial commit").call();
		}
		writeTrashFile("onlyInWorkingTree.txt", "not in repo");
		assertThrows("no such path 'onlyInWorkingTree.txt' in HEAD", Die.class,
				() -> execute("git blame onlyInWorkingTree.txt"));
	}

	@Test
	public void testBlameNonExisting() throws Exception {
		try (Git git = new Git(db)) {
			git.commit().setMessage("initial commit").call();
		}
		assertThrows("no such path 'does_not_exist.txt' in HEAD", Die.class,
				() -> execute("git blame does_not_exist.txt"));
	}

	@Test
	public void testBlameNonExistingInSubdir() throws Exception {
		try (Git git = new Git(db)) {
			git.commit().setMessage("initial commit").call();
		}
		assertThrows("no such path 'sub/does_not_exist.txt' in HEAD", Die.class,
				() -> execute("git blame sub/does_not_exist.txt"));
	}

	@Test
	public void testBlameMergeConflict() throws Exception {
		try (Git git = new Git(db)) {
			writeTrashFile("file", "Origin\n");
			git.add().addFilepattern("file").call();
			git.commit().setMessage("initial commit").call();
			git.checkout().setCreateBranch(true)
					.setName("side").call();
			writeTrashFile("file",
					"Conflicting change from side branch\n");
			git.add().addFilepattern("file").call();
			RevCommit side = git.commit().setMessage("side commit")
					.setCommitter(new PersonIdent("gitter", "")).call();
			git.checkout().setName(Constants.MASTER).call();
			writeTrashFile("file", "Change on master branch\n");
			git.add().addFilepattern("file").call();
			git.commit().setMessage("Commit conflict on master")
					.setCommitter(new PersonIdent("gitter", "")).call();
			MergeResult result = git.merge()
					.include("side", side).call();
			assertTrue("Expected conflict on 'file'",
					result.getConflicts().containsKey("file"));
		}
		String[] expected = {
				"00000000 (Not Committed Yet 2009-08-15 20:12:58 -0330 1) <<<<<<< HEAD",
				"0f5b671c (gitter            2009-08-15 20:12:58 -0330 2) Change on master branch",
				"00000000 (Not Committed Yet 2009-08-15 20:12:58 -0330 3) =======",
				"ae78cff6 (gitter            2009-08-15 20:12:58 -0330 4) Conflicting change from side branch",
				"00000000 (Not Committed Yet 2009-08-15 20:12:58 -0330 5) >>>>>>> side" };
		assertArrayOfLinesEquals(expected, execute("git blame file"));
	}
}