1
2
3
4
5
6
7
8
9
10 package org.eclipse.jgit.pgm;
11
12 import static org.junit.Assert.assertThrows;
13 import static org.junit.Assert.assertTrue;
14
15 import org.eclipse.jgit.api.Git;
16 import org.eclipse.jgit.api.MergeResult;
17 import org.eclipse.jgit.lib.CLIRepositoryTestCase;
18 import org.eclipse.jgit.lib.Constants;
19 import org.eclipse.jgit.lib.PersonIdent;
20 import org.eclipse.jgit.revwalk.RevCommit;
21 import org.junit.Test;
22
23 public class BlameTest extends CLIRepositoryTestCase {
24
25 @Test
26 public void testBlameNoHead() throws Exception {
27 try (Git git = new Git(db)) {
28 writeTrashFile("inIndex.txt", "index");
29 git.add().addFilepattern("inIndex.txt").call();
30 }
31 assertThrows("no such ref: HEAD", Die.class,
32 () -> execute("git blame inIndex.txt"));
33 }
34
35 @Test
36 public void testBlameCommitted() throws Exception {
37 try (Git git = new Git(db)) {
38 git.commit().setMessage("initial commit").call();
39 writeTrashFile("committed.txt", "committed");
40 git.add().addFilepattern("committed.txt").call();
41 git.commit().setMessage("commit").call();
42 }
43 assertStringArrayEquals(
44 "1ad3399c (GIT_COMMITTER_NAME 2009-08-15 20:12:58 -0330 1) committed",
45 execute("git blame committed.txt"));
46 }
47
48 @Test
49 public void testBlameStaged() throws Exception {
50 try (Git git = new Git(db)) {
51 git.commit().setMessage("initial commit").call();
52 writeTrashFile("inIndex.txt", "index");
53 git.add().addFilepattern("inIndex.txt").call();
54 }
55 assertStringArrayEquals(
56 "00000000 (Not Committed Yet 2009-08-15 20:12:58 -0330 1) index",
57 execute("git blame inIndex.txt"));
58 }
59
60 @Test
61 public void testBlameUnstaged() throws Exception {
62 try (Git git = new Git(db)) {
63 git.commit().setMessage("initial commit").call();
64 }
65 writeTrashFile("onlyInWorkingTree.txt", "not in repo");
66 assertThrows("no such path 'onlyInWorkingTree.txt' in HEAD", Die.class,
67 () -> execute("git blame onlyInWorkingTree.txt"));
68 }
69
70 @Test
71 public void testBlameNonExisting() throws Exception {
72 try (Git git = new Git(db)) {
73 git.commit().setMessage("initial commit").call();
74 }
75 assertThrows("no such path 'does_not_exist.txt' in HEAD", Die.class,
76 () -> execute("git blame does_not_exist.txt"));
77 }
78
79 @Test
80 public void testBlameNonExistingInSubdir() throws Exception {
81 try (Git git = new Git(db)) {
82 git.commit().setMessage("initial commit").call();
83 }
84 assertThrows("no such path 'sub/does_not_exist.txt' in HEAD", Die.class,
85 () -> execute("git blame sub/does_not_exist.txt"));
86 }
87
88 @Test
89 public void testBlameMergeConflict() throws Exception {
90 try (Git git = new Git(db)) {
91 writeTrashFile("file", "Origin\n");
92 git.add().addFilepattern("file").call();
93 git.commit().setMessage("initial commit").call();
94 git.checkout().setCreateBranch(true)
95 .setName("side").call();
96 writeTrashFile("file",
97 "Conflicting change from side branch\n");
98 git.add().addFilepattern("file").call();
99 RevCommit side = git.commit().setMessage("side commit")
100 .setCommitter(new PersonIdent("gitter", "")).call();
101 git.checkout().setName(Constants.MASTER).call();
102 writeTrashFile("file", "Change on master branch\n");
103 git.add().addFilepattern("file").call();
104 git.commit().setMessage("Commit conflict on master")
105 .setCommitter(new PersonIdent("gitter", "")).call();
106 MergeResult result = git.merge()
107 .include("side", side).call();
108 assertTrue("Expected conflict on 'file'",
109 result.getConflicts().containsKey("file"));
110 }
111 String[] expected = {
112 "00000000 (Not Committed Yet 2009-08-15 20:12:58 -0330 1) <<<<<<< HEAD",
113 "0f5b671c (gitter 2009-08-15 20:12:58 -0330 2) Change on master branch",
114 "00000000 (Not Committed Yet 2009-08-15 20:12:58 -0330 3) =======",
115 "ae78cff6 (gitter 2009-08-15 20:12:58 -0330 4) Conflicting change from side branch",
116 "00000000 (Not Committed Yet 2009-08-15 20:12:58 -0330 5) >>>>>>> side" };
117 assertArrayOfLinesEquals(expected, execute("git blame file"));
118 }
119 }