aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/.bashrc_git
blob: 57766fc41c73887ce6bdae7e3cbcca5c150b5892 (plain) (blame)
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
90
91
92
[ ! -z "${BASHRC_GIT+x}" ] && return || readonly BASHRC_GIT=1

list_repo_files() (
  local -a cmd=(git ls-files)

  while [ "$#" -gt 0 ]; do
    case "$1" in
      -z|-0):
        cmd+=(-z)
        shift
        ;;

      -h):
        echo "usage: $FUNCNAME [-h] [-z|-0]"
        return 0
        ;;

      *)
        echo "$FUNCNAME: unrecognized parameter: $1" >&2
        return 1
        ;;
    esac
  done

  eval "${cmd[@]}"
)

list_repo_dirs() (
  set -o errexit

  local terminator='\n'

  while [ "$#" -gt 0 ]; do
    case "$1" in
      -z|-0):
        terminator='\000'
        shift
        ;;

      -h):
        echo "usage: $FUNCNAME [-h] [-z|-0]"
        return 0
        ;;

      *)
        echo "$FUNCNAME: unrecognized parameter: $1" >&2
        return 1
        ;;
    esac
  done

  { printf '.\0' ; list_repo_files -z ; } \
    | xargs -0 dirname -z \
    | sort -uz \
    | tail -z -n +2 \
    | tr '\000' "$terminator"
)

tighten_repo_security() (
  set -o errexit

  list_repo_files -z | xargs -0 chmod 0600
  list_repo_dirs  -z | xargs -0 chmod 0700
  chmod 0700 .git
)

backup_repo() (
  set -o errexit

  local repo_dir="$( realpath . )"
  local repo_name="$( basename "$repo_dir" )"
  local backup_dir="$repo_dir"

  if [ $# -eq 1 ]; then
    backup_dir="$1"
  elif [ $# -gt 1 ]; then
    echo "usage: $FUNCNAME [BACKUP_DIR]" >&2
    exit 1
  fi

  local zip_name="${repo_name}_$( date -u +'%Y%m%dT%H%M%S' ).zip"

  git archive \
    --format=zip -9 \
    --output="$backup_dir/$zip_name" \
    --remote="$repo_dir" \
    HEAD
)

backup_repo_dropbox() {
  backup_repo "$USERPROFILE/Dropbox/backups"
}