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

Support working paths with spaces #9

Merged
merged 2 commits into from
Mar 9, 2022
Merged

Conversation

Koolstr
Copy link
Contributor

@Koolstr Koolstr commented Mar 1, 2022

Resolves this issue.

Changes:

  • Added a new function for parsing out the working path from the tree strings that are output by 'git worktree list'.
  • Used the new function to set the working directory instead of relying on positional parameters set by 'git worktree list'.
  • Configured 'awk' calls to use tabs instead of spaces as field separators for a line.
  • Distinguished the console message that is printed when switching to the main worktree.

This can possibly be programmed more elegantly, so feel free to make improvements or suggestions if you have any.

Added a new function for parsing out the working path from the tree strings that are output by 'git worktree list'.
Used the new function to set the working directory instead of relying on positional parameters set by 'git worktree list'.
Configured 'awk' calls to use tabs instead of spaces as field separators for a line.
Distinguished the console message that is printed when switching to the main worktree.
@yankeexe
Copy link
Owner

yankeexe commented Mar 3, 2022

@Koolstr thank you for the PR.
About the spaces in the path to worktrees, the solution should be really simple.
Instead of parsing through the list, we can isolate the path to worktree and wrap it with double-quotes.
This is what I purpose.
image

We can modify the last command to use regex with awk to grab everything after worktree<space>

git worktree list --porcelain  | awk '$1=="worktree" {print}' 

Or with similar strategy, we can use git worktree list and grab all columns except the last two to get the full path.

Reworked and simplified the approach used for parsing out the working directory from 'git worktree list, by taking advantage of the '--porcelain' flag and using awk to directly identify the first line with the worktree & parse out the path from it.
Removed now-unused function and its calls for parsing out the path via the previous array-based method.
@Koolstr
Copy link
Contributor Author

Koolstr commented Mar 3, 2022

I like that idea, it's certainly more clean and efficient. However, what you've suggested doesn't work when you have more than one worktree. In such a case, it will print/return multiple retrieved 'worktree' lines. Having awk stop after finding the first match prevents that, so I've added the exit keyword to accomplish that.

For parsing the path without worktree at the start of the string, we could use something like | cut -d ' ' -f 2-, but I chose to settle with using substr() directly within awk. Let me know if you disagree with that choice.

I've adapted the implementation according to your suggestion, removing the now unnecessary function related to my old approach. It seems to work as expected. Please review the updated approach.

@yankeexe yankeexe merged commit f07dd40 into yankeexe:master Mar 9, 2022
@yankeexe
Copy link
Owner

yankeexe commented Mar 9, 2022

@Koolstr Thanks looks neat! 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Breaks under working paths with spaces
2 participants