Skip to content

Commit

Permalink
Git prompt using libgit2 (again) (#55)
Browse files Browse the repository at this point in the history
* Added simple Git repository parsing code

The reference (which can be the branch name or commit hash) is obtained by reading `.git/HEAD` because the suggested way (https://stackoverflow.com/questions/12132862) does not work (`git_repository_head` fails) if there are no commits on the branch yet.

* Workflow: install libgit2 on Linux

* Merge and rebase status

* Cherry-picking and reverting statuses

* Reverted workflow change

* Bisecting status

* Full handling of (unborn) branch name, commit ID and tag

* No need to check if reference is branch

`git_branch_name` already does that

* Status listing with callback

* Moved callback function explanation to declaration

* Replaced state `bool`s with single `std::string`

* Added `bare` member

* Renamed `set_*` methods as `establish_*` because they are not setters

* Skip ignored files in status detection

* Completed status detection code

* Use git information from libgit2 instead of `__git_ps1`

* Updated Bash and Zsh to not pass `__git_ps1` output

* Initialise all primitive members

Use this to detect if there is no Git repository.

* Fixed edge case: a file can be new and modified both

* Removed static linking on Windows

Cannot link libgit2 statically

* Renames and minor fixes

* Moved tag detection to separate method

* Detect annotated and unannotated tags both

* Bit-fiddling to set dirty, staged and untracked statuses

* Removed parameter names from method declarations

* Updated documentation

* Added Windows workflow to build libgit2 statically

* Use Git instead of GitHub CLI, which requires a token

* Install to target

* Fixed `make` command

* Windows has `mkdir`

* Install pkg-onfig

* Removed package workflow

* Removed code to load `__git_ps1` in Bash and Zsh
  • Loading branch information
tfpf authored Oct 30, 2024
1 parent 0320831 commit 2e922e4
Show file tree
Hide file tree
Showing 7 changed files with 309 additions and 109 deletions.
2 changes: 1 addition & 1 deletion .bash_aliases
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ _after_command()
local exit_code=$?
[ -z "${__begin_window+.}" ] && return
local last_command=$(history 1)
PS1=$(custom-bash-prompt "$last_command" $exit_code $__begin_window $COLUMNS "$(__git_ps1 %s)" $SHLVL "$PWD")
PS1=$(custom-bash-prompt "$last_command" $exit_code $__begin_window $COLUMNS $SHLVL "$PWD")
unset __begin_window
}

Expand Down
7 changes: 0 additions & 7 deletions .bashrc
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,6 @@ then
_source_one /usr/share/bash-completion/bash_completion /etc/bash_completion
fi

# Showing the Git branch in the primary prompt depends upon a script which must
# be sourced separately on some Linux distributions.
if ! command -v __git_ps1 &>/dev/null
then
_source_one $HOME/Documents/projects/git/contrib/completion/git-prompt.sh /usr/share/git/completion/git-prompt.sh /usr/share/git-core/contrib/completion/git-prompt.sh
fi

PS1='[\u@\h \w]\$ '
PS2='──▶ '
PS3='#? '
Expand Down
69 changes: 0 additions & 69 deletions .github/workflows/package.yml

This file was deleted.

11 changes: 1 addition & 10 deletions .zshrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ _after_command()
local exit_code=$?
[ -z "${__begin_window+.}" ] && return
local last_command=$(history -n -1 2>/dev/null)
PS1=$(custom-zsh-prompt "$last_command" $exit_code $=__begin_window $COLUMNS "$(__git_ps1 %s)" $SHLVL $PWD)
PS1=$(custom-zsh-prompt "$last_command" $exit_code $=__begin_window $COLUMNS $SHLVL $PWD)
unset __begin_window
}

Expand Down Expand Up @@ -75,15 +75,6 @@ rr()
done
}

if ! command -v __git_ps1 &>/dev/null
then
. ~/Documents/projects/git/contrib/completion/git-prompt.sh &>/dev/null \
|| . /usr/lib/git-core/git-sh-prompt &>/dev/null \
|| . /usr/share/git-core/contrib/completion/git-prompt.sh &>/dev/null \
|| . /usr/share/git/completion/git-prompt.sh &>/dev/null \
|| . /usr/share/git/git-prompt.sh &>/dev/null
fi

###############################################################################
# Shell options.
###############################################################################
Expand Down
2 changes: 1 addition & 1 deletion custom-prompt/.clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ BreakBeforeInheritanceComma: false
BreakInheritanceList: BeforeColon
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeComma
BreakConstructorInitializers: AfterColon
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
ColumnLimit: 119
Expand Down
14 changes: 5 additions & 9 deletions custom-prompt/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
CC = gcc
CFLAGS = -fstrict-aliasing -std=c11 -Wall -Wextra
CXXFLAGS = -fstrict-aliasing -std=c++17 -Wall -Wextra
LDLIBS = -lstdc++
CPPFLAGS = $(shell pkg-config --cflags libgit2)
CFLAGS = -fstrict-aliasing -std=c11 -Wall -Wextra -Wno-unused-parameter
CXXFLAGS = -fstrict-aliasing -std=c++17 -Wall -Wextra -Wno-unused-parameter
LDLIBS = -lstdc++ $(shell pkg-config --libs libgit2)

MainSource = custom-prompt.cc
MainBashObject = custom-bash-prompt.o
Expand All @@ -10,12 +11,7 @@ MainZshObject = custom-zsh-prompt.o
MainZshExecutable = bin/$(MainZshObject:.o=)
GetActiveWidObjects = get_active_wid_linux.o get_active_wid_windows.o

ifeq "$(OS)" "Windows_NT"
# When building using something other than Visual Studio, the shared
# libraries the executables are linked with may lie in folders Windows does
# not typically search. Avoid the hassle of fixing that.
LDFLAGS += -static
else
ifneq "$(OS)" "Windows_NT"
UNAME = $(shell uname)
ifeq "$(UNAME)" "Linux"
CPPFLAGS += $(shell pkg-config --cflags glib-2.0 libnotify x11)
Expand Down
Loading

0 comments on commit 2e922e4

Please sign in to comment.