-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgit-top
executable file
·38 lines (32 loc) · 1.05 KB
/
git-top
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env bash
#
# Custom git command that returns the top of the project directory.
# This handles submodules stored at any depth.
#
# Author: Lucas Magasweran <[email protected]>
#
# Start from the current directory
current_dir=$(pwd)
# Initialize a variable to store the last directory with .git
last_git_dir=""
# Loop until we reach the root directory "/"
while [[ "$current_dir" != "/" ]]; do
# Check if the current directory contains a .git directory or file
if [[ -d "$current_dir/.git" || -f "$current_dir/.git" ]]; then
last_git_dir="$current_dir"
fi
# Use 'git rev-parse' to jump from submodule to parent Git repo
parent_dir=$(git -C "$current_dir" rev-parse --show-superproject-working-tree)
# If 'git rev-parse' fails to return a parent directory, break the loop
if [[ -z "$parent_dir" || "$parent_dir" == "$current_dir" ]]; then
break
fi
current_dir="$parent_dir"
done
# Check if a .git directory or file was found
if [[ -n "$last_git_dir" ]]; then
echo "$last_git_dir"
exit 0
else
exit 1
fi