summaryrefslogtreecommitdiff
path: root/share/hydractl/sync-media-remotes
blob: 217b17fb19161e1f092fc04c557ce1840c9ff984 (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
#!/bin/bash
#
# Keep git remotes in sync among media archives.
#

# Parameters
BASENAME="$0"
DEST="$1"
MEDIA="/var/cache/media"

# Syntax
if [ -z "$DEST" ]; then
  echo "usage: $BASENAME <dest>"
  exit 1
elif [ ! -d "$DEST/$MEDIA" ]; then
  echo "folder $DEST/$MEDIA does not exist."
  exit 1
fi

# Run
for folder in `ls $MEDIA`; do
  if [ -d "$DEST/$MEDIA/$folder/.git" ]; then
    # Add new remotes
    git -C $MEDIA/$folder remote -v | while read remote; do
      cd $DEST/$MEDIA/$folder
      name="`echo $remote | cut -d ' ' -f 1`"
      addr="`echo $remote | cut -d ' ' -f 2`"
      type="`echo $remote | cut -d ' ' -f 3`"

      if [ "$type" == "(push)" ]; then
        command="set-url --add --push"
      else
        command="add"
      fi

      if ! git remote -v | sed -e 's/\t/ /g' | grep -q "^$name $addr $type$"; then
        git remote $command $name $addr
      fi
    done

    # Delete old remotes
    git -C $DEST/$MEDIA/$folder remote -v | while read remote; do
      cd $MEDIA/$folder
      name="`echo $remote | cut -d ' ' -f 1`"
      addr="`echo $remote | cut -d ' ' -f 2`"
      type="`echo $remote | cut -d ' ' -f 3`"

      if [ "$type" == "(push)" ]; then
        command="set-url --delete"
        arg="$addr"
      else
        command="remove"
        arg=""
      fi

      if ! git remote -v | sed -e 's/\t/ /g' | grep -q "^$name $addr $type$"; then
        # Check if was not already removed by a previous command
        if git -C $DEST/$MEDIA/$folder remote | grep -q "^$name$"; then
          git -C $DEST/$MEDIA/$folder remote $command $name $arg
        fi
      fi
    done
  fi
done