This repository was archived by the owner on Mar 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 184
Expand file tree
/
Copy pathGitilesView.java
More file actions
879 lines (786 loc) · 25 KB
/
Copy pathGitilesView.java
File metadata and controls
879 lines (786 loc) · 25 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
// Copyright 2012 Google Inc. All Rights Reserved.
//
// 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.google.gitiles;
import static com.google.common.base.MoreObjects.firstNonNull;
import static com.google.common.base.MoreObjects.toStringHelper;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import static com.google.gitiles.GitilesUrls.escapeName;
import static java.nio.charset.StandardCharsets.UTF_8;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Joiner;
import com.google.common.base.MoreObjects.ToStringHelper;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.LinkedListMultimap;
import com.google.common.collect.ListMultimap;
import com.google.common.collect.Multimaps;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.revwalk.RevObject;
/**
* Information about a view in Gitiles.
*
* <p>Views are uniquely identified by a type, and dispatched to servlet types by {@link
* GitilesServlet}. This class contains the list of all types, as well as some methods containing
* basic information parsed from the URL. Construction happens in {@link ViewFilter}.
*/
public class GitilesView {
private static final String DEFAULT_ARCHIVE_EXTENSION = ".tar.gz";
/** All the possible view types supported in the application. */
public enum Type {
HOST_INDEX,
REPOSITORY_INDEX,
REFS,
REVISION,
PATH,
SHOW,
DIFF,
LOG,
DESCRIBE,
ARCHIVE,
BLAME,
DOC,
ROOTED_DOC;
}
/** Exception thrown when building a view that is invalid. */
public static class InvalidViewException extends IllegalStateException {
private static final long serialVersionUID = 1L;
public InvalidViewException(String msg) {
super(msg);
}
}
/** Builder for views. */
public static class Builder {
private Type type;
private final ListMultimap<String, String> params = LinkedListMultimap.create();
private String hostName;
private String servletPath;
private String repositoryPrefix;
private String repositoryName;
private Revision revision = Revision.NULL;
private Revision oldRevision = Revision.NULL;
private String path;
private String extension;
private String anchor;
private Builder(Type type) {
this.type = type;
}
public Builder copyFrom(GitilesView other) {
if (type == Type.DOC && other.type == Type.ROOTED_DOC) {
type = Type.ROOTED_DOC;
}
hostName = other.hostName;
servletPath = other.servletPath;
switch (type) {
case HOST_INDEX:
repositoryPrefix = other.repositoryPrefix;
break;
case LOG:
case DIFF:
oldRevision = other.oldRevision;
// $FALL-THROUGH$
case PATH:
case DOC:
case ROOTED_DOC:
case ARCHIVE:
case BLAME:
case SHOW:
path = other.path;
// $FALL-THROUGH$
case REVISION:
revision = other.revision;
// $FALL-THROUGH$
case DESCRIBE:
case REFS:
case REPOSITORY_INDEX:
repositoryName = other.repositoryName;
// $FALL-THROUGH$
default:
break;
}
if (other.type == type) {
// Only copy params for matching type.
params.putAll(other.params);
if (type == Type.ARCHIVE) {
extension = other.extension;
}
}
return this;
}
public Builder copyFrom(HttpServletRequest req) {
return copyFrom(ViewFilter.getView(req));
}
public Builder setHostName(String hostName) {
this.hostName = checkNotNull(hostName);
return this;
}
public String getHostName() {
return hostName;
}
public Builder setServletPath(String servletPath) {
this.servletPath = checkNotNull(servletPath);
return this;
}
public String getServletPath() {
return servletPath;
}
public Builder setRepositoryPrefix(String prefix) {
if (type == Type.HOST_INDEX) {
this.repositoryPrefix =
prefix != null ? Strings.emptyToNull(maybeTrimLeadingAndTrailingSlash(prefix)) : null;
return this;
}
throw new IllegalStateException(
String.format("cannot set repository prefix on %s view", type));
}
public Builder setRepositoryName(String repositoryName) {
if (type == Type.HOST_INDEX) {
throw new IllegalStateException(
String.format("cannot set repository name on %s view", type));
}
this.repositoryName = checkNotNull(repositoryName);
return this;
}
public String getRepositoryName() {
return repositoryName;
}
public Builder setRevision(Revision revision) {
switch (type) {
case HOST_INDEX:
case REPOSITORY_INDEX:
case REFS:
case DESCRIBE:
throw new IllegalStateException(String.format("cannot set revision on %s view", type));
case ARCHIVE:
case BLAME:
case DIFF:
case DOC:
case LOG:
case PATH:
case REVISION:
case ROOTED_DOC:
case SHOW:
default:
this.revision = checkNotNull(revision);
return this;
}
}
public Builder setRevision(String name) {
return setRevision(Revision.named(name));
}
public Builder setRevision(RevObject obj) {
return setRevision(Revision.peeled(obj.name(), obj));
}
public Builder setRevision(String name, RevObject obj) {
return setRevision(Revision.peeled(name, obj));
}
public Revision getRevision() {
return revision;
}
public Builder setOldRevision(Revision revision) {
if (type != Type.DIFF && type != Type.LOG) {
revision = firstNonNull(revision, Revision.NULL);
checkState(Revision.isNull(revision), "cannot set old revision on %s view", type);
}
this.oldRevision = revision;
return this;
}
public Builder setOldRevision(RevObject obj) {
return setOldRevision(Revision.peeled(obj.name(), obj));
}
public Builder setOldRevision(String name, RevObject obj) {
return setOldRevision(Revision.peeled(name, obj));
}
public Revision getOldRevision() {
return oldRevision;
}
public Builder setPathPart(String path) {
switch (type) {
case PATH:
case DIFF:
case SHOW:
checkState(path != null, "cannot set null path on %s view", type);
break;
case BLAME:
case ARCHIVE:
case DESCRIBE:
case REFS:
case LOG:
case DOC:
case ROOTED_DOC:
break;
case HOST_INDEX:
case REPOSITORY_INDEX:
case REVISION:
default:
checkState(path == null, "cannot set path on %s view", type);
break;
}
this.path = path != null ? maybeTrimLeadingAndTrailingSlash(path) : null;
return this;
}
public String getPathPart() {
return path;
}
public Builder setExtension(String extension) {
if (type != Type.ARCHIVE) {
checkState(extension == null, "cannot set extension on %s view", type);
}
this.extension = extension;
return this;
}
public String getExtension() {
return extension;
}
public Builder putParam(String key, String value) {
params.put(key, value);
return this;
}
public Builder replaceParam(String key, String value) {
params.replaceValues(key, ImmutableList.of(value));
return this;
}
public Builder putAllParams(Map<String, String[]> params) {
for (Map.Entry<String, String[]> e : params.entrySet()) {
this.params.putAll(e.getKey(), Arrays.asList(e.getValue()));
}
return this;
}
public ListMultimap<String, String> getParams() {
return params;
}
public Builder setAnchor(String anchor) {
this.anchor = anchor;
return this;
}
public String getAnchor() {
return anchor;
}
public GitilesView build() {
switch (type) {
case HOST_INDEX:
checkHostIndex();
break;
case REPOSITORY_INDEX:
checkRepositoryIndex();
break;
case REFS:
checkRefs();
break;
case DESCRIBE:
checkDescribe();
break;
case REVISION:
checkRevision();
break;
case PATH:
case SHOW:
case DOC:
checkPath();
break;
case DIFF:
checkDiff();
break;
case LOG:
checkLog();
break;
case ARCHIVE:
checkArchive();
break;
case BLAME:
checkBlame();
break;
case ROOTED_DOC:
checkRootedDoc();
break;
}
return new GitilesView(
type,
hostName,
servletPath,
repositoryPrefix,
repositoryName,
revision,
oldRevision,
path,
extension,
params,
anchor);
}
public String toUrl() {
return build().toUrl();
}
private void checkView(boolean expr, String msg, Object... args) {
if (!expr) {
throw new InvalidViewException(String.format(msg, args));
}
}
private void checkHostIndex() {
checkView(hostName != null, "missing hostName on %s view", type);
checkView(servletPath != null, "missing servletPath on %s view", type);
}
private void checkRepositoryIndex() {
checkView(repositoryName != null, "missing repository name on %s view", type);
checkHostIndex();
}
private void checkRefs() {
checkRepositoryIndex();
}
private void checkDescribe() {
checkRepositoryIndex();
}
private void checkRevision() {
checkView(!Revision.isNull(revision), "missing revision on %s view", type);
checkRepositoryIndex();
}
private void checkDiff() {
checkPath();
}
private void checkLog() {
checkRepositoryIndex();
}
private void checkPath() {
checkView(path != null, "missing path on %s view", type);
checkRevision();
}
private void checkArchive() {
checkRevision();
}
private void checkBlame() {
checkPath();
}
private void checkRootedDoc() {
checkView(hostName != null, "missing hostName on %s view", type);
checkView(servletPath != null, "missing hostName on %s view", type);
checkView(!Revision.isNull(revision), "missing revision on %s view", type);
checkView(path != null, "missing path on %s view", type);
}
}
public static Builder hostIndex() {
return new Builder(Type.HOST_INDEX);
}
public static Builder repositoryIndex() {
return new Builder(Type.REPOSITORY_INDEX);
}
public static Builder refs() {
return new Builder(Type.REFS);
}
public static Builder describe() {
return new Builder(Type.DESCRIBE);
}
public static Builder revision() {
return new Builder(Type.REVISION);
}
public static Builder path() {
return new Builder(Type.PATH);
}
public static Builder show() {
return new Builder(Type.SHOW);
}
public static Builder diff() {
return new Builder(Type.DIFF);
}
public static Builder log() {
return new Builder(Type.LOG);
}
public static Builder archive() {
return new Builder(Type.ARCHIVE);
}
public static Builder blame() {
return new Builder(Type.BLAME);
}
public static Builder doc() {
return new Builder(Type.DOC);
}
public static Builder rootedDoc() {
return new Builder(Type.ROOTED_DOC);
}
static String maybeTrimLeadingAndTrailingSlash(String str) {
if (str.startsWith("/")) {
str = str.substring(1);
}
return !str.isEmpty() && str.endsWith("/") ? str.substring(0, str.length() - 1) : str;
}
private final Type type;
private final String hostName;
private final String servletPath;
private final String repositoryPrefix;
private final String repositoryName;
private final Revision revision;
private final Revision oldRevision;
private final String path;
private final String extension;
private final ListMultimap<String, String> params;
private final String anchor;
private GitilesView(
Type type,
String hostName,
String servletPath,
String repositoryPrefix,
String repositoryName,
Revision revision,
Revision oldRevision,
String path,
String extension,
ListMultimap<String, String> params,
String anchor) {
this.type = type;
this.hostName = hostName;
this.servletPath = servletPath;
this.repositoryPrefix = repositoryPrefix;
this.repositoryName = repositoryName;
this.revision = firstNonNull(revision, Revision.NULL);
this.oldRevision = firstNonNull(oldRevision, Revision.NULL);
this.path = path;
this.extension = extension;
this.params = Multimaps.unmodifiableListMultimap(params);
this.anchor = anchor;
}
public Builder copyFrom(GitilesView other) {
return new Builder(other.type).copyFrom(this);
}
public Builder toBuilder() {
return copyFrom(this);
}
public String getHostName() {
return hostName;
}
public String getServletPath() {
return servletPath;
}
public String getRepositoryPrefix() {
return repositoryPrefix;
}
public String getRepositoryName() {
return repositoryName;
}
public Revision getRevision() {
return revision;
}
public Revision getOldRevision() {
return oldRevision;
}
public String getRevisionRange() {
if (Revision.isNull(oldRevision)) {
if (type == Type.LOG || type == Type.DIFF) {
// For types that require two revisions, NULL indicates the empty
// tree/commit.
return revision.getName() + "^!";
}
// For everything else NULL indicates it is not a range, just a single
// revision.
return null;
} else if (type == Type.DIFF && isFirstParent(revision, oldRevision)) {
return revision.getName() + "^!";
} else {
return oldRevision.getName() + ".." + revision.getName();
}
}
public String getPathPart() {
return path;
}
public String getExtension() {
return extension;
}
public ListMultimap<String, String> getParameters() {
return params;
}
public String getAnchor() {
return anchor;
}
public Type getType() {
return type;
}
@Override
public String toString() {
ToStringHelper b =
toStringHelper(type.toString())
.omitNullValues()
.add("host", hostName)
.add("servlet", servletPath)
.add("prefix", repositoryPrefix)
.add("repo", repositoryName)
.add("rev", revision)
.add("old", oldRevision)
.add("path", path)
.add("extension", extension);
if (!params.isEmpty()) {
b.add("params", params);
}
b.add("anchor", anchor);
return b.toString();
}
/** @return an escaped, relative URL representing this view. */
public String toUrl() {
StringBuilder url = new StringBuilder(servletPath).append('/');
ListMultimap<String, String> params = this.params;
switch (type) {
case HOST_INDEX:
if (repositoryPrefix != null) {
url.append(repositoryPrefix).append('/');
}
params = LinkedListMultimap.create();
if (repositoryPrefix == null && !this.params.containsKey("format")) {
params.put("format", FormatType.HTML.toString());
}
params.putAll(this.params);
break;
case REPOSITORY_INDEX:
url.append(repositoryName).append('/');
break;
case REFS:
url.append(repositoryName).append("/+refs");
break;
case DESCRIBE:
url.append(repositoryName).append("/+describe");
break;
case REVISION:
url.append(repositoryName).append("/+/").append(revision.getName());
break;
case ARCHIVE:
url.append(repositoryName).append("/+archive/").append(revision.getName());
if (path != null) {
url.append('/').append(path);
}
url.append(firstNonNull(extension, DEFAULT_ARCHIVE_EXTENSION));
break;
case PATH:
url.append(repositoryName)
.append("/+/")
.append(revision.getName())
.append('/')
.append(path);
break;
case SHOW:
url.append(repositoryName)
.append("/+show/")
.append(revision.getName())
.append('/')
.append(path);
break;
case DIFF:
url.append(repositoryName).append("/+/");
if (isFirstParent(revision, oldRevision)) {
url.append(revision.getName()).append("^!");
} else {
url.append(oldRevision.getName()).append("..").append(revision.getName());
}
url.append('/').append(path);
break;
case LOG:
url.append(repositoryName).append("/+log");
if (!Revision.isNull(revision)) {
url.append('/');
if (!Revision.isNull(oldRevision)) {
url.append(oldRevision.getName()).append("..");
}
url.append(revision.getName());
if (path != null) {
url.append('/').append(path);
}
}
break;
case BLAME:
url.append(repositoryName)
.append("/+blame/")
.append(revision.getName())
.append('/')
.append(path);
break;
case DOC:
url.append(repositoryName);
if (path != null && path.endsWith(".md")) {
url.append("/+/");
} else {
url.append("/+doc/");
}
url.append(revision.getName());
if (path != null) {
url.append('/').append(path);
}
break;
case ROOTED_DOC:
if (path != null) {
url.append(path);
}
break;
default:
throw new IllegalStateException("Unknown view type: " + type);
}
String baseUrl = escapeName(url.toString());
url = new StringBuilder();
if (!params.isEmpty()) {
url.append('?').append(paramsToString(params));
}
if (!Strings.isNullOrEmpty(anchor)) {
url.append('#').append(escapeName(anchor));
}
return baseUrl + url;
}
/**
* @return a list of maps with "text" and "url" keys for all file paths leading up to the path
* represented by this view. All URLs allow auto-diving into one-entry subtrees; see also
* {@link #getBreadcrumbs(List)}.
*/
public List<Map<String, String>> getBreadcrumbs() {
return getBreadcrumbs(null);
}
private static final EnumSet<Type> NON_HTML_TYPES = EnumSet.of(Type.DESCRIBE, Type.ARCHIVE);
/**
* @param hasSingleTree list of booleans, one per path entry in this view's path excluding the
* leaf. True entries indicate the tree at that path only has a single entry that is another
* tree.
* @return a list of maps with "text" and "url" keys for all file paths leading up to the path
* represented by this view. URLs whose corresponding entry in {@code hasSingleTree} is true
* will disable auto-diving into one-entry subtrees.
*/
public List<Map<String, String>> getBreadcrumbs(List<Boolean> hasSingleTree) {
checkArgument(!NON_HTML_TYPES.contains(type), "breadcrumbs for %s view not supported", type);
checkArgument(
type != Type.REFS || Strings.isNullOrEmpty(path),
"breadcrumbs for REFS view with path not supported");
checkArgument(
hasSingleTree == null || type == Type.PATH, "hasSingleTree must be null for %s view", type);
String path = this.path;
ImmutableList.Builder<Map<String, String>> breadcrumbs = ImmutableList.builder();
breadcrumbs.add(breadcrumb(hostName, hostIndex().copyFrom(this).setRepositoryPrefix(null)));
if (repositoryPrefix != null) {
breadcrumbs.addAll(hostIndexBreadcrumbs(repositoryPrefix));
} else if (repositoryName != null) {
breadcrumbs.addAll(hostIndexBreadcrumbs(repositoryName));
}
if (type == Type.DIFF) {
// TODO(dborowitz): Tweak the breadcrumbs template to allow us to render
// separate links in "old..new".
breadcrumbs.add(breadcrumb(getRevisionRange(), diff().copyFrom(this).setPathPart("")));
} else if (type == Type.LOG) {
if (!Revision.isNull(revision)) {
// TODO(dborowitz): Add something in the navigation area (probably not
// a breadcrumb) to allow switching between /+log/ and /+/.
if (Revision.isNull(oldRevision)) {
breadcrumbs.add(breadcrumb(revision.getName(), log().copyFrom(this).setPathPart(null)));
} else {
breadcrumbs.add(breadcrumb(getRevisionRange(), log().copyFrom(this).setPathPart(null)));
}
} else {
breadcrumbs.add(breadcrumb(Constants.HEAD, log().copyFrom(this)));
}
path = Strings.emptyToNull(path);
} else if (!Revision.isNull(revision)) {
breadcrumbs.add(breadcrumb(revision.getName(), revision().copyFrom(this)));
}
if (path != null) {
if (type != Type.LOG && type != Type.REFS) {
// The "." breadcrumb would be no different for LOG or REFS.
breadcrumbs.add(breadcrumb(".", copyWithPath(false).setPathPart("")));
}
StringBuilder cur = new StringBuilder();
List<String> parts = PathUtil.SPLITTER.omitEmptyStrings().splitToList(path);
checkArgument(
hasSingleTree == null
|| (parts.isEmpty() && hasSingleTree.isEmpty())
|| hasSingleTree.size() == parts.size() - 1,
"hasSingleTree has wrong number of entries");
for (int i = 0; i < parts.size(); i++) {
String part = parts.get(i);
cur.append(part).append('/');
String curPath = cur.toString();
boolean isLeaf = i == parts.size() - 1;
Builder builder = copyWithPath(isLeaf).setPathPart(curPath);
if (hasSingleTree != null && i < parts.size() - 1 && hasSingleTree.get(i)) {
builder.replaceParam(PathServlet.AUTODIVE_PARAM, PathServlet.NO_AUTODIVE_VALUE);
}
breadcrumbs.add(breadcrumb(part, builder));
}
}
return breadcrumbs.build();
}
private List<Map<String, String>> hostIndexBreadcrumbs(String name) {
List<String> parts = Splitter.on('/').splitToList(name);
List<Map<String, String>> r = new ArrayList<>(parts.size());
for (int i = 0; i < parts.size(); i++) {
String prefix = Joiner.on('/').join(parts.subList(0, i + 1));
r.add(breadcrumb(parts.get(i), hostIndex().copyFrom(this).setRepositoryPrefix(prefix)));
}
return r;
}
private static Map<String, String> breadcrumb(String text, Builder url) {
return ImmutableMap.of("text", text, "url", url.toUrl());
}
private Builder copyWithPath(boolean isLeaf) {
Builder copy;
switch (type) {
case DIFF:
copy = diff();
break;
case LOG:
copy = log();
break;
case BLAME:
copy = isLeaf ? blame() : path();
break;
case ARCHIVE:
case DESCRIBE:
case DOC:
case HOST_INDEX:
case PATH:
case REFS:
case REPOSITORY_INDEX:
case REVISION:
case ROOTED_DOC:
case SHOW:
default:
copy = path();
break;
}
return copy.copyFrom(this);
}
private static boolean isFirstParent(Revision rev1, Revision rev2) {
return Revision.isNull(rev2)
|| rev2.getName().equals(rev1.getName() + "^")
|| rev2.getName().equals(rev1.getName() + "~1");
}
@VisibleForTesting
static String paramsToString(ListMultimap<String, String> params) {
try {
StringBuilder sb = new StringBuilder();
boolean first = true;
for (Map.Entry<String, String> e : params.entries()) {
if (!first) {
sb.append('&');
} else {
first = false;
}
sb.append(URLEncoder.encode(e.getKey(), UTF_8.name()));
if (!"".equals(e.getValue())) {
sb.append('=').append(URLEncoder.encode(e.getValue(), UTF_8.name()));
}
}
return sb.toString();
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException(e);
}
}
}