aboutsummaryrefslogtreecommitdiff
path: root/rcommit
blob: 50ec8be57b2c9082085cf7133180748ddbbc03de (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
84
85
86
87
88
89
#!/bin/bash
#
# Recursively commit submodule changes
#
# Usage:
#
# From a submodule folder:
#
#   rcommit <message>
#
# This go upwards and commit, until there's no parent repository.

# Parameters
DIRNAME="`dirname $0`"
BASENAME="`basename $0`"
MESSAGE="$*"
GIT="hit"

# Commit upwards
function upward_commit {
  local level="../"
  local up="../"
  local found="0"
  local base
  local log
  local message

  # Check upwards if there's a .git folder
  while true; do
    # Stop on the root folder
    if [ "`cd $level &> /dev/null && pwd`" == "/" ]; then
      break
    fi

    if [ -d "$level/.git" ]; then
      found="1"
      break
    fi

    level="${level}${up}"
  done

  # Commit in the parent repository
  if [ "$found" == "1" ]; then
    base="$(basename `pwd`)"
    log="`git log -1 --oneline`"
    message="Updates $base: $log"

    ( cd .. &> /dev/null && $GIT add -f $base )

    cd $level && $DIRNAME/commit "$message"

    return 0
  fi

  return 1
}

# Check if it is a git repository
# Thanks https://stackoverflow.com/questions/4917871/does-git-return-specific-return-error-codes#comment124785102_19441790
#git status &> /dev/null
#if [ ! -d ".git" ]; then
#if [ "$?" == "128" ]; then
if [ "`git rev-parse --is-inside-work-tree &> /dev/null`" == "true" ]; then
  echo "$BASENAME: not a git repository"
  exit 1
fi

# Default message
if [ -z "$MESSAGE" ]; then
  BASE="$(basename `pwd`)"
  MESSAGE="Updates $BASE"
fi

# Commit
$DIRNAME/commit $MESSAGE

# Commit upwards until there are repositories
while true; do
  # Stop on the root folder
  if [ "`pwd`" == "/" ]; then
    break
  fi

  # Go up
  if ! upward_commit; then
    break
  fi
done