Documentation ¶
Overview ¶
Package config implements encoding and decoding of git config files.
Configuration File ------------------ The Git configuration file contains a number of variables that affect the Git commands' behavior. The `.git/config` file in each repository is used to store the configuration for that repository, and `$HOME/.gitconfig` is used to store a per-user configuration as fallback values for the `.git/config` file. The file `/etc/gitconfig` can be used to store a system-wide default configuration. The configuration variables are used by both the Git plumbing and the porcelains. The variables are divided into sections, wherein the fully qualified variable name of the variable itself is the last dot-separated segment and the section name is everything before the last dot. The variable names are case-insensitive, allow only alphanumeric characters and `-`, and must start with an alphabetic character. Some variables may appear multiple times; we say then that the variable is multivalued. Syntax ~~~~~~ The syntax is fairly flexible and permissive; whitespaces are mostly ignored. The '#' and ';' characters begin comments to the end of line, blank lines are ignored. The file consists of sections and variables. A section begins with the name of the section in square brackets and continues until the next section begins. Section names are case-insensitive. Only alphanumeric characters, `-` and `.` are allowed in section names. Each variable must belong to some section, which means that there must be a section header before the first setting of a variable. Sections can be further divided into subsections. To begin a subsection put its name in double quotes, separated by space from the section name, in the section header, like in the example below: -------- [section "subsection"] -------- Subsection names are case sensitive and can contain any characters except newline (doublequote `"` and backslash can be included by escaping them as `\"` and `\\`, respectively). Section headers cannot span multiple lines. Variables may belong directly to a section or to a given subsection. You can have `[section]` if you have `[section "subsection"]`, but you don't need to. There is also a deprecated `[section.subsection]` syntax. With this syntax, the subsection name is converted to lower-case and is also compared case sensitively. These subsection names follow the same restrictions as section names. All the other lines (and the remainder of the line after the section header) are recognized as setting variables, in the form 'name = value' (or just 'name', which is a short-hand to say that the variable is the boolean "true"). The variable names are case-insensitive, allow only alphanumeric characters and `-`, and must start with an alphabetic character. A line that defines a value can be continued to the next line by ending it with a `\`; the backquote and the end-of-line are stripped. Leading whitespaces after 'name =', the remainder of the line after the first comment character '#' or ';', and trailing whitespaces of the line are discarded unless they are enclosed in double quotes. Internal whitespaces within the value are retained verbatim. Inside double quotes, double quote `"` and backslash `\` characters must be escaped: use `\"` for `"` and `\\` for `\`. The following escape sequences (beside `\"` and `\\`) are recognized: `\n` for newline character (NL), `\t` for horizontal tabulation (HT, TAB) and `\b` for backspace (BS). Other char escape sequences (including octal escape sequences) are invalid. Includes ~~~~~~~~ You can include one config file from another by setting the special `include.path` variable to the name of the file to be included. The variable takes a pathname as its value, and is subject to tilde expansion. The included file is expanded immediately, as if its contents had been found at the location of the include directive. If the value of the `include.path` variable is a relative path, the path is considered to be relative to the configuration file in which the include directive was found. See below for examples. Example ~~~~~~~ # Core variables [core] ; Don't trust file modes filemode = false # Our diff algorithm [diff] external = /usr/local/bin/diff-wrapper renames = true [branch "devel"] remote = origin merge = refs/heads/devel # Proxy settings [core] gitProxy="ssh" for "kernel.org" gitProxy=default-proxy ; for the rest [include] path = /path/to/foo.inc ; include by absolute path path = foo ; expand "foo" relative to the current file path = ~/foo ; expand "foo" in your `$HOME` directory
Index ¶
- Constants
- type Comment
- type Config
- func (c *Config) AddOption(section string, subsection string, key string, value string) *Config
- func (c *Config) HasSection(name string) bool
- func (c *Config) RemoveSection(name string) *Config
- func (c *Config) RemoveSubsection(section string, subsection string) *Config
- func (c *Config) Section(name string) *Section
- func (c *Config) SetOption(section string, subsection string, key string, value string) *Config
- type Decoder
- type Encoder
- type Include
- type Includes
- type Option
- type Options
- type Section
- func (s *Section) AddOption(key string, value string) *Section
- func (s *Section) HasOption(key string) bool
- func (s *Section) HasSubsection(name string) bool
- func (s *Section) IsName(name string) bool
- func (s *Section) Option(key string) string
- func (s *Section) OptionAll(key string) []string
- func (s *Section) RemoveOption(key string) *Section
- func (s *Section) RemoveSubsection(name string) *Section
- func (s *Section) SetOption(key string, value string) *Section
- func (s *Section) Subsection(name string) *Subsection
- type Sections
- type Subsection
- func (s *Subsection) AddOption(key string, value string) *Subsection
- func (s *Subsection) HasOption(key string) bool
- func (s *Subsection) IsName(name string) bool
- func (s *Subsection) Option(key string) string
- func (s *Subsection) OptionAll(key string) []string
- func (s *Subsection) RemoveOption(key string) *Subsection
- func (s *Subsection) SetOption(key string, value ...string) *Subsection
- type Subsections
Constants ¶
const ( // NoSubsection token is passed to Config.Section and Config.SetSection to // represent the absence of a section. NoSubsection = "" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
Config contains all the sections, comments and includes from a config file.
func (*Config) AddOption ¶
AddOption adds an option to a given section and subsection. Use the NoSubsection constant for the subsection argument if no subsection is wanted.
func (*Config) HasSection ¶
HasSection checks if the Config has a section with the specified name.
func (*Config) RemoveSection ¶
RemoveSection removes a section from a config file.
func (*Config) RemoveSubsection ¶
RemoveSubsection remove a subsection from a config file.
type Decoder ¶
A Decoder reads and decodes config files from an input stream.
func NewDecoder ¶
NewDecoder returns a new decoder that reads from r.
type Encoder ¶
type Encoder struct {
// contains filtered or unexported fields
}
An Encoder writes config files to an output stream.
func NewEncoder ¶
NewEncoder returns a new encoder that writes to w.
type Option ¶
type Option struct { // Key preserving original caseness. // Use IsKey instead to compare key regardless of caseness. Key string // Original value as string, could be not normalized. Value string }
Option defines a key/value entity in a config file.
type Options ¶
type Options []*Option
func (Options) Get ¶
Get gets the value for the given key if set, otherwise it returns the empty string.
Note that there is no difference ¶
This matches git behaviour since git v1.8.1-rc1, if there are multiple definitions of a key, the last one wins.
See: http://article.gmane.org/gmane.linux.kernel/1407184
In order to get all possible values for the same key, use GetAll.
type Section ¶
type Section struct { Name string Options Options Subsections Subsections }
Section is the representation of a section inside git configuration files. Each Section contains Options that are used by both the Git plumbing and the porcelains. Sections can be further divided into subsections. To begin a subsection put its name in double quotes, separated by space from the section name, in the section header, like in the example below:
[section "subsection"]
All the other lines (and the remainder of the line after the section header) are recognized as option variables, in the form "name = value" (or just name, which is a short-hand to say that the variable is the boolean "true"). The variable names are case-insensitive, allow only alphanumeric characters and -, and must start with an alphabetic character:
[section "subsection1"] option1 = value1 option2 [section "subsection2"] option3 = value2
func (*Section) AddOption ¶
AddOption adds a new Option to the Section. The updated Section is returned.
func (*Section) HasSubsection ¶
HasSubsection checks if the Section has a Subsection with the specified name.
func (*Section) IsName ¶
IsName checks if the name provided is equals to the Section name, case insensitive.
func (*Section) Option ¶
Option return the value for the specified key. Empty string is returned if key does not exists.
func (*Section) OptionAll ¶
OptionAll returns all possible values for an option with the specified key. If the option does not exists, an empty slice will be returned.
func (*Section) RemoveOption ¶
Remove an option with the specified key. The updated Section is returned.
func (*Section) RemoveSubsection ¶
RemoveSubsection removes a subsection from a Section.
func (*Section) SetOption ¶
SetOption adds a new Option to the Section. If the option already exists, is replaced. The updated Section is returned.
func (*Section) Subsection ¶
func (s *Section) Subsection(name string) *Subsection
Subsection returns a Subsection from the specified Section. If the Subsection does not exists, new one is created and added to Section.
type Subsection ¶
func (*Subsection) AddOption ¶
func (s *Subsection) AddOption(key string, value string) *Subsection
AddOption adds a new Option to the Subsection. The updated Subsection is returned.
func (*Subsection) HasOption ¶
func (s *Subsection) HasOption(key string) bool
HasOption checks if the Subsection has an Option with the given key.
func (*Subsection) IsName ¶
func (s *Subsection) IsName(name string) bool
IsName checks if the name of the subsection is exactly the specified name.
func (*Subsection) Option ¶
func (s *Subsection) Option(key string) string
Option returns an option with the specified key. If the option does not exists, empty spring will be returned.
func (*Subsection) OptionAll ¶
func (s *Subsection) OptionAll(key string) []string
OptionAll returns all possible values for an option with the specified key. If the option does not exists, an empty slice will be returned.
func (*Subsection) RemoveOption ¶
func (s *Subsection) RemoveOption(key string) *Subsection
RemoveOption removes the option with the specified key. The updated Subsection is returned.
func (*Subsection) SetOption ¶
func (s *Subsection) SetOption(key string, value ...string) *Subsection
SetOption adds a new Option to the Subsection. If the option already exists, is replaced. The updated Subsection is returned.
type Subsections ¶
type Subsections []*Subsection
func (Subsections) GoString ¶
func (s Subsections) GoString() string