summaryrefslogtreecommitdiffstats
path: root/src/com/gitblit/tests/JGitUtilsTest.java
blob: 8dfa46a305d73256e2e729bc322a9563d3b769e3 (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
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
/*
 * Copyright 2011 gitblit.com.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.gitblit.tests;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Date;
import java.util.List;

import junit.framework.TestCase;

import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevBlob;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevObject;
import org.eclipse.jgit.revwalk.RevTree;
import org.eclipse.jgit.storage.file.FileRepository;

import com.gitblit.utils.JGitUtils;
import com.gitblit.utils.JGitUtils.DiffOutputType;
import com.gitblit.wicket.models.PathModel.PathChangeModel;
import com.gitblit.wicket.models.RefModel;
import com.gitblit.wicket.models.TicketModel;

public class JGitUtilsTest extends TestCase {

	private File repositoriesFolder = new File("c:/projects/git");
	private boolean exportAll = true;
	private boolean readNested = true;

	private List<String> getRepositories() {
		return JGitUtils.getRepositoryList(repositoriesFolder, exportAll, readNested);
	}

	private Repository getRepository() throws Exception {
		return new FileRepository(new File(repositoriesFolder, getRepositories().get(0)) + "/" + Constants.DOT_GIT);
	}

	public void testFindRepositories() {
		List<String> list = getRepositories();
		assertTrue("No repositories found in " + repositoriesFolder, list.size() > 0);
	}

	public void testOpenRepository() throws Exception {
		Repository r = getRepository();
		r.close();
		assertTrue("Could not find repository!", r != null);
	}

	public void testLastChangeRepository() throws Exception {
		Repository r = getRepository();
		Date date = JGitUtils.getLastChange(r);
		r.close();
		assertTrue("Could not get last repository change date!", date != null);
	}
	
	public void testFirstCommit() throws Exception {
		Repository r = getRepository();
		RevCommit commit = JGitUtils.getFirstCommit(r, null);
		r.close();
		assertTrue("Could not get first commit!", commit != null);
		System.out.println(commit.getName() + " " + commit.getShortMessage());
	}

	public void testRetrieveRevObject() throws Exception {
		Repository r = getRepository();
		RevCommit commit = JGitUtils.getCommit(r, Constants.HEAD);
		RevTree tree = commit.getTree();
		RevObject object = JGitUtils.getRevObject(r, tree, "AUTHORS");
		r.close();
		assertTrue("Object is null!", object != null);
	}

	public void testRetrieveStringContent() throws Exception {
		Repository r = getRepository();
		RevCommit commit = JGitUtils.getCommit(r, Constants.HEAD);
		RevTree tree = commit.getTree();
		RevBlob blob = (RevBlob) JGitUtils.getRevObject(r, tree, "AUTHORS");
		String content = JGitUtils.getRawContentAsString(r, blob);
		r.close();
		assertTrue("Content is null!", content != null);
	}

	public void testTicGit() throws Exception {
		Repository r = new FileRepository(new File(repositoriesFolder, "ticgit") + "/" + Constants.DOT_GIT);
		RefModel ticgit = JGitUtils.getTicketsBranch(r);
		assertTrue("Ticgit branch does not exist!", ticgit != null);
		List<TicketModel> tickets = JGitUtils.getTickets(r);
		assertTrue("No tickets found!", tickets.size() > 0);
		r.close();
	}

	public void testFilesInCommit() throws Exception {
		Repository r = getRepository();
		RevCommit commit = JGitUtils.getCommit(r, Constants.HEAD);
		List<PathChangeModel> paths = JGitUtils.getFilesInCommit(r, commit);
		r.close();
		assertTrue("No changed paths found!", paths.size() > 0);
	}

	public void testCommitDiff() throws Exception {
		Repository r = getRepository();
		RevCommit commit = JGitUtils.getCommit(r, Constants.HEAD);
		String diff = JGitUtils.getCommitDiff(r, commit, DiffOutputType.PLAIN);
		r.close();
		System.out.println(diff);
	}
	
	public void testZip() throws Exception {
		Repository r = new FileRepository(new File(repositoriesFolder, "gitblit.git/" + Constants.DOT_GIT));
		FileOutputStream fos = null;
		try {
			File zipFile = new File("c:/output.zip");
			zipFile.delete();
			fos = new FileOutputStream(zipFile);
			if (JGitUtils.zip(r, "src", Constants.HEAD, fos)) {
				System.out.println("zip = " + zipFile.length() + " bytes");
			} else {
				System.err.println("failed to generate zip file?!");
			}
		} finally {
			if (fos != null) {
				try {
					fos.close();
				} catch (Throwable t) {
				}
			}
		}
	}

}