2

Given 2 git repositories (Product A and Product B), with submodules (CommonSubmodule, SomeOtherSubmodule)

D:\Repositories\ProductA\
D:\Repositories\ProductA\CommonSubmodule
D:\Repositories\ProductA\SomeOtherSubmodule
D:\Repositories\ProductA\SomeOtherSubmodule\
D:\Repositories\ProductB\SomeOtherSubmodule\
D:\Repositories\ProductB\CommonSubmodule\

I've found a script online that allows for branches to be logged via a post_checkout hook

#!/bin/sh

previous_head_ref=$1
new_head_ref=$2
is_branch_checkout=$3

if [[ "$previous_head_ref" != "$new_head_ref" ]] && [[ "$is_branch_checkout" == 1 ]]; then
    branch=$(git rev-parse --abbrev-ref HEAD)
    #if [[ "develop" != "$branch" ]]; then
        path="$(dirname "$0")/.."
        logfile="$path/x_branch_log"
        ts=$(date +%s)
        echo "$branch|1|$ts" >> $logfile
        echo "Logging $branch|1|$ts to $logfile"
        echo PWD is $PWD
    #fi
fi

In a post_checkout context, how can I get the root directory (D:\Repositories) no matter how deep in the submodule the hook is installed, without encoding absolute paths?

D:\Repositories\

Additionally, how can I get the root product directory, e.g.

D:\Repositories\ProductA\
D:\Repositories\ProductB\
Ryan Leach
  • 4,262
  • 5
  • 34
  • 71

1 Answers1

0

From the comments I got on my answer on Git repo root folder, check the return value of a

git rev-parse --git-dir

You might have to iterate (if you are in a submodule of a submodule of a parent repo), but it should return the root folder of the parent repo.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Doing this on my command line, in the working directory of CommonSubmodule results in D:/Repositories/ProductA/.git/modules/CommonSubmodule – Ryan Leach May 03 '18 at 05:39
  • Yes, treeline the module part, and you get your root folder – VonC May 03 '18 at 05:45
  • @RyanTheLeach Don't mind the downvote, I still consider this a valid answer to your problem. – VonC May 03 '18 at 12:17
  • @RyanTheLeach I know, I meant, "please, (you) do not mind the downvote" ;) Or "don't attach the downvote too much importance" – VonC May 03 '18 at 12:20
  • I'm tempted to trust you based on other answers I've seen across SO relating to git hooks, But I'm an utter bash noob, and I've been trying to find out how to reliably manipulate file paths, but as far as I can tell, people just tend to hack it together using string manipulation? What is 'treeline' that you mentioned? – Ryan Leach May 04 '18 at 01:13
  • @RyanTheLeach DO I understand correctly that you want the root folder of the top parent repo? That is: if a repo A has a submodule B which has a submodule C, you would want the root folder of A when queried from within C? – VonC May 04 '18 at 06:52
  • I'm actually after 1 level higher then that again, but that should be trivial once I have the root folder of A. Just append a ../ my biggest problem by far is getting it to be reliable, and get a sane error when it's not easily possible, as I'm trying to convince others to also use my git hook eventually. – Ryan Leach May 04 '18 at 15:09
  • @RyanTheLeach The parent folder of A? – VonC May 04 '18 at 15:10
  • @RyanTheLeach I'll have a script for you before that. I wanted to be sure of what you are after. – VonC May 04 '18 at 15:11
  • My end goal is the parent folder of A. However the question is asking about the root folder of A if available. I was just going to append a \..\ afterwards to go up a directory. – Ryan Leach May 11 '18 at 06:15
  • @RyanTheLeach Try https://stackoverflow.com/a/38843585/6309 as an example of script. – VonC May 11 '18 at 07:27