blob: 152a50afc85f95009a6bb0eb071d3bb93ebfbedf [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;
18import com.google.common.base.Optional;
19import com.google.common.collect.ImmutableMap;
20import com.google.common.collect.ImmutableSet;
Dave Borowitzc8a15682013-07-02 14:33:08 -070021
22import 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;
38 static {
39 ImmutableMap.Builder<String, ArchiveFormat> byExt = ImmutableMap.builder();
40 for (ArchiveFormat format : ArchiveFormat.values()) {
41 for (String ext : format.getSuffixes()) {
42 byExt.put(ext.toLowerCase(), format);
43 }
44 }
45 BY_EXT = byExt.build();
46 }
Dave Borowitzc8a15682013-07-02 14:33:08 -070047
Dave Borowitz29168c62014-04-16 12:08:23 -070048 /** Unregister all JGit archive formats supported by Gitiles. */
49 public static void unregisterAll() {
50 for (ArchiveFormat fmt : values()) {
Dave Borowitz36eb26d2014-04-19 16:42:32 -070051 ArchiveCommand.unregisterFormat(fmt.getRegisteredName());
Dave Borowitz29168c62014-04-16 12:08:23 -070052 }
53 }
54
Dave Borowitzc8a15682013-07-02 14:33:08 -070055 private final ArchiveCommand.Format<?> format;
56 private final String mimeType;
57
58 private ArchiveFormat(String mimeType, ArchiveCommand.Format<?> format) {
59 this.format = format;
60 this.mimeType = mimeType;
Dave Borowitz36eb26d2014-04-19 16:42:32 -070061 ArchiveCommand.registerFormat(getRegisteredName(), format);
62 }
63
64 String getRegisteredName() {
65 return getShortName();
Dave Borowitzc8a15682013-07-02 14:33:08 -070066 }
67
68 String getShortName() {
69 return name().toLowerCase();
70 }
71
72 String getMimeType() {
73 return mimeType;
74 }
75
76 String getDefaultSuffix() {
77 return getSuffixes().iterator().next();
78 }
79
80 Iterable<String> getSuffixes() {
81 return format.suffixes();
82 }
83
84 static ArchiveFormat getDefault(Config cfg) {
Dave Borowitzded109a2014-03-03 15:25:39 -050085 for (String allowed : cfg.getStringList("archive", null, "format")) {
86 Optional<ArchiveFormat> result =
87 Enums.getIfPresent(ArchiveFormat.class, allowed.toUpperCase());
88 if (result.isPresent()) {
89 return result.get();
90 }
91 }
92 return TGZ;
Dave Borowitzc8a15682013-07-02 14:33:08 -070093 }
94
Dave Borowitzded109a2014-03-03 15:25:39 -050095 static ImmutableSet<String> allExtensions() {
96 return BY_EXT.keySet();
97 }
98
99 static Optional<ArchiveFormat> byExtension(String ext, Config cfg) {
100 ArchiveFormat format = BY_EXT.get(ext);
101 if (format == null) {
102 return Optional.absent();
103 }
Dave Borowitzc8a15682013-07-02 14:33:08 -0700104 String[] formats = cfg.getStringList("archive", null, "format");
105 if (formats.length == 0) {
Dave Borowitzded109a2014-03-03 15:25:39 -0500106 return Optional.of(format);
107 }
108 for (String allowed : formats) {
109 if (format.name().equals(allowed.toUpperCase())) {
110 return Optional.of(format);
Dave Borowitzc8a15682013-07-02 14:33:08 -0700111 }
112 }
Dave Borowitzded109a2014-03-03 15:25:39 -0500113 return Optional.absent();
Dave Borowitzc8a15682013-07-02 14:33:08 -0700114 }
115}