Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add global config file support for SharedOptions #2207

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ this release has no changes to `0.26.0` but provides windows binaries that were
* support `core.commitChar` filtering [[@concelare](https://github.com/concelare)] ([#2136](https://github.com/extrawurst/gitui/issues/2136))
* allow reset in branch popup ([#2170](https://github.com/extrawurst/gitui/issues/2170))
* respect configuration for remote when pushing [[@cruessler](https://github.com/cruessler)] ([#2156](https://github.com/extrawurst/gitui/issues/2156))
* support global options [[@remique](https://github.com/remique)] ([#2140](https://github.com/extrawurst/gitui/issues/2140))

### Changed
* Make info and error message popups scrollable [[@MichaelAug](https://github.com/MichaelAug)] ([#1138](https://github.com/extrawurst/gitui/issues/1138))
Expand Down
32 changes: 32 additions & 0 deletions GLOBAL_OPTIONS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Global options

You can set global options that will take effect over the entire system for GitUI. Note however, that each time GitUI closes, it saves a local config file (`./.git/gitui.ron`) that has a higher order precedence. Meaning, if you had changed global config while already having local file for given repository, the changes will not be visible.

The reason behind this decision is local file has additional fields saved which facilitates GitUI for a specific repository (eg. `tab` which allows to open up GitUI in the last tab it was closed with).

The precedence of fetching the options is:

1. Use **local** options file. _If not found then:_
2. Use **global** options file. _If not found then:_
3. Use default values.

To set up global options create `gitui.ron` file:

```
(
diff: (
ignore_whitespace: false,
context: 3,
interhunk_lines: 2,
),
status_show_untracked: None,
)
```

The options file format based on the [Ron file format](https://github.com/ron-rs/ron).
The location of the file depends on your OS:

- `$HOME/.config/gitui/gitui.ron` (mac)
- `$XDG_CONFIG_HOME/gitui/gitui.ron` (linux using XDG)
- `$HOME/.config/gitui/gitui.ron` (linux)
- `%APPDATA%/gitui/gitui.ron` (Windows)
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,23 +267,29 @@ However, you can customize everything to your liking: See [Themes](THEMES.md).

The key bindings can be customized: See [Key Config](KEY_CONFIG.md) on how to set them to `vim`-like bindings.

## 12. <a name="sponsoring"></a> Sponsoring <small><sup>[Top ▲](#table-of-contents)</sup></small>
## 12. <a name="options"></a> Global options <small><sup>[Top ▲](#table-of-contents)</sup></small>

The key bindings can be customized: See [Key Config](KEY_CONFIG.md) on how to set them to `vim`-like bindings.

You can set Global Options: See [Global Options](GLOBAL_OPTIONS.md) on how to set it up.

## 13. <a name="sponsoring"></a> Sponsoring <small><sup>[Top ▲](#table-of-contents)</sup></small>

[![github](https://img.shields.io/badge/-GitHub%20Sponsors-fafbfc?logo=GitHub%20Sponsors)](https://github.com/sponsors/extrawurst)

## 13. <a name="inspiration"></a> Inspiration <small><sup>[Top ▲](#table-of-contents)</sup></small>
## 14. <a name="inspiration"></a> Inspiration <small><sup>[Top ▲](#table-of-contents)</sup></small>

- [lazygit](https://github.com/jesseduffield/lazygit)
- [tig](https://github.com/jonas/tig)
- [GitUp](https://github.com/git-up/GitUp)
- It would be nice to come up with a way to have the map view available in a terminal tool
- [git-brunch](https://github.com/andys8/git-brunch)

## 14. <a name="contributing"></a> Contributing <small><sup>[Top ▲](#table-of-contents)</sup></small>
## 15. <a name="contributing"></a> Contributing <small><sup>[Top ▲](#table-of-contents)</sup></small>

See [CONTRIBUTING.md](CONTRIBUTING.md).

## 15. <a name="contributors"></a> Contributors <small><sup>[Top ▲](#table-of-contents)</sup></small>
## 16. <a name="contributors"></a> Contributors <small><sup>[Top ▲](#table-of-contents)</sup></small>

Thanks goes to all the contributors that help make GitUI amazing! ❤️

Expand Down
18 changes: 15 additions & 3 deletions src/options.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::args::get_app_config_path;

use anyhow::Result;
use asyncgit::sync::{
diff::DiffOptions, repo_dir, RepoPathRef,
Expand Down Expand Up @@ -26,6 +28,8 @@ struct OptionsData {

const COMMIT_MSG_HISTORY_LENGTH: usize = 20;

const OPTIONS_FILENAME: &str = "gitui.ron";

#[derive(Clone)]
pub struct Options {
repo: RepoPathRef,
Expand Down Expand Up @@ -144,11 +148,19 @@ impl Options {
}

fn read(repo: &RepoPathRef) -> Result<OptionsData> {
let dir = Self::options_file(repo)?;
let local_file = Self::options_file(repo)?;

let app_home = get_app_config_path()?;
let config_file = app_home.join(OPTIONS_FILENAME);

let mut f = match File::open(local_file) {
Ok(file) => file,
Err(_) => File::open(config_file)?,
};

let mut f = File::open(dir)?;
let mut buffer = Vec::new();
f.read_to_end(&mut buffer)?;

Ok(from_bytes(&buffer)?)
}

Expand All @@ -167,7 +179,7 @@ impl Options {

fn options_file(repo: &RepoPathRef) -> Result<PathBuf> {
let dir = repo_dir(&repo.borrow())?;
let dir = dir.join("gitui");
let dir = dir.join(OPTIONS_FILENAME);
Ok(dir)
}
}
Loading