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

Use filepath functions for path manipulation in getGitRepositorySubdir #177

Merged
merged 3 commits into from
Sep 22, 2023

Conversation

JeffreyVdb
Copy link
Contributor

Before this commit, the root directory would be removed which leaves a trailing file separator character in the beginning. This would then be trimmed by using strings.TrimPrefix. The problem here is that it uses a hardcoded file separator character which won't work on Windows because Windows uses a backslash instead of a forward slash.

Using filepath.Rel to get the directory relative to the root directory will not leave a separator character in the beginning.

It's also important to use filepath.ToSlash which will translate every file separator character to the forward slash.

This commit makes discovery work on Windows which it didn't before.

Before this commit, the root directory would be removed which leaves a
trailing file separator character in the beginning. This would then be trimmed
by using strings.TrimPrefix. The problem here is that it uses a hardcoded
file separator character which won't work on Windows because
Windows uses a backslash instead of a forward slash.

Using filepath.Rel to get the directory relative to the root directory
will not leave a separator character in the beginning.

It's also important to use filepath.ToSlash which will translate every
file separator character to the forward slash.

This commit makes discovery work on Windows which it didn't before.
@JeffreyVdb
Copy link
Contributor Author

Just for clearness. On windows, the return value of this function for a subdirectory called "foobar\something' would be:

\foobar\something

instead of

foobar/something

Rel returns . when the directory is the same as the base directory.
We can easily translate that to the empty string which is what we're
looking for.
filepath.Dir will return the same result when you're calling it on the
root directory of your filesystem. Checking when this happens is
required so that you break out of your loop.

This code used to loop endlessly when called outside of a git repo.
This commit fixes that.
@tomasmik tomasmik self-assigned this Sep 22, 2023
@tomasmik
Copy link
Contributor

Hey, thanks for your contribution. Please allow for some time to test this as this is Windows specific.

@JeffreyVdb
Copy link
Contributor Author

The last commit should also be applicable to Linux. It should endlessly loop on all platforms (before this fix).

@tomasmik tomasmik self-requested a review September 22, 2023 08:18
@tomasmik tomasmik merged commit 87116b2 into spacelift-io:main Sep 22, 2023
8 checks passed
@tomasmik
Copy link
Contributor

Tested and merged, thanks @JeffreyVdb

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.

2 participants