blob: 76c4efc374c2e27623178f684431533a91eb5e05 [file] [log] [blame]
Dave Borowitzc8a15682013-07-02 14:33:08 -07001// Copyright 2013 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
15package com.google.gitiles;
16
Dave Borowitzded109a2014-03-03 15:25:39 -050017import com.google.common.base.Enums;
Dave Borowitzded109a2014-03-03 15:25:39 -050018import com.google.common.collect.ImmutableMap;
19import com.google.common.collect.ImmutableSet;
David Pursehousec9621972016-10-17 14:56:57 +090020import java.util.Arrays;
David Pursehouse7a7f5472016-10-14 09:59:20 +090021import java.util.Optional;
Dave Borowitzc8a15682013-07-02 14:33:08 -070022import org.eclipse.jgit.api.ArchiveCommand;
23import org.eclipse.jgit.archive.TarFormat;
24import org.eclipse.jgit.archive.Tbz2Format;
25import org.eclipse.jgit.archive.TgzFormat;
26import org.eclipse.jgit.archive.TxzFormat;
27import org.eclipse.jgit.lib.Config;
Dave Borowitzc8a15682013-07-02 14:33:08 -070028
Dave Borowitz29168c62014-04-16 12:08:23 -070029public enum ArchiveFormat {
Dave Borowitzc8a15682013-07-02 14:33:08 -070030 TGZ("application/x-gzip", new TgzFormat()),
31 TAR("application/x-tar", new TarFormat()),
32 TBZ2("application/x-bzip2", new Tbz2Format()),
33 TXZ("application/x-xz", new TxzFormat());
34 // Zip is not supported because it may be interpreted by a Java plugin as a
35 // valid JAR file, whose code would have access to cookies on the domain.
36
Dave Borowitzded109a2014-03-03 15:25:39 -050037 private static final ImmutableMap<String, ArchiveFormat> BY_EXT;
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +020038
Dave Borowitzded109a2014-03-03 15:25:39 -050039 static {
40 ImmutableMap.Builder<String, ArchiveFormat> byExt = ImmutableMap.builder();
41 for (ArchiveFormat format : ArchiveFormat.values()) {
42 for (String ext : format.getSuffixes()) {
43 byExt.put(ext.toLowerCase(), format);
44 }
45 }
46 BY_EXT = byExt.build();
47 }
Dave Borowitzc8a15682013-07-02 14:33:08 -070048
Dave Borowitz29168c62014-04-16 12:08:23 -070049 /** Unregister all JGit archive formats supported by Gitiles. */
50 public static void unregisterAll() {
51 for (ArchiveFormat fmt : values()) {
Dave Borowitz36eb26d2014-04-19 16:42:32 -070052 ArchiveCommand.unregisterFormat(fmt.getRegisteredName());
Dave Borowitz29168c62014-04-16 12:08:23 -070053 }
54 }
55
Dave Borowitzc8a15682013-07-02 14:33:08 -070056 private final ArchiveCommand.Format<?> format;
57 private final String mimeType;
58
David Pursehousee3d3ec82016-06-15 22:10:48 +090059 ArchiveFormat(String mimeType, ArchiveCommand.Format<?> format) {
Dave Borowitzc8a15682013-07-02 14:33:08 -070060 this.format = format;
61 this.mimeType = mimeType;
Dave Borowitz36eb26d2014-04-19 16:42:32 -070062 ArchiveCommand.registerFormat(getRegisteredName(), format);
63 }
64
65 String getRegisteredName() {
66 return getShortName();
Dave Borowitzc8a15682013-07-02 14:33:08 -070067 }
68
69 String getShortName() {
70 return name().toLowerCase();
71 }
72
73 String getMimeType() {
74 return mimeType;
75 }
76
77 String getDefaultSuffix() {
78 return getSuffixes().iterator().next();
79 }
80
81 Iterable<String> getSuffixes() {
82 return format.suffixes();
83 }
84
85 static ArchiveFormat getDefault(Config cfg) {
Dave Borowitzded109a2014-03-03 15:25:39 -050086 for (String allowed : cfg.getStringList("archive", null, "format")) {
David Pursehouse7a7f5472016-10-14 09:59:20 +090087 ArchiveFormat result =
88 Enums.getIfPresent(ArchiveFormat.class, allowed.toUpperCase()).orNull();
89 if (result != null) {
90 return result;
Dave Borowitzded109a2014-03-03 15:25:39 -050091 }
92 }
93 return TGZ;
Dave Borowitzc8a15682013-07-02 14:33:08 -070094 }
95
Dave Borowitzded109a2014-03-03 15:25:39 -050096 static ImmutableSet<String> allExtensions() {
97 return BY_EXT.keySet();
98 }
99
100 static Optional<ArchiveFormat> byExtension(String ext, Config cfg) {
101 ArchiveFormat format = BY_EXT.get(ext);
102 if (format == null) {
David Pursehouse7a7f5472016-10-14 09:59:20 +0900103 return Optional.empty();
Dave Borowitzded109a2014-03-03 15:25:39 -0500104 }
Dave Borowitzc8a15682013-07-02 14:33:08 -0700105 String[] formats = cfg.getStringList("archive", null, "format");
106 if (formats.length == 0) {
Dave Borowitzded109a2014-03-03 15:25:39 -0500107 return Optional.of(format);
108 }
David Pursehousec9621972016-10-17 14:56:57 +0900109 return Arrays.stream(formats)
110 .filter(format.name()::equalsIgnoreCase)
111 .findFirst()
112 .map(x -> format);
Dave Borowitzc8a15682013-07-02 14:33:08 -0700113 }
114}