Skip to content

Add option to abbreviate based path based on the git root#720

Merged
rkeithhill merged 6 commits intodahlbyk:masterfrom
elsassph:feature/abbreviate-git
Dec 8, 2019
Merged

Add option to abbreviate based path based on the git root#720
rkeithhill merged 6 commits intodahlbyk:masterfrom
elsassph:feature/abbreviate-git

Conversation

@elsassph
Copy link
Copy Markdown
Contributor

@elsassph elsassph commented Nov 25, 2019

Implement #719. Added an option to abbreviate the prompt path based on the git root.

When enabled

$GitPromptSettings.DefaultPromptAbbreviateGitDirectory = $true

When the currentpath is under a git repository, e.g.

~/company/work/projects/client/project-name/src [master] >

Prompt shows a path shortened to:

# using an arbitrary token to indicate the shortening
-/project-name/src [master] >
# or maybe using git `:/` root convention
project-name:/src [master] >

@elsassph elsassph force-pushed the feature/abbreviate-git branch from 3fa372c to 828461b Compare December 2, 2019 17:56
@elsassph
Copy link
Copy Markdown
Contributor Author

elsassph commented Dec 3, 2019

@dahlbyk @rkeithhill now with tests :)

src/Utils.ps1 Outdated
$currentPath.StartsWith($gitPath, $stringComparison)) {
# Up another level to keep repo name in path
$removePath = $(Split-Path $gitPath -Parent)
$currentPath = "-" + $currentPath.SubString($removePath.Length)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Hmm, what character should be used to indicate the rooted in the Git repo? I could see some folks wanting to use $/ as opposed to -/. Or maybe ./ or even an emoji. I wind up replacing ~ with 🏠. I might be tempted to use 🎪 or 💩for the root of Git repos. :-)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fair enough, -/ is no convention.

OMG apparently :/ is the git convention for the repository root! https://stackoverflow.com/a/22049939/27219

Additionally would you suggest adding a new configuration option like $GitPromptSettings.DefaultPromptGitRootPrefix?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Cool. Let's go with that then. How about we call it DefaultPromptGitDirectoryRootPrefix to try to tie it a bit closer to the other setting?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@rkeithhill having tried this :/ I'm hesitating a bit.

  • :/ is supposed to be the root,
  • :/myproject/src is incorrect based on this convention
  • maybe myproject:/src or [myproject]:/src would be more correct?

Using a function (passing project name and sub-path) would allow to format that the way people want.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

image

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

That seems reasonable. Of course, whether it's reponame:\ or reponame:/ will depend on the platform.

Using a function (passing project name and sub-path) would allow to format that the way people want.

What do you mean? Keep in mind folks can already do this:

$GitPromptSettings.DefaultPromptPath.Text = '$(CallMyCustomPromptFunction)'

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good point about the directory separator direction!
And you're right - one may as well just replace the function.

All fixed, but I didn't add any option for the : as it sounds like we are using a correct convention

@rkeithhill rkeithhill merged commit 1b64a52 into dahlbyk:master Dec 8, 2019
@rkeithhill
Copy link
Copy Markdown
Collaborator

@elsassph Thanks!

@elsassph
Copy link
Copy Markdown
Contributor Author

@rkeithhill I stumbled onto something more about the colon:

# this is correct
git show :README.md
git show :src/GitUtils.ps1

# this is wrong
git show :/README.md 
git show :/src/GitUtils.ps1

So it sounds like it should be adjusted before being worth releasing and maybe this would be a better formatting:
image

@elsassph elsassph deleted the feature/abbreviate-git branch December 17, 2019 12:46
if ($abbrevGitDir) {
$gitPath = Get-GitDirectory
# Up one level from `.git`
if ($gitPath) { $gitPath = Split-Path $gitPath -Parent }
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Two edge cases to consider here:

  1. If the current repo is bare, this returns its parent directory.
  2. Get-GitDirectory inside a worktree currently returns the path to .git/worktrees/[name], which won't match the current path. I can think of two ways to resolve this:
    1. Revise Get-GitDirectory to return the contents of .git/worktrees/[name]/gitdir. No idea what the downstream effects of this would be.
    2. Check here if $gitPath matches .git/worktrees/; if it does, set $gitPath to the contents of $gitPath/gitdir

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Never used worktrees, but git abbreviation just won't happen in that case.
An issue should be created to track it.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Never used worktrees, but git abbreviation just won't happen in that case.

Not a problem. It doesn't break, it just won't work as expected in all cases.

An issue should be created to track it.

#746

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants