aboutsummaryrefslogtreecommitdiff
path: root/git-submodule-move-git-dir
blob: b30f247c1257ecc47fe61ff84ef49283119b8521 (plain)
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/bin/bash
#
# Put the .git submodule folder back to the right place.
#
# When initializing a git submodule, git usually puts the
# submodules' .git folder inside the .git/modules folder
# in the parent repository.
#
# While this keeps all submodule metadata in a single place,
# it may not be confortable when you also want to develop
# the submodule.
#
# This plugin simply moves the submodules' git folder back
# to the standard place.

# Parameters
BASENAME="`basename $0`"
MODULE="$1"

# Checks
if [ -z "$MODULE" ]; then
  echo "usage: $BASENAME <submodule-path>"
  exit 1
elif [ ! -d "$MODULE" ]; then
  echo "module not found: $MODULE"
  exit 1
elif [ -d "$MODULE/.git" ]; then
  echo "module $MODULE already converted"
  exit 1
elif [ ! -d ".git" ]; then
  echo "please run this command in the top-level repository folder"
  exit 1
fi

if [ ! -f "$MODULE/.git" ]; then
  echo "$BASENAME: not a file, or file not found: $MODULE/.git"
  exit 1
fi

# Additional parameters
RELATIVE_REPO_DIR="$(cd $MODULE &> /dev/null && cd `cut -d : -f 2 .git` &> /dev/null && pwd)"
REPO_DIR="`pwd`"
MODULE_DIR="`cd $MODULE &> /dev/null && pwd`"

if [ -z "$RELATIVE_REPO_DIR" ] || [ ! -d "$RELATIVE_REPO_DIR" ]; then
  RELATIVE_REPO_DIR=".git/modules/$RELATIVE_REPO_DIR/$(echo $MODULE_DIR | sed -e "s|^$REPO_DIR/||")"
fi

if [ -z "$RELATIVE_REPO_DIR" ] || [ ! -d "$RELATIVE_REPO_DIR" ]; then
  echo "$BASENAME: no git folder found for submodule $MODULE"
  exit 1
fi

if [ ! -e "$RELATIVE_REPO_DIR/config" ]; then
  echo "$BASENAME: config not found at $RELATIVE_REPO_DIR/config"
  exit 1
fi

# Convert
rm $MODULE/.git && \
mv "$RELATIVE_REPO_DIR" "$MODULE/.git" && \
sed -i -e '/worktree =/d' "$MODULE/.git/config"