View Javadoc
1   /*
2    * Copyright (C) 2014, Arthur Daussy <arthur.daussy@obeo.fr>
3    * Copyright (C) 2015, Christian Halstrick <christian.halstrick@sap.com> and others
4    *
5    * This program and the accompanying materials are made available under the
6    * terms of the Eclipse Distribution License v. 1.0 which is available at
7    * https://www.eclipse.org/org/documents/edl-v10.php.
8    *
9    * SPDX-License-Identifier: BSD-3-Clause
10   */
11  package org.eclipse.jgit.internal.storage.file;
12  
13  import java.io.File;
14  import java.io.IOException;
15  
16  import org.eclipse.jgit.attributes.AttributesNode;
17  import org.eclipse.jgit.lib.CoreConfig;
18  import org.eclipse.jgit.lib.Repository;
19  import org.eclipse.jgit.util.FS;
20  
21  /**
22   * Attribute node loaded from global system-wide file.
23   */
24  public class GlobalAttributesNode extends AttributesNode {
25  	final Repository repository;
26  
27  	/**
28  	 * Constructor for GlobalAttributesNode.
29  	 *
30  	 * @param repository
31  	 *            the {@link org.eclipse.jgit.lib.Repository}.
32  	 */
33  	public GlobalAttributesNode(Repository repository) {
34  		this.repository = repository;
35  	}
36  
37  	/**
38  	 * Load the attributes node
39  	 *
40  	 * @return the attributes node
41  	 * @throws java.io.IOException
42  	 */
43  	public AttributesNode load() throws IOException {
44  		AttributesNode r = new AttributesNode();
45  
46  		FS fs = repository.getFS();
47  		String path = repository.getConfig().get(CoreConfig.KEY)
48  				.getAttributesFile();
49  		if (path != null) {
50  			File attributesFile;
51  			if (path.startsWith("~/")) { //$NON-NLS-1$
52  				attributesFile = fs.resolve(fs.userHome(),
53  						path.substring(2));
54  			} else {
55  				attributesFile = fs.resolve(null, path);
56  			}
57  			FileRepository.AttributesNodeProviderImpl.loadRulesFromFile(r, attributesFile);
58  		}
59  		return r.getRules().isEmpty() ? null : r;
60  	}
61  }