Re: Git-repo-root relative path

2020-11-22 Thread Per Nordlöw via Digitalmars-d-learn
On Wednesday, 18 November 2020 at 23:43:15 UTC, Petar Kirov 
[ZombineDev] wrote:

https://gist.github.com/PetarKirov/b4c8b64e7fc9bb7391901bcb541ddf3a


Thanks a lot!


Re: Git-repo-root relative path

2020-11-18 Thread Petar via Digitalmars-d-learn

On Monday, 16 November 2020 at 10:21:27 UTC, Per Nordlöw wrote:
I need a function that gets the relative path of a file in a 
Git-repo and preferrably also its status.


I'm not sure I understand the question. I have written two 
programs, hopefully one of them does what you want :D


Either via an external call to `git` or optionally via `libgit` 
(if available).


Which DUB packages do you prefer?


For such small tasks, the easiest is to just use the shell.

1st answer:

Initially I thought that you want to convert the current working 
directory (I don't know why - I didn't read well the question 
apparently :D) to a path relative to the root git repo path. 
Here's my solution to that problem:


```d
import std.exception : enforce;
import std.format : format;
import std.file : getcwd;
import std.path : asRelativePath;
import std.process : executeShell;
import std.stdio : writeln;
import std.string : stripRight;

void main()
{
auto cwd = getcwd();
const gitRootPathResult = executeShell("git rev-parse 
--show-toplevel");

enforce(
gitRootPathResult.status == 0,
"`git` is not installed, or '%s' is not a git 
repo".format(cwd)

);
// Trim trailing whitespace from the shell invocation
const gitRoot = gitRootPathResult.output.stripRight;
debug writeln("Git root path: ", gitRoot);
gitRoot
.asRelativePath(getcwd())
.writeln;
}
```

Example usage:

```
$ cd ~/code/repos/dlang/dlang/dmd/src/dmd/backend/

$ dmd -run ~/code/cwd_to_git_relative_path.d
../../..

# Sanity check:

$ dmd -debug -run ~/code/cwd_to_git_relative_path.d
Git root path: /home/zlx/code/repos/dlang/dlang/dmd
../../..

$ cd '../../..' && pwd
/home/zlx/code/repos/dlang/dlang/dmd
```

2nd answer:

Reading a second time, I don't understand what you meant by "gets 
the relative path of a file in a Git-repo". Did you mean that it 
receives an absolute path (or relative to current working 
directory) to a file and converts it to a path relative to a git 
repo? If so, here's my solution and for determining the status of 
a file:


https://gist.github.com/PetarKirov/b4c8b64e7fc9bb7391901bcb541ddf3a


Re: Git-repo-root relative path

2020-11-16 Thread Per Nordlöw via Digitalmars-d-learn

On Monday, 16 November 2020 at 10:21:27 UTC, Per Nordlöw wrote:

Which DUB packages do you prefer?


https://code.dlang.org/packages/dlibgit seems good. But that 
requires `libgit` to be installed. It would be nice to have a 
fallback to calling `git`.


Git-repo-root relative path

2020-11-16 Thread Per Nordlöw via Digitalmars-d-learn
I need a function that gets the relative path of a file in a 
Git-repo and preferrably also its status.


Either via an external call to `git` or optionally via `libgit` 
(if available).


Which DUB packages do you prefer?