aboutsummaryrefslogtreecommitdiff
path: root/sup
blob: b06f16f95ef274522bc894fb0a0a45d8b8fe2362 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/bin/bash
#
# Commit submodule changes
#
# Usage:
#
# 1. From a submodule folder:
#
#   sup # go the upward repo and commit
#
# 2. From the top-level git repo:
#
#   sup <submodule>
#
# Usage in an alternative design:
#
# 1. From a submodule folder:
#
#     sup # go the upward repo and commit
#
# 2. From the top-level git repo:
#
#     sup # detect changed submodules
#     sup <submodule1> [..<submoduleN>]
#

# Parameters
DIRNAME="`dirname $0`"
BASENAME="`basename $0`"
PROJECT="$1"
GIT="hit"

# Check each file at the the submodule registry
#function sup_registry {
#  $GIT status --short | grep -v "??" | awk '{ print $2 }' | while read module; do
#    if grep -q "\[submodule \"$module\"\]" .gitmodules; then
#      true
#    fi
#  done
#}

# Check if it is a git repository
if [ ! -d ".git" ]; then
  echo "$BASENAME: not a git repository"
  exit 1
fi

# Remove trailing slash from project name
PROJECT="`echo "$PROJECT" | sed -e 's|/$||'`"

# Check if param is a project
if [ ! -z "$PROJECT" ]; then
  # Check if project is a registered submodule
  if ! grep -q "\[submodule \"$PROJECT\"\]" .gitmodules; then
    echo "$BASENAME: not a submodule: $PROJECT"
    exit 1
  fi

  # Check if it has changes to be commited
  #if ! $GIT status --short $PROJECT | grep -q "^[AM]"; then
  #  echo "$BASENAME: not changes to be commited for $PROJECT"
  #  exit 1
  #fi

  # Work with cached version only
  #COMMIT="`$GIT diff --cached $PROJECT | grep '^\+Subproject commit ' | cut -d ' ' -f 3`"
  #LOG="`cd $PROJECT &> /dev/null && git log -1 --oneline $COMMIT`"

  # Always work with the latest commit
  $GIT add $PROJECT
  LOG="`cd $PROJECT &> /dev/null && git log -1 --oneline`"

  MESSAGE="Updates $PROJECT: $LOG"
  $DIRNAME/commit $MESSAGE
else
  # Get log
  LOG="`git log -1 --oneline`"
  BASE="$(basename `pwd`)"
  MESSAGE="Updates $BASE: $LOG"

  # Got upward and commit
  ( cd .. &> /dev/null && $GIT add $BASE && $DIRNAME/commit "$MESSAGE" )
fi