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

Normalize path used for git add in respect to OS/environment #1134

Merged
merged 4 commits into from
Nov 28, 2017
Merged

Normalize path used for git add in respect to OS/environment #1134

merged 4 commits into from
Nov 28, 2017

Conversation

zenflow
Copy link
Contributor

@zenflow zenflow commented Nov 21, 2017

Description

Added code to transform file paths used in git add to OS- (POSIX vs Windows) neutral form, for improved portability.

Motivation and Context

See issue #1133

How Has This Been Tested?

Added two tests to the GitUtilities test suite.

Also npm linked the package to my monorepo project to verify that my changes resolve the problem in #1133

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

All new and existing tests passed

Actually some tests were failing on my Windows 7 Pro system after a fresh clone and install. Ignoring those, yes, all new and existing tests passed. I expect tests to pass on CI.

});
it("works with absolute paths for file and cwd", () => {
const cwd = path.resolve("test");
const file = path.resolve("test", "foo");
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this be const file = path.resolve(cwd, "foo");? Otherwise I don't see how this test is any different than the previous one.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@evocateur

In the previous test only cwd is absolute; the file path is still relative.

In this one both are absolute.

Your line of code is equivalent to mine, i.e. path.resolve(path.resolve("test"), "foo") (your code but with cwd inlined) returns the same value as path.resolve("test", "foo").

Should I change it?

@@ -38,7 +38,8 @@ export default class GitUtilities {

static addFile(file, opts) {
log.silly("addFile", file);
ChildProcessUtilities.execSync("git", ["add", file], opts);
const portablePath = path.relative(opts.cwd, path.resolve(opts.cwd, file)).replace(/\\/g, '/');
Copy link
Member

Choose a reason for hiding this comment

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

It's a pity there's no built-in way to coerce paths to posix... (path.posix.* methods don't work with Windows paths, near as I can tell)

Copy link
Contributor Author

@zenflow zenflow Nov 21, 2017

Choose a reason for hiding this comment

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

@evocateur

Would it be worthwhile to pull in one of these dependencies?

https://www.npmjs.com/package/path-posix (edit: not what we're looking for)
https://www.npmjs.com/package/ensure-posix-path (no README but tests look good)

@zenflow
Copy link
Contributor Author

zenflow commented Nov 22, 2017

@evocateur Added some commits.

I think my changes to the two new tests helps to clarify the difference (there's only 1 line different between them now, const file = "foo"; vs const file = path.resolve(cwd, "foo");) though the 1st test might be a bit gratuitous. We could probably do without it, as long as we still have the 2nd test. Should I just remove the 1st test or is it ok to remain?

I pulled in 'slash' for coercing the paths to POSIX format. If you look at the source, it's a very light weight package, made just for what we need it for, and no more. And it helps to more explicitly declare the intent of what we're doing. Think this is acceptable?

Also added one more test to make sure Windows paths are always transformed to POSIX paths for git add.

Copy link
Member

@evocateur evocateur left a comment

Choose a reason for hiding this comment

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

Awesome, thanks!

@lock
Copy link

lock bot commented Dec 27, 2018

This thread has been automatically locked because there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Dec 27, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants