Skip to content
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
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -384,9 +384,10 @@ export FORGIT_LOG_FZF_OPTS='

## other options

| Option | Description | Default |
|-----------------------------|-------------------------------------------|-----------------------------------------------|
| `FORGIT_LOG_FORMAT` | git log format | `%C(auto)%h%d %s %C(black)%C(bold)%cr%Creset` |
| Option | Description | Default |
|--------------------------------------------|-----------------------------------------------|-----------------------------------------------|
| `FORGIT_CHECKOUT_BRANCH_EXCLUDE_WORKTREES` | exclude worktree branches in `gcb` | `true` |
| `FORGIT_LOG_FORMAT` | git log format | `%C(auto)%h%d %s %C(black)%C(bold)%cr%Creset` |
| `FORGIT_PREVIEW_CONTEXT` | lines of diff context in preview mode | 3 |
| `FORGIT_FULLSCREEN_CONTEXT` | lines of diff context in full-screen mode | 10 |
| `FORGIT_DIR_VIEW` | command used to preview directories | `tree` if available, otherwise `find` |
Comment on lines +387 to 393
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the rest of the table should be widened as well to keep the formatting consistent.

Expand Down
11 changes: 10 additions & 1 deletion bin/git-forgit
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ _forgit_emojify() {
fi
}

_forgit_worktree_filter() {
if [[ "${FORGIT_CHECKOUT_BRANCH_EXCLUDE_WORKTREES:-true}" == "true" ]]; then
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, one more thing: we're evaluating this kind of env variables at a central place in the code (search for FORGIT_LOG_GRAPH_ENABLE for example). I think we should do it like that with this variable, too.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there actually a case where somebody would not want to exclude worktrees? If they don't work with checkout we might want to always exclude them...

# Worktree branches are prefixed with '+' in 'git branch' output
grep -v '^[+]'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From my understanding, the bracket expression isn't necessary or is there any reason we can't simply use:

Suggested change
grep -v '^[+]'
grep -v '^+'

else
cat
fi
}

# extract the first git sha occurring in the input and strip trailing newline
_forgit_extract_sha() {
grep -Eo '[a-f0-9]+' | head -1 | tr -d '[:space:]'
Expand Down Expand Up @@ -879,7 +888,7 @@ _forgit_checkout_branch() {
"
_forgit_checkout_branch_branch_git_opts=()
_forgit_parse_array _forgit_checkout_branch_branch_git_opts "$FORGIT_CHECKOUT_BRANCH_BRANCH_GIT_OPTS"
branch="$(git branch --color=always "${_forgit_checkout_branch_branch_git_opts[@]:---all}" | LC_ALL=C sort -k1.1,1.1 -rs |
branch="$(git branch --color=always "${_forgit_checkout_branch_branch_git_opts[@]:---all}" | _forgit_worktree_filter | LC_ALL=C sort -k1.1,1.1 -rs |
FZF_DEFAULT_OPTS="$opts" fzf | awk '{print $1}')"
[[ -z "$branch" ]] && return 1

Expand Down
46 changes: 46 additions & 0 deletions tests/worktree_exclude.test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env bash

function set_up() {
source bin/git-forgit
}

function test_forgit_worktree_filter_excludes_plus_lines_by_default() {
local input="+ worktree-branch
* main-branch
other-branch"

# Default behavior (true)
local actual
actual=$(echo "$input" | _forgit_worktree_filter)
local expected="* main-branch
other-branch"

assert_same "$expected" "$actual"
}

function test_forgit_worktree_filter_excludes_plus_lines_when_explicitly_enabled() {
export FORGIT_CHECKOUT_BRANCH_EXCLUDE_WORKTREES="true"
local input="+ worktree-branch
* main-branch"

local actual
actual=$(echo "$input" | _forgit_worktree_filter)
local expected="* main-branch"

assert_same "$expected" "$actual"
unset FORGIT_CHECKOUT_BRANCH_EXCLUDE_WORKTREES
}

function test_forgit_worktree_filter_includes_plus_lines_when_disabled() {
export FORGIT_CHECKOUT_BRANCH_EXCLUDE_WORKTREES="false"
local input="+ worktree-branch
* main-branch"

local actual
actual=$(echo "$input" | _forgit_worktree_filter)
local expected="+ worktree-branch
* main-branch"

assert_same "$expected" "$actual"
unset FORGIT_CHECKOUT_BRANCH_EXCLUDE_WORKTREES
}