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

Show branch track stats for active branches #34

Merged
merged 9 commits into from
Feb 1, 2022

Conversation

RangerCD
Copy link
Contributor

@RangerCD RangerCD commented Jan 4, 2022

This PR will add track stat after branch name for each active branch:

🌳 3 active branches
feat/branch-stats    1↑   ↓ Add ☁ for remote-only branch                               now RangerCD
test              ☁         Match branches without upstream by name                     4h RangerCD
main                  ↑   ↓ Bump ssh_config to v1.1.0                                   1w muesli

Each track stat has 2 parts:

  • Stat indicator
  • Ahead/behind metrics

Stat indicator

Indicator Color Meaning
☁ Cyan Remote-only branch
↻ Red Outdated reference, need git fetch
- Reference is up-to-date

Ahead/behind metrics

In format of xxx↑xxx↓, corresponding to git rev-list --left-right --count <local branch>...<remote branch>.
Some special cases:

  • Any metric equals to 0, number will be hiden
  • Any metric greater than 99 will be shown as 99+
  • If both ahead/behind are 0, arrows are shown in green
  • If stat indicator is ☁, nothing shows here
  • If stat indicator is ↻, metrics are not accurate unless git fetch updates remote branch reference

TODO:

  • Show ahead/behind
  • Get stats after filtering branches
  • Mark 0↑0↓ with a different symbol
  • Mark remote branches out of sync with remote repo
  • Faster process speed(similar to git rev-list)
  • Match branches without upstream by name
  • Mark remote-only branch

@muesli
Copy link
Owner

muesli commented Jan 4, 2022

Looks like quite some progress, amazing!

I seem to hit the TODO in track.go:68 for most of my local tests however and haven't quite wrapped my head around why that happens. Since you marked it as a TODO, though, I guess you do, and I'll just wait for you to finish this up. Just let me know whenever you are ready for a review!

@RangerCD
Copy link
Contributor Author

RangerCD commented Jan 4, 2022

The way I implement this is to compare two branches locally, just like running command git rev-list --left-right --count main...origin/main. The problem is that origin/main is out of sync with the real main on remote.
For example:

C <- main on Github
|
B <- origin/main
|
A <- main
|
  1. Clone a repo from Github when main pointed to commit A
  2. git fetch after someone pushes main with commit B
  3. Run gitty after someone pushes main with commit C

main from Github API is pointed to commit C, but there is no commit C in local repo unless performing a git fetch.
I don't think it's a good idea to perform a git fetch with gitty, maybe all we can do is to put an "out of sync" symbol after branch name.

@muesli
Copy link
Owner

muesli commented Jan 4, 2022

I don't think it's a good idea to perform a git fetch with gitty, maybe all we can do is to put an "out of sync" symbol after branch name.

I agree, gitty should always operate read-only. An "out-of-sync" indicator may be good enough. We could probably also show the diff-stats from what we can gather locally in such scenarios, even if it may not be fully accurate.

@muesli muesli added the enhancement New feature or request label Jan 4, 2022
@RangerCD
Copy link
Contributor Author

RangerCD commented Jan 7, 2022

After some benchmark, I realized that current implementation using github.com/go-git/go-git/v5/plumbing/revlist is too slow on large repo, even slower than directly running git rev-list. I'm trying to implement this in another way.

@RangerCD
Copy link
Contributor Author

RangerCD commented Jan 7, 2022

Now it should be fast enough.

@muesli
Copy link
Owner

muesli commented Jan 8, 2022

Noticed a little weirdness while playing with it:
image

@muesli
Copy link
Owner

muesli commented Jan 8, 2022

Additionally, I've just added a new commit to my branch hard-soft-lint, but it didn't show my local branch is ahead of the remote:
image

@RangerCD
Copy link
Contributor Author

RangerCD commented Jan 8, 2022

I've replaced 0↑/0↓ with only ↑/↓. Thought it would be helpful to concentrate on non-zero value. I'll revert this if it's a bad idea.

For the second problem, please run git branch -vv then check the line of branch hard-soft-lint. It looks like the remote name recognized by gitty is different from the one that hard-soft-lint is tracking.

@muesli
Copy link
Owner

muesli commented Jan 9, 2022

I've replaced 0↑/0↓ with only ↑/↓. Thought it would be helpful to concentrate on non-zero value. I'll revert this if it's a bad idea.

I don't necessarily think it's a bad idea, but I guess the thing that currently confuses me is that I see the arrows for some branches, but not others. This may be related to my second problem (below) tho!

For the second problem, please run git branch -vv then check the line of branch hard-soft-lint. It looks like the remote name recognized by gitty is different from the one that hard-soft-lint is tracking.

  hard-soft-lint     5be27bf Add godoc comments and fix linter warnings

The last commit on GitHub is bd92dc1, which should result in an up-arrow being displayed. I'll try to debug this locally.

@RangerCD
Copy link
Contributor Author

RangerCD commented Jan 9, 2022

It seems like your local branch is not tracking any remote branch. It would be like

hard-soft-lint     5be27bf [origin/hard-soft-lint: ahead 0] Add godoc comments and fix linter warnings

in git branch -vv if it's tracking a remote branch.

@muesli
Copy link
Owner

muesli commented Jan 9, 2022

It seems like your local branch is not tracking any remote branch. It would be like

hard-soft-lint     5be27bf [origin/hard-soft-lint: ahead 0] Add godoc comments and fix linter warnings

in git branch -vv if it's tracking a remote branch.

That's indeed correct and now it's fairly obvious what's happening. With that being said, I often use git like that and push/pull from specific branches as I see fit. I wonder if we should try to match the branch by name in such scenarios.

@muesli
Copy link
Owner

muesli commented Jan 9, 2022

@RangerCD I've now set up my branch to track the remote branch and it's mostly working fine. Nice work! I do however see a down arrow for that branch, even tho git status reports that I'm one commit ahead of origin/hard-soft-lint:
image

@RangerCD
Copy link
Contributor Author

RangerCD commented Jan 9, 2022

I'm not sure which direction should be used. Active branch is basically a remote branch, commit message is also from the remote branch. In this opinion, "ahead" in track stat means remote is ahead of local, which is opposite to regular "ahead".

@muesli
Copy link
Owner

muesli commented Jan 9, 2022

I'm not sure which direction should be used. Active branch is basically a remote branch, commit message is also from the remote branch. In this opinion, "ahead" in track stat means remote is ahead of local, which is opposite to regular "ahead".

Yeah, I can see that reasoning, and I guess it depends a bit on the perspective. I guess I fear the inverted lingo may be confusing a lot of people who use git status and gitty at the same time. Maybe we should just stick talking about the same kind of "ahead" and "behind" as git status here?

@RangerCD
Copy link
Contributor Author

Consistency is important indeed, I've reverse the direction in latest commit.
Matching branches without upstream should be also working now!

@RangerCD RangerCD marked this pull request as ready for review January 11, 2022 02:17
@RangerCD
Copy link
Contributor Author

The latest commit added a cloud indicator ☁ to represent remote-only branch. This cloud renders in a wierd way on my PC(Linux). I believe it's related to my terminal or font setting, but I can't figure this out.

When I disable emoji font, it renders as expected: most emojis are shown as unknown rectangles, others are shown as normal characters, arrangement is fine.
2022-01-11_16-41
But when I enable emoji font, most emojis seem to be rendered properly, except this cloud:
2022-01-11_15-02
The cloud indicator after test branch only shows in half, and the commit message for feat/branch-stats loses half of the letter h.

@muesli muesli merged commit a1b2433 into muesli:main Feb 1, 2022
@muesli
Copy link
Owner

muesli commented Feb 1, 2022

Great work! Once again, thank you so much, @RangerCD!

@RangerCD RangerCD deleted the feat/branch-stats branch February 1, 2022 11:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants