filepath icon indicating copy to clipboard operation
filepath copied to clipboard

Add function to get root directory

Open adius opened this issue 7 years ago • 8 comments

Is there really no function to get the root directory? 😳

rootDirectory :: FilePath -> FilePat

rootDirectory "/directory/other.ext" == "/"
rootDirectory "directory/other.ext" == "directory"
rootDirectory "/foo/bar/baz" == "/"
rootDirectory "foo/bar/baz" == "foo"

adius avatar Feb 12 '18 15:02 adius

Found it (kind of): https://hackage.haskell.org/package/shake-0.16.1/docs/Development-Shake-FilePath.html#v:takeDirectory1 Wow, that's weird function name. Definitely not what I was expecting 🙈

adius avatar Feb 12 '18 15:02 adius

What's the use case? Generally there's not much use in the "root" directory - Shake has takeDirectory1 because it assumes relative file paths and build-system style operations, which are less common in other contexts. That said, given Shake has it, if it's generally useful it could certainly be considered.

ndmitchell avatar Feb 12 '18 17:02 ndmitchell

Well, thinking about it I actually just need takeDirectory1. Not sure either if there is a use case for the function I described ... So feel free to close this issue from my side.

adius avatar Feb 12 '18 17:02 adius

So the one that works on relative paths only? It's arguable that if takeDirectory1 is useful outside build systems it should be renamed to something sensible and moved over. What was your use case? Do you think it's common enough to go in filepath itself?

ndmitchell avatar Feb 12 '18 17:02 ndmitchell

Ah, I understand. Yeah I'd definitely say that takeDirectory1 could be useful in other contexts. So I guess it should be moved over (with a better name 😛)

What might be a common use case (which I at least often do) is, to build a tool which recursively analyzes a set of directories (e.g. all modules in a mono repo) and then only executes commands in the takeDirectory1 directories. (e.g. find all changed files and git add them, or analyze some files and write the result in the directory).

adius avatar Feb 12 '18 17:02 adius

@adius re that mono-repo, does this assume that all git submodules are located at the same level? If not, wouldn't you rather need a more generic way to locate the olders each submodule was "mounted" in (by analysing either .gitmodules or git submodule's output), and then operate with a kind of isPrefixOf operator to check which submodule an argument belongs to?

hvr avatar Feb 12 '18 18:02 hvr

Sorry, I mixed things up a little. I was actually talking about 2 different things: Monorepo (of packages) and a directory of projects (git repos). And they were just 2 examples of the same pattern:

~/modules $ tr -L 1
.
├── moduleA
├── moduleB
└── moduleC
~/projects $ tr -L 1
.
├── repoA
├── repoB
└── repoC
~/photos $ tr -L 1
.
├── 2016
├── 2017
└── 2018

To handle operations in subtrees with this pattern takeDirectory1 is really helpful & important.

adius avatar Feb 12 '18 19:02 adius

OK, so I'm open to adding these functions. You assumedly want:

takeFirstDirectory :: FilePath -> FilePath
dropFirstDirectory :: FilePath -> FilePath

What name should they have? What should their semantics be? What should they do on Windows? Should they subsume splitDrive?

ndmitchell avatar Jul 02 '18 14:07 ndmitchell