aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit/src/org/eclipse/jgit/storage/file/UserConfigFile.java
blob: c1eaac7b55ee3c554cece1e8ca13c2e1e5cf4c02 (plain)
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
/*
 * Copyright (C) 2023, Thomas Wolf <twolf@apache.org> and others
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Distribution License v. 1.0 which is available at
 * https://www.eclipse.org/org/documents/edl-v10.php.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */
package org.eclipse.jgit.storage.file;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.eclipse.jgit.annotations.NonNull;
import org.eclipse.jgit.errors.ConfigInvalidException;
import org.eclipse.jgit.lib.Config;
import org.eclipse.jgit.util.FS;

/**
 * User (global) git config based on two possible locations,
 * {@code ~/.gitconfig} and {@code $XDG_CONFIG_HOME/git/config}.
 * <p>
 * For reading, both locations are considered, first the XDG file, then the file
 * in the home directory. All updates occur in the last file read that exists,
 * or in the home directory file if neither exists. In other words: if only the
 * XDG file exists, it is updated, otherwise the home directory file is updated.
 * </p>
 *
 * @since 6.7
 */
public class UserConfigFile extends FileBasedConfig {

	private final FileBasedConfig parent;

	/**
	 * Creates a new {@link UserConfigFile}.
	 *
	 * @param parent
	 *            parent {@link Config}; may be {@code null}
	 * @param config
	 *            {@link File} for {@code ~/.gitconfig}
	 * @param xdgConfig
	 *            {@link File} for {@code $XDG_CONFIG_HOME/.gitconfig}
	 * @param fileSystem
	 *            {@link FS} to use for the two files; normally
	 *            {@link FS#DETECTED}
	 */
	public UserConfigFile(Config parent, @NonNull File config,
			@NonNull File xdgConfig, @NonNull FS fileSystem) {
		super(new FileBasedConfig(parent, xdgConfig, fileSystem), config,
				fileSystem);
		this.parent = (FileBasedConfig) getBaseConfig();
	}

	@Override
	public void setStringList(String section, String subsection, String name,
			List<String> values) {
		if (exists() || !parent.exists()) {
			super.setStringList(section, subsection, name, values);
		} else {
			parent.setStringList(section, subsection, name, values);
		}
	}

	@Override
	public void unset(String section, String subsection, String name) {
		if (exists() || !parent.exists()) {
			super.unset(section, subsection, name);
		} else {
			parent.unset(section, subsection, name);
		}
	}

	@Override
	public void unsetSection(String section, String subsection) {
		if (exists() || !parent.exists()) {
			super.unsetSection(section, subsection);
		} else {
			parent.unsetSection(section, subsection);
		}
	}

	@Override
	public boolean removeSection(String section, String subsection) {
		if (exists() || !parent.exists()) {
			return super.removeSection(section, subsection);
		}
		return parent.removeSection(section, subsection);
	}

	@Override
	public boolean isOutdated() {
		return super.isOutdated() || parent.isOutdated();
	}

	@Override
	public void load() throws IOException, ConfigInvalidException {
		if (super.isOutdated()) {
			super.load();
		}
		if (parent.isOutdated()) {
			parent.load();
		}
	}

	@Override
	public void save() throws IOException {
		if (exists() || !parent.exists()) {
			if (exists() || !toText().strip().isEmpty()) {
				super.save();
			}
		} else {
			parent.save();
		}
	}
}