View Javadoc
1   /*
2    * Copyright (C) 2018, Thomas Wolf <thomas.wolf@paranor.ch> and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  package org.eclipse.jgit.lib;
11  
12  import static org.junit.Assert.assertTrue;
13  
14  import java.util.List;
15  
16  import org.eclipse.jgit.api.Git;
17  import org.eclipse.jgit.junit.RepositoryTestCase;
18  import org.eclipse.jgit.revwalk.RevCommit;
19  import org.eclipse.jgit.storage.file.WindowCacheConfig;
20  import org.eclipse.jgit.storage.pack.PackConfig;
21  import org.junit.Test;
22  
23  public class HugeCommitMessageTest extends RepositoryTestCase {
24  
25  	private static final int HUGE_SIZE = Math.max(15 * WindowCacheConfig.MB,
26  			PackConfig.DEFAULT_BIG_FILE_THRESHOLD + WindowCacheConfig.MB);
27  	// Larger than the 5MB fallback limit in RevWalk.getCachedBytes(RevObject
28  	// obj, ObjectLoader ldr), and also larger than the default
29  	// streamFileThreshold.
30  
31  	@Test
32  	public void testHugeCommitMessage() throws Exception {
33  		try (Git git = new Git(db)) {
34  			writeTrashFile("foo", "foo");
35  			git.add().addFilepattern("foo").call();
36  			WindowCacheConfig wc = new WindowCacheConfig();
37  			wc.setStreamFileThreshold(HUGE_SIZE + WindowCacheConfig.MB);
38  			wc.install();
39  			RevCommit commit = git.commit()
40  					.setMessage(insanelyHugeCommitMessage()).call();
41  			Ref master = db.findRef("master");
42  			List<Ref> actual = git.branchList().setContains(commit.getName())
43  					.call();
44  			assertTrue("Should be contained in branch master",
45  					actual.contains(master));
46  		}
47  	}
48  
49  	private String insanelyHugeCommitMessage() {
50  		final String oneLine = "012345678901234567890123456789012345678901234567890123456789\n";
51  		StringBuilder b = new StringBuilder(HUGE_SIZE + oneLine.length());
52  		// Give the message a real header; otherwise even writing the reflog
53  		// message may run into troubles because RevCommit.getShortMessage()
54  		// will return the whole message.
55  		b.append("An insanely huge commit message\n\n");
56  		while (b.length() < HUGE_SIZE) {
57  			b.append(oneLine);
58  		}
59  		return b.toString();
60  	}
61  
62  }