Git Product home page Git Product logo

elvish-gitstatus's Introduction

Gitstatus for Elvish Shell

Gitstatus is a 10x faster alternative to git status and git describe. Its primary use case is to enable fast git prompt in interactive shells.

Elvish is a friendly interactive shell and an expressive programming language. It runs on Linux, BSDs, macOS and Windows.

This Elvish package automatically installs, runs and queries gitstatus and returns the result in a Map:

$ pprint (gitstatus:query $pwd)
[
 &stashes=  0
 &unstaged= 0
 &commits-ahead=    0
 &tag=  ''
 &action=   ''
 &index-size=   0
 &untracked=    1
 &conflicted=   1
 &workdir=  /Users/denis/Documents/Code/elvish-gitstatus
 &local-branch= master
 &remote-branch=    ''
 &commit=   ''
 &is-repository=    $true
 &remote-url=   ''
 &upstream-branch=  $nil
 &commits-behind=   0
 &staged=   0
 &remote-name=  ''
]

The resulting map can then be used to change the prompt. For example:

edit:prompt = {
    git = (gitstatus:query $pwd)

    if (bool $git[is-repository]) {

        # show the branch, or current commit if not on a branch
        branch = ''
        if (eq $git[local-branch] "") {
            branch = $git[commit][:8]
        } else {
            branch = $git[local-branch]
        }

        put '|'
        put (styled $branch red)

        # show a state indicator
        if (or (> $git[unstaged] 0) (> $git[untracked] 0)) {
            put (styled '*' yellow)
        } elif (> $git[staged] 0) {
            put (styled '*' green)
        } elif (> $git[commits-ahead] 0) {
            put (styled '^' yellow)
        } elif (> $git[commits-behind] 0) {
            put (styled 'โŒ„' yellow)
        }

    }
}

Installation

Using the Elvish package manager:

use epm
epm:install github.com/href/elvish-gitstatus

Usage

To query a folder:

use github.com/href/elvish-gitstatus/gitstatus
gitstatus:query /foo/bar

To query the current folder:

gitstatus:query $pwd

To update gitstatus:

gitstatus:update

Notes

Gitstatus is run in the background as a separate process. The binaries are automatically downloaded from the gitstatus repository and run once per shell process (no sharing between shell processes).

Processes should use fairly small amounts of memory (<2MiB on my system).

I have not yet tested this outside of my Macbook and I use the latest commit of Elvish. So your mileage my vary on other systems (issues and PRs welcome).

Gitstatus needs to store a binary locally. This is currently done at the following folder:

~/.elvish/package-data/gitstatus

This means that you might need to update the .gitignore file of your dotfiles if you check them into git.

Fields

result = gitstatus:query /foo/bar

result[is-repository]

$true if the given folder is part of a git repository. Note that all other fields are set to $nil if the given folder is not part of a git repository.

result[workdir]

The root folder of the git repository.

result[commit]

The commit hash of the current commit.

result[local-branch]

The name of the local branch.

result[upstream-branch]

The name of the upstream branch.

result[remote-name]

The name of the remote.

result[remote-url]

The URL of the remote.

result[action]

The current repository state or active action (e.g. "rebase").

result[index-size]

The number of files in the index.

result[staged]

The number of staged files. Limited to 1 by default (see configuration section).

result[unstaged]

The number of unstaged files. Limited to 1 by default (see configuration section).

result[conflicted]

The number of conflicted files. Limited to 1 by default (see configuration section).

result[untracked]

The number of untracked files. Limited to 1 by default (see configuration section).

result[commits-ahead]

The number of commits ahead of the remote.

result[commits-behind]

The number of commits behind the remote.

result[stashes]

The number of stashes.

result[tag]

The current tag.

Configuration

Configuration can be done via environment variables. It should be done before querying gitstatus via gitstatus:query. If done later, the daemon has to be restarted using gitstatus:stop and gitstatus:start.

Max Staged, Unstaged, Untracked

By default, gitstatus limits the number of staged, unstaged and untracked files that it enumerates. So those values are either set to 1 or to 0. This is of course faster than providing an accurate count.

If you need to know the exact number of files (or of there are one or many), then you should set the configuration as follows (here with the example value of 10):

use gitstatus

$E:GITSTATUS_MAX_NUM_STAGED = "10"
$E:GITSTATUS_MAX_NUM_UNSTAGED = "10"
$E:GITSTATUS_MAX_NUM_UNTRACKED = "10"
$E:GITSTATUS_MAX_NUM_CONFLICTED = "10"

If you want an accurate count use "-1", disabling the limit.

elvish-gitstatus's People

Contributors

href avatar iandol avatar krader1961 avatar mmlb avatar wizhi avatar zzamboni avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

elvish-gitstatus's Issues

Update for V0.17

There are a couple of syntax changes that cause lots of deprecation warnings when running this module.

The downloaded gitstatusd binary should be saved to an arch specific name

I sync my home directory (most of if it, anyway) to my other systems. The problem is that this elvish module saves the appropriate downloaded binary to ~/.elvish/package-data/gitstatusd. This causes problems if that directory is shared by platforms that have different operating systems (such as macOS and Linux) or architectures (such as ppc and x86_64). The downloaded binary should include the OS and architecture in the filename it is saved to. Note that this is easier, and cheaper, to do now that we have $platform:os and $platform:arch.

prompt function error: want 0 arguments, got 1

This seems to be an issue with elvish-gitstatus and Elvish HEAD. Somewhat at random, the following error will pop up:

prompt function error: want 0 arguments, got 1

This cannot be reliably triggered and it is unclear what causes it, though it looks like a race-condition.

Compatibility with current Elvish versions

There have been some recent changes to Elvish that require updating this module. Specifically, \ as a line continuation has been replaced by ^. See https://elv.sh/blog/0.14.0-release-notes.html. While both are supported by anyone using the official 0.14 release anyone building from the master branch will see parse errors:

Exception: Multiple parse errors:
  should be '}'
    /Users/krader/.elvish/lib/github.com/href/elvish-gitstatus/gitstatus.elv, line 69:
          http-get https://raw.githubusercontent.com/romkatv/gitstatus/master/install.info \
  unexpected rune '\\'
    /Users/krader/.elvish/lib/github.com/href/elvish-gitstatus/gitstatus.elv, line 69:
          http-get https://raw.githubusercontent.com/romkatv/gitstatus/master/install.info \
Traceback:
  /Users/krader/.elvish/lib/github.com/zzamboni/elvish-themes/chain.elv, line 12:
    use github.com/href/elvish-gitstatus/gitstatus
  /Users/krader/.elvish/rc.elv, line 81:
    use github.com/zzamboni/elvish-themes/chain

The gitstatusd installation fails on FreeBSD 11.1

I recently attempted to use Elvish on FreeBSD 11.1 using the Elvish config I use on macOS. That config uses this module to display Git status in my prompt. On FreeBSD it fails with these errors:

???> tar: Error opening archive: Failed to open '/dev/sa0'
[prompt error] (curl exited with 23 | tar exited with 1)
see stack trace with "use exc; exc:show $edit:exceptions[0]"

The error is due to the tar in function install not explicitly specifying it should read from stdin. On most platforms reading from stdin is the default. On FreeBSD 11.1 the default is to read from /dev/sa0. If you instead explicitly specify reading from stdin, by including -f - in the arguments to tar, it will work on all platforms.

I modified my local copy of this package and confirmed my proposed change works on macOS, FreeBSD, and Linux. That is,

http-get (download-url $version) | tar -x -z -C $appdir -f -

broken on ubuntu lts

I tried using zzamboni's chain them for elvish but ran into an issue with this module. I'm running ubuntu 18.04.3, and line #217 fails with error:

???> /bin/sh: 1: read: Illegal option -d prompt function error: sh exited with 2

zzamboni helped debug the issue and it seems that while on MacOS, sh will call bash, it does not on ubuntu. Changing that line to explicitly call bash fixed the error.

Though, with this issue being filed and implemented, updating to utilize the new functionality may be the best path forward.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.