Most development tools and platforms today support some level of configuration as code - meaning that the state of the tool can be created from some level of code - usually a hidden file or folder in the root of the repository.
You probably noticed the .github folder in the root of this repository. It holds configuration data for GitHub Classroom (classroom), GitHub Actions (workflows) and I even created my own scope called template I used to copy the state of the template repository to this one.
Often configuration files are written in either yaml or json. While yaml and json are the most commonly used configuration file formats git uses it's own format - a format sometimes referred to as ini-files because they use the same format as ini-files which Microsoft used up until Windows 3.1 - before the dreaded Registry Database was introduced in Windows '95.
Git has an inline editor/reader called config.
[ ] Try to run:
git config --get user.name
[ ] Or to get everything that git knows - and at the same time learn where git knows it from run:
git config --list --show-origin
As always run the command with the --help switch - if you need help:
git config --help
git config can not only read - it can also write. Imagine the following scenario:
I would like to have a command that could always tell me the root of the repository I'm in. Even if my working directory is something else it should always just display the repository root.
It turns out that there actually is such a git command already
[ ] try to run:
git root
Argh that wasn't it - although that would have been a good intuitively memorable name for such a command.
[ ] Try this one instead:
git rev-parse --show-toplevel
...eeeh OK! rev-parse - what kind og command name is that? - It's not short and neat, and not at all easy to remember. But we can use git config, to create an alias.
[ ] try this snippet:
git config --global --add alias.root "rev-parse --show-toplevel"
# Now it works!
git root
In the script you used to copy the issues over cpissues.sh - remember? - I can now use the new command to read settings from my own gitconfig file which I know is in .github/template folder - but only if you are in the repository root.
[ ] This one always work - regardless of what you working directory is (as long as it's inside a repository):
Dive into Command Substitution
---
`git root` is a command that returns a string - and in the example above I use the output of the command to concatenate a string to build the qualified path to the config file.
It's supported in bash - and the concept goes by the name _Command Substitution_
Here's what the [man page for bash](https://www.gnu.org/software/bash/manual/bash.html) has to say:
>Command substitution allows the output of a command to replace the command name. >There are two forms:
> ```
> $(command)
> ```
> or
> ```
> `command`
>```
Consequently you can have used:
```shell
git config --file `git root`/.github/template/gitconfig --get template.repo
```
---
Most development tools and platforms today support some level of configuration as code - meaning that the state of the tool can be created from some level of code - usually a hidden file or folder in the root of the repository.
You probably noticed the
.github
folder in the root of this repository. It holds configuration data for GitHub Classroom (classroom
), GitHub Actions (workflows
) and I even created my own scope calledtemplate
I used to copy the state of the template repository to this one.Often configuration files are written in either
yaml
orjson
. Whileyaml
andjson
are the most commonly used configuration file formats git uses it's own format - a format sometimes referred to as ini-files because they use the same format as ini-files which Microsoft used up until Windows 3.1 - before the dreaded Registry Database was introduced in Windows '95.Git has an inline editor/reader called
config
.[ ] Try to run:
[ ] Or to get everything that git knows - and at the same time learn where git knows it from run:
As always run the command with the
--help
switch - if you need help:git config can not only read - it can also write. Imagine the following scenario:
It turns out that there actually is such a git command already
[ ] try to run:
Argh that wasn't it - although that would have been a good intuitively memorable name for such a command.
[ ] Try this one instead:
...eeeh OK!
rev-parse
- what kind og command name is that? - It's not short and neat, and not at all easy to remember. But we can use git config, to create an alias.[ ] try this snippet:
In the script you used to copy the issues over
cpissues.sh
- remember? - I can now use the new command to read settings from my owngitconfig
file which I know is in.github/template
folder - but only if you are in the repository root.[ ] This one always work - regardless of what you working directory is (as long as it's inside a repository):
Dive into Command Substitution
--- `git root` is a command that returns a string - and in the example above I use the output of the command to concatenate a string to build the qualified path to the config file. It's supported in bash - and the concept goes by the name _Command Substitution_ Here's what the [man page for bash](https://www.gnu.org/software/bash/manual/bash.html) has to say: >Command substitution allows the output of a command to replace the command name. >There are two forms: > ``` > $(command) > ``` > or > ``` > `command` >``` Consequently you can have used: ```shell git config --file `git root`/.github/template/gitconfig --get template.repo ``` ---