| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 1 | // Copyright 2012 Google Inc. All Rights Reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package com.google.gitiles; |
| 16 | |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 17 | import static com.google.common.truth.Truth.assertThat; |
| Dave Borowitz | 1d94e65 | 2014-07-30 12:45:09 -0700 | [diff] [blame] | 18 | import static com.google.gitiles.TestGitilesUrls.URLS; |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 19 | |
| Dave Borowitz | 32ec5b9 | 2014-07-30 07:43:28 -0700 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableList; |
| Dave Borowitz | 1d94e65 | 2014-07-30 12:45:09 -0700 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableMap; |
| Dave Borowitz | 32ec5b9 | 2014-07-30 07:43:28 -0700 | [diff] [blame] | 22 | import com.google.gitiles.RefServlet.RefJsonData; |
| Dave Borowitz | 32ec5b9 | 2014-07-30 07:43:28 -0700 | [diff] [blame] | 23 | import com.google.gson.reflect.TypeToken; |
| Dave Borowitz | 3b744b1 | 2016-08-19 16:11:10 -0400 | [diff] [blame] | 24 | import java.io.IOException; |
| 25 | import java.util.List; |
| 26 | import java.util.Map; |
| 27 | import javax.servlet.http.HttpServletRequest; |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 28 | import org.eclipse.jgit.lib.ObjectId; |
| 29 | import org.eclipse.jgit.lib.Repository; |
| 30 | import org.eclipse.jgit.revwalk.RevCommit; |
| 31 | import org.eclipse.jgit.revwalk.RevTag; |
| Dave Borowitz | 1d94e65 | 2014-07-30 12:45:09 -0700 | [diff] [blame] | 32 | import org.eclipse.jgit.revwalk.RevWalk; |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 33 | import org.junit.Test; |
| Dave Borowitz | 3dc854f | 2014-11-04 16:19:37 -0800 | [diff] [blame] | 34 | import org.junit.runner.RunWith; |
| 35 | import org.junit.runners.JUnit4; |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 36 | |
| Nodir Turakulov | 08cdc23 | 2015-08-28 12:02:16 -0700 | [diff] [blame] | 37 | /** Tests for {@link RefServlet}. */ |
| Dave Borowitz | 3dc854f | 2014-11-04 16:19:37 -0800 | [diff] [blame] | 38 | @RunWith(JUnit4.class) |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 39 | public class RefServletTest extends ServletTest { |
| Dave Borowitz | 1d94e65 | 2014-07-30 12:45:09 -0700 | [diff] [blame] | 40 | private void setUpSimpleRefs() throws Exception { |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 41 | RevCommit commit = repo.branch("refs/heads/master").commit().create(); |
| 42 | repo.update("refs/heads/branch", commit); |
| 43 | repo.update("refs/tags/ctag", commit); |
| 44 | RevTag tag = repo.tag("atag", commit); |
| 45 | repo.update("refs/tags/atag", tag); |
| Dave Borowitz | 1d94e65 | 2014-07-30 12:45:09 -0700 | [diff] [blame] | 46 | repo.getRepository().updateRef("HEAD").link("refs/heads/master"); |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 47 | } |
| 48 | |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 49 | @Test |
| 50 | public void evilRefName() throws Exception { |
| Dave Borowitz | 1d94e65 | 2014-07-30 12:45:09 -0700 | [diff] [blame] | 51 | setUpSimpleRefs(); |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 52 | String evilRefName = "refs/evil/<script>window.close();</script>/&foo"; |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 53 | assertThat(Repository.isValidRefName(evilRefName)).isTrue(); |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 54 | repo.branch(evilRefName).commit().create(); |
| 55 | |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 56 | FakeHttpServletResponse res = buildText("/repo/+refs/evil"); |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 57 | assertThat(res.getActualBodyString()) |
| 58 | .isEqualTo( |
| 59 | id(evilRefName) + " refs/evil/<script>window.close();</script>/&foo\n"); |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 62 | @Test |
| 63 | public void getRefsTextAll() throws Exception { |
| Dave Borowitz | 1d94e65 | 2014-07-30 12:45:09 -0700 | [diff] [blame] | 64 | setUpSimpleRefs(); |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 65 | FakeHttpServletResponse res = buildText("/repo/+refs"); |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 66 | |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 67 | assertThat(res.getActualBodyString()) |
| 68 | .isEqualTo( |
| 69 | id("HEAD") |
| 70 | + " HEAD\n" |
| 71 | + id("refs/heads/branch") |
| 72 | + " refs/heads/branch\n" |
| 73 | + id("refs/heads/master") |
| 74 | + " refs/heads/master\n" |
| 75 | + id("refs/tags/atag") |
| 76 | + " refs/tags/atag\n" |
| 77 | + peeled("refs/tags/atag") |
| 78 | + " refs/tags/atag^{}\n" |
| 79 | + id("refs/tags/ctag") |
| 80 | + " refs/tags/ctag\n"); |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 81 | } |
| 82 | |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 83 | @Test |
| 84 | public void getRefsTextAllTrailingSlash() throws Exception { |
| Dave Borowitz | 1d94e65 | 2014-07-30 12:45:09 -0700 | [diff] [blame] | 85 | setUpSimpleRefs(); |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 86 | FakeHttpServletResponse res = buildText("/repo/+refs/"); |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 87 | |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 88 | assertThat(res.getActualBodyString()) |
| 89 | .isEqualTo( |
| 90 | id("HEAD") |
| 91 | + " HEAD\n" |
| 92 | + id("refs/heads/branch") |
| 93 | + " refs/heads/branch\n" |
| 94 | + id("refs/heads/master") |
| 95 | + " refs/heads/master\n" |
| 96 | + id("refs/tags/atag") |
| 97 | + " refs/tags/atag\n" |
| 98 | + peeled("refs/tags/atag") |
| 99 | + " refs/tags/atag^{}\n" |
| 100 | + id("refs/tags/ctag") |
| 101 | + " refs/tags/ctag\n"); |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 104 | @Test |
| 105 | public void getRefsHeadsText() throws Exception { |
| Dave Borowitz | 1d94e65 | 2014-07-30 12:45:09 -0700 | [diff] [blame] | 106 | setUpSimpleRefs(); |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 107 | FakeHttpServletResponse res = buildText("/repo/+refs/heads"); |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 108 | |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 109 | assertThat(res.getActualBodyString()) |
| 110 | .isEqualTo( |
| 111 | id("refs/heads/branch") |
| 112 | + " refs/heads/branch\n" |
| 113 | + id("refs/heads/master") |
| 114 | + " refs/heads/master\n"); |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 115 | } |
| 116 | |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 117 | @Test |
| 118 | public void getRefsHeadsTextTrailingSlash() throws Exception { |
| Dave Borowitz | 1d94e65 | 2014-07-30 12:45:09 -0700 | [diff] [blame] | 119 | setUpSimpleRefs(); |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 120 | FakeHttpServletResponse res = buildText("/repo/+refs/heads/"); |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 121 | |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 122 | assertThat(res.getActualBodyString()) |
| 123 | .isEqualTo( |
| 124 | id("refs/heads/branch") |
| 125 | + " refs/heads/branch\n" |
| 126 | + id("refs/heads/master") |
| 127 | + " refs/heads/master\n"); |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 128 | } |
| 129 | |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 130 | @Test |
| 131 | public void noHeadText() throws Exception { |
| Dave Borowitz | 1d94e65 | 2014-07-30 12:45:09 -0700 | [diff] [blame] | 132 | setUpSimpleRefs(); |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 133 | FakeHttpServletResponse res = buildText("/repo/+refs/HEAD"); |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 134 | |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 135 | // /+refs/foo means refs/foo(/*), so this is empty. |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 136 | assertThat(res.getActualBodyString()).isEqualTo(""); |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 137 | } |
| 138 | |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 139 | @Test |
| 140 | public void singleHeadText() throws Exception { |
| Dave Borowitz | 1d94e65 | 2014-07-30 12:45:09 -0700 | [diff] [blame] | 141 | setUpSimpleRefs(); |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 142 | FakeHttpServletResponse res = buildText("/repo/+refs/heads/master"); |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 143 | |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 144 | assertThat(res.getActualBodyString()) |
| 145 | .isEqualTo(id("refs/heads/master") + " refs/heads/master\n"); |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 146 | } |
| 147 | |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 148 | @Test |
| Alice Kober-Sotzek | 53794aa | 2018-11-07 14:29:47 +0100 | [diff] [blame^] | 149 | public void singleHeadJson() throws Exception { |
| 150 | setUpSimpleRefs(); |
| 151 | Map<String, RefJsonData> result = buildRefJson("/repo/+refs/heads/master"); |
| 152 | |
| 153 | assertThat(result.keySet()).containsExactly("refs/heads/master"); |
| 154 | RefJsonData master = result.get("refs/heads/master"); |
| 155 | assertThat(master.value).isEqualTo(id("refs/heads/master")); |
| 156 | assertThat(master.peeled).isNull(); |
| 157 | assertThat(master.target).isNull(); |
| 158 | } |
| 159 | |
| 160 | @Test |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 161 | public void singlePeeledTagText() throws Exception { |
| Dave Borowitz | 1d94e65 | 2014-07-30 12:45:09 -0700 | [diff] [blame] | 162 | setUpSimpleRefs(); |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 163 | FakeHttpServletResponse res = buildText("/repo/+refs/tags/atag"); |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 164 | |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 165 | assertThat(res.getActualBodyString()) |
| 166 | .isEqualTo( |
| 167 | id("refs/tags/atag") |
| 168 | + " refs/tags/atag\n" |
| 169 | + peeled("refs/tags/atag") |
| 170 | + " refs/tags/atag^{}\n"); |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 171 | } |
| Dave Borowitz | 32ec5b9 | 2014-07-30 07:43:28 -0700 | [diff] [blame] | 172 | |
| 173 | @Test |
| 174 | public void getRefsJsonAll() throws Exception { |
| Dave Borowitz | 1d94e65 | 2014-07-30 12:45:09 -0700 | [diff] [blame] | 175 | setUpSimpleRefs(); |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 176 | Map<String, RefJsonData> result = buildRefJson("/repo/+refs"); |
| Dave Borowitz | 32ec5b9 | 2014-07-30 07:43:28 -0700 | [diff] [blame] | 177 | List<String> keys = ImmutableList.copyOf(result.keySet()); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 178 | assertThat(keys) |
| 179 | .containsExactly( |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 180 | "HEAD", "refs/heads/branch", "refs/heads/master", "refs/tags/atag", "refs/tags/ctag") |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 181 | .inOrder(); |
| Dave Borowitz | 32ec5b9 | 2014-07-30 07:43:28 -0700 | [diff] [blame] | 182 | |
| 183 | RefJsonData head = result.get(keys.get(0)); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 184 | assertThat(head.value).isEqualTo(id("HEAD")); |
| 185 | assertThat(head.peeled).isNull(); |
| 186 | assertThat(head.target).isEqualTo("refs/heads/master"); |
| Dave Borowitz | 32ec5b9 | 2014-07-30 07:43:28 -0700 | [diff] [blame] | 187 | |
| 188 | RefJsonData branch = result.get(keys.get(1)); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 189 | assertThat(branch.value).isEqualTo(id("refs/heads/branch")); |
| 190 | assertThat(branch.peeled).isNull(); |
| 191 | assertThat(branch.target).isNull(); |
| Dave Borowitz | 32ec5b9 | 2014-07-30 07:43:28 -0700 | [diff] [blame] | 192 | |
| 193 | RefJsonData master = result.get(keys.get(2)); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 194 | assertThat(master.value).isEqualTo(id("refs/heads/master")); |
| 195 | assertThat(master.peeled).isNull(); |
| 196 | assertThat(master.target).isNull(); |
| Dave Borowitz | 32ec5b9 | 2014-07-30 07:43:28 -0700 | [diff] [blame] | 197 | |
| 198 | RefJsonData atag = result.get(keys.get(3)); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 199 | assertThat(atag.value).isEqualTo(id("refs/tags/atag")); |
| 200 | assertThat(atag.peeled).isEqualTo(peeled("refs/tags/atag")); |
| 201 | assertThat(atag.target).isNull(); |
| Dave Borowitz | 32ec5b9 | 2014-07-30 07:43:28 -0700 | [diff] [blame] | 202 | |
| 203 | RefJsonData ctag = result.get(keys.get(4)); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 204 | assertThat(ctag.value).isEqualTo(id("refs/tags/ctag")); |
| 205 | assertThat(ctag.peeled).isNull(); |
| 206 | assertThat(ctag.target).isNull(); |
| Dave Borowitz | 32ec5b9 | 2014-07-30 07:43:28 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | @Test |
| 210 | public void getRefsHeadsJson() throws Exception { |
| Dave Borowitz | 1d94e65 | 2014-07-30 12:45:09 -0700 | [diff] [blame] | 211 | setUpSimpleRefs(); |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 212 | Map<String, RefJsonData> result = buildRefJson("/repo/+refs/heads"); |
| Dave Borowitz | 32ec5b9 | 2014-07-30 07:43:28 -0700 | [diff] [blame] | 213 | List<String> keys = ImmutableList.copyOf(result.keySet()); |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 214 | assertThat(keys).containsExactly("branch", "master").inOrder(); |
| Dave Borowitz | 32ec5b9 | 2014-07-30 07:43:28 -0700 | [diff] [blame] | 215 | |
| 216 | RefJsonData branch = result.get(keys.get(0)); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 217 | assertThat(branch.value).isEqualTo(id("refs/heads/branch")); |
| 218 | assertThat(branch.peeled).isNull(); |
| 219 | assertThat(branch.target).isNull(); |
| Dave Borowitz | 32ec5b9 | 2014-07-30 07:43:28 -0700 | [diff] [blame] | 220 | |
| 221 | RefJsonData master = result.get(keys.get(1)); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 222 | assertThat(master.value).isEqualTo(id("refs/heads/master")); |
| 223 | assertThat(master.peeled).isNull(); |
| 224 | assertThat(master.target).isNull(); |
| Dave Borowitz | 32ec5b9 | 2014-07-30 07:43:28 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 227 | private Map<String, RefJsonData> buildRefJson(String path) throws Exception { |
| Dave Borowitz | a774f59 | 2015-10-26 11:41:27 -0400 | [diff] [blame] | 228 | return buildJson(new TypeToken<Map<String, RefJsonData>>() {}, path); |
| Dave Borowitz | 32ec5b9 | 2014-07-30 07:43:28 -0700 | [diff] [blame] | 229 | } |
| Dave Borowitz | 1d94e65 | 2014-07-30 12:45:09 -0700 | [diff] [blame] | 230 | |
| 231 | @Test |
| 232 | public void emptySoy() throws Exception { |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 233 | assertThat(buildBranchesSoyData()).isEmpty(); |
| 234 | assertThat(buildTagsSoyData()).isEmpty(); |
| Dave Borowitz | 1d94e65 | 2014-07-30 12:45:09 -0700 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | @Test |
| 238 | public void branchesAndTagsSoy() throws Exception { |
| 239 | repo.branch("refs/heads/foo").commit().create(); |
| 240 | repo.branch("refs/heads/bar").commit().create(); |
| 241 | repo.branch("refs/tags/baz").commit().create(); |
| 242 | repo.branch("refs/nope/quux").commit().create(); |
| 243 | |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 244 | assertThat(buildBranchesSoyData()) |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 245 | .containsExactly(ref("/b/test/+/bar", "bar"), ref("/b/test/+/foo", "foo")) |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 246 | .inOrder(); |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 247 | assertThat(buildTagsSoyData()).containsExactly(ref("/b/test/+/baz", "baz")).inOrder(); |
| Dave Borowitz | 1d94e65 | 2014-07-30 12:45:09 -0700 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | @Test |
| 251 | public void ambiguousBranchSoy() throws Exception { |
| 252 | repo.branch("refs/heads/foo").commit().create(); |
| 253 | repo.branch("refs/heads/bar").commit().create(); |
| 254 | repo.branch("refs/tags/foo").commit().create(); |
| 255 | |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 256 | assertThat(buildBranchesSoyData()) |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 257 | .containsExactly(ref("/b/test/+/bar", "bar"), ref("/b/test/+/refs/heads/foo", "foo")) |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 258 | .inOrder(); |
| 259 | assertThat(buildTagsSoyData()) |
| 260 | .containsExactly( |
| Dave Borowitz | 1d94e65 | 2014-07-30 12:45:09 -0700 | [diff] [blame] | 261 | // refs/tags/ is searched before refs/heads/, so this does not |
| 262 | // appear ambiguous. |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 263 | ref("/b/test/+/foo", "foo")) |
| 264 | .inOrder(); |
| Dave Borowitz | 1d94e65 | 2014-07-30 12:45:09 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | @Test |
| 268 | public void ambiguousRelativeToNonBranchOrTagSoy() throws Exception { |
| 269 | repo.branch("refs/foo").commit().create(); |
| 270 | repo.branch("refs/heads/foo").commit().create(); |
| 271 | repo.branch("refs/tags/foo").commit().create(); |
| 272 | |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 273 | assertThat(buildBranchesSoyData()) |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 274 | .containsExactly(ref("/b/test/+/refs/heads/foo", "foo")) |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 275 | .inOrder(); |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 276 | assertThat(buildTagsSoyData()).containsExactly(ref("/b/test/+/refs/tags/foo", "foo")).inOrder(); |
| Dave Borowitz | 1d94e65 | 2014-07-30 12:45:09 -0700 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | @Test |
| 280 | public void refsHeadsSoy() throws Exception { |
| 281 | repo.branch("refs/heads/foo").commit().create(); |
| 282 | repo.branch("refs/heads/refs/heads/foo").commit().create(); |
| 283 | |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 284 | assertThat(buildBranchesSoyData()) |
| 285 | .containsExactly( |
| Dave Borowitz | 1d94e65 | 2014-07-30 12:45:09 -0700 | [diff] [blame] | 286 | ref("/b/test/+/foo", "foo"), |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 287 | ref("/b/test/+/refs/heads/refs/heads/foo", "refs/heads/foo")) |
| 288 | .inOrder(); |
| Dave Borowitz | 1d94e65 | 2014-07-30 12:45:09 -0700 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | private HttpServletRequest buildSoyRequest() { |
| 292 | HttpServletRequest req = FakeHttpServletRequest.newRequest(repo.getRepository()); |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 293 | ViewFilter.setView( |
| 294 | req, |
| 295 | GitilesView.repositoryIndex() |
| 296 | .setHostName(URLS.getHostName(req)) |
| 297 | .setServletPath(req.getServletPath()) |
| 298 | .setRepositoryName("test") |
| 299 | .build()); |
| Dave Borowitz | 1d94e65 | 2014-07-30 12:45:09 -0700 | [diff] [blame] | 300 | return req; |
| 301 | } |
| 302 | |
| 303 | private List<?> buildBranchesSoyData() throws Exception { |
| 304 | return RefServlet.getBranchesSoyData(buildSoyRequest(), Integer.MAX_VALUE); |
| 305 | } |
| 306 | |
| 307 | private List<?> buildTagsSoyData() throws Exception { |
| Shawn Pearce | b5ad0a0 | 2015-05-24 20:33:17 -0700 | [diff] [blame] | 308 | try (RevWalk rw = new RevWalk(repo.getRepository())) { |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 309 | return RefServlet.getTagsSoyData( |
| 310 | buildSoyRequest(), new TimeCache(TimeCache.defaultBuilder()), rw, Integer.MAX_VALUE); |
| Dave Borowitz | 1d94e65 | 2014-07-30 12:45:09 -0700 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | |
| 314 | private String id(String refName) throws IOException { |
| Dave Borowitz | 14cad73 | 2016-05-26 17:34:19 -0400 | [diff] [blame] | 315 | return ObjectId.toString(repo.getRepository().exactRef(refName).getObjectId()); |
| Dave Borowitz | 1d94e65 | 2014-07-30 12:45:09 -0700 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | private String peeled(String refName) throws IOException { |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 319 | return ObjectId.toString( |
| David Pursehouse | dcb3d05 | 2018-07-03 14:04:17 +0900 | [diff] [blame] | 320 | repo.getRepository() |
| 321 | .getRefDatabase() |
| 322 | .peel(repo.getRepository().exactRef(refName)) |
| 323 | .getPeeledObjectId()); |
| Dave Borowitz | 1d94e65 | 2014-07-30 12:45:09 -0700 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | private Map<String, String> ref(String url, String name) { |
| 327 | return ImmutableMap.of("url", url, "name", name); |
| 328 | } |
| Dave Borowitz | d0b7e18 | 2013-01-11 15:55:09 -0800 | [diff] [blame] | 329 | } |