blob: 8e2ad8eeb99cbb60ab80d2a3402b5e6ab7788f00 [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
David Pursehouseee4b4d72019-06-05 13:31:11 +090056 @SuppressWarnings("ImmutableEnumChecker") // ArchiveCommand.Format is effectively immutable.
Dave Borowitzc8a15682013-07-02 14:33:08 -070057 private final ArchiveCommand.Format<?> format;
David Pursehoused79cca72019-06-10 12:32:46 +090058
Dave Borowitzc8a15682013-07-02 14:33:08 -070059 private final String mimeType;
60
David Pursehousee3d3ec82016-06-15 22:10:48 +090061 ArchiveFormat(String mimeType, ArchiveCommand.Format<?> format) {
Dave Borowitzc8a15682013-07-02 14:33:08 -070062 this.format = format;
63 this.mimeType = mimeType;
Dave Borowitz36eb26d2014-04-19 16:42:32 -070064 ArchiveCommand.registerFormat(getRegisteredName(), format);
65 }
66
67 String getRegisteredName() {
68 return getShortName();
Dave Borowitzc8a15682013-07-02 14:33:08 -070069 }
70
71 String getShortName() {
72 return name().toLowerCase();
73 }
74
75 String getMimeType() {
76 return mimeType;
77 }
78
79 String getDefaultSuffix() {
80 return getSuffixes().iterator().next();
81 }
82
83 Iterable<String> getSuffixes() {
84 return format.suffixes();
85 }
86
87 static ArchiveFormat getDefault(Config cfg) {
Dave Borowitzded109a2014-03-03 15:25:39 -050088 for (String allowed : cfg.getStringList("archive", null, "format")) {
David Pursehouse7a7f5472016-10-14 09:59:20 +090089 ArchiveFormat result =
90 Enums.getIfPresent(ArchiveFormat.class, allowed.toUpperCase()).orNull();
91 if (result != null) {
92 return result;
Dave Borowitzded109a2014-03-03 15:25:39 -050093 }
94 }
95 return TGZ;
Dave Borowitzc8a15682013-07-02 14:33:08 -070096 }
97
Dave Borowitzded109a2014-03-03 15:25:39 -050098 static ImmutableSet<String> allExtensions() {
99 return BY_EXT.keySet();
100 }
101
102 static Optional<ArchiveFormat> byExtension(String ext, Config cfg) {
103 ArchiveFormat format = BY_EXT.get(ext);
104 if (format == null) {
David Pursehouse7a7f5472016-10-14 09:59:20 +0900105 return Optional.empty();
Dave Borowitzded109a2014-03-03 15:25:39 -0500106 }
Dave Borowitzc8a15682013-07-02 14:33:08 -0700107 String[] formats = cfg.getStringList("archive", null, "format");
108 if (formats.length == 0) {
Dave Borowitzded109a2014-03-03 15:25:39 -0500109 return Optional.of(format);
110 }
David Pursehousec9621972016-10-17 14:56:57 +0900111 return Arrays.stream(formats)
112 .filter(format.name()::equalsIgnoreCase)
113 .findFirst()
114 .map(x -> format);
Dave Borowitzc8a15682013-07-02 14:33:08 -0700115 }
116}