1// Copyright 2018 The Gitea Authors. All rights reserved.
2// Use of this source code is governed by a MIT-style
3// license that can be found in the LICENSE file.
4
5package git
6
7import (
8	"path/filepath"
9	"testing"
10
11	"github.com/stretchr/testify/assert"
12)
13
14func TestRepository_GetCommitBranches(t *testing.T) {
15	bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
16	bareRepo1, err := OpenRepository(bareRepo1Path)
17	assert.NoError(t, err)
18	defer bareRepo1.Close()
19
20	// these test case are specific to the repo1_bare test repo
21	testCases := []struct {
22		CommitID         string
23		ExpectedBranches []string
24	}{
25		{"2839944139e0de9737a044f78b0e4b40d989a9e3", []string{"branch1"}},
26		{"5c80b0245c1c6f8343fa418ec374b13b5d4ee658", []string{"branch2"}},
27		{"37991dec2c8e592043f47155ce4808d4580f9123", []string{"master"}},
28		{"95bb4d39648ee7e325106df01a621c530863a653", []string{"branch1", "branch2"}},
29		{"8d92fc957a4d7cfd98bc375f0b7bb189a0d6c9f2", []string{"branch2", "master"}},
30		{"master", []string{"master"}},
31	}
32	for _, testCase := range testCases {
33		commit, err := bareRepo1.GetCommit(testCase.CommitID)
34		assert.NoError(t, err)
35		branches, err := bareRepo1.getBranches(commit, 2)
36		assert.NoError(t, err)
37		assert.Equal(t, testCase.ExpectedBranches, branches)
38	}
39}
40
41func TestGetTagCommitWithSignature(t *testing.T) {
42	bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
43	bareRepo1, err := OpenRepository(bareRepo1Path)
44	assert.NoError(t, err)
45	defer bareRepo1.Close()
46
47	commit, err := bareRepo1.GetCommit("3ad28a9149a2864384548f3d17ed7f38014c9e8a")
48	assert.NoError(t, err)
49	assert.NotNil(t, commit)
50	assert.NotNil(t, commit.Signature)
51	// test that signature is not in message
52	assert.Equal(t, "tag", commit.CommitMessage)
53}
54
55func TestGetCommitWithBadCommitID(t *testing.T) {
56	bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
57	bareRepo1, err := OpenRepository(bareRepo1Path)
58	assert.NoError(t, err)
59	defer bareRepo1.Close()
60
61	commit, err := bareRepo1.GetCommit("bad_branch")
62	assert.Nil(t, commit)
63	assert.Error(t, err)
64	assert.True(t, IsErrNotExist(err))
65}
66
67func TestIsCommitInBranch(t *testing.T) {
68	bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
69	bareRepo1, err := OpenRepository(bareRepo1Path)
70	assert.NoError(t, err)
71	defer bareRepo1.Close()
72
73	result, err := bareRepo1.IsCommitInBranch("2839944139e0de9737a044f78b0e4b40d989a9e3", "branch1")
74	assert.NoError(t, err)
75	assert.True(t, result)
76
77	result, err = bareRepo1.IsCommitInBranch("2839944139e0de9737a044f78b0e4b40d989a9e3", "branch2")
78	assert.NoError(t, err)
79	assert.False(t, result)
80}
81
82func TestRepository_CommitsBetweenIDs(t *testing.T) {
83	bareRepo1Path := filepath.Join(testReposDir, "repo4_commitsbetween")
84	bareRepo1, err := OpenRepository(bareRepo1Path)
85	assert.NoError(t, err)
86	defer bareRepo1.Close()
87
88	cases := []struct {
89		OldID           string
90		NewID           string
91		ExpectedCommits int
92	}{
93		{"fdc1b615bdcff0f0658b216df0c9209e5ecb7c78", "78a445db1eac62fe15e624e1137965969addf344", 1}, //com1 -> com2
94		{"78a445db1eac62fe15e624e1137965969addf344", "fdc1b615bdcff0f0658b216df0c9209e5ecb7c78", 0}, //reset HEAD~, com2 -> com1
95		{"78a445db1eac62fe15e624e1137965969addf344", "a78e5638b66ccfe7e1b4689d3d5684e42c97d7ca", 1}, //com2 -> com2_new
96	}
97	for i, c := range cases {
98		commits, err := bareRepo1.CommitsBetweenIDs(c.NewID, c.OldID)
99		assert.NoError(t, err)
100		assert.Equal(t, c.ExpectedCommits, len(commits), "case %d", i)
101	}
102}
103