aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/%HOME%/.bash_utils/git.sh
blob: c99f2e2016df6c107c231abb7faaaf92384a27e1 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env bash

# Copyright (c) 2016 Egor Tensin <Egor.Tensin@gmail.com>
# This file is part of the "Cygwin configuration files" project.
# For details, see https://github.com/egor-tensin/cygwin-home.
# Distributed under the MIT License.

source "$HOME/.bash_utils/text.sh"

alias repo_files='git ls-tree -r --name-only HEAD'
alias repo_dirs='git ls-tree -r --name-only HEAD -d'

repo_branches() {
    git for-each-ref --format='%(refname:short)' refs/heads/
}

repo_is_clean() (
    set -o errexit -o nounset -o pipefail
    test -z "$( git status --porcelain )"
)

alias repo_clean_all='git clean -fdx'
alias repo_clean_ignored='git clean -fdX'

branch_eol_normalized() (
    set -o errexit -o nounset -o pipefail

    if repo_is_clean; then
        repo_clean_ignored
    else
        echo "${FUNCNAME[0]}: repository isn't clean" >&2
        return 1
    fi

    local line
    while IFS= read -d '' -r line; do
        local eolinfo
        if ! eolinfo="$( expr "$line" : 'i/\([^ ]*\)' )"; then
            echo "${FUNCNAME[0]}: couldn't extract eolinfo from: $line" >&2
            return 1
        fi

        local path
        if ! path="$( expr "$line" : $'[^\t]*\t\\(.*\\)' )"; then
            echo "${FUNCNAME[0]}: couldn't extract file path from: $line" >&2
            return 1
        fi

        if [ "$eolinfo" == crlf ] || [ "$eolinfo" == mixed ]; then
            echo "${FUNCNAME[0]}: detected inconsistent line endings in file: $path" >&2
        fi
    done < <( git ls-files -z --eol )
)

repo_eol_normalized() (
    set -o errexit -o nounset -o pipefail

    if repo_is_clean; then
        repo_clean_ignored
    else
        echo "${FUNCNAME[0]}: repository isn't clean" >&2
        return 1
    fi

    local branch
    while IFS= read -r branch; do
        git checkout --quiet "$branch"
        branch_eol_normalized "$branch"
    done < <( repo_branches )
)

repo_tighten_permissions() (
    set -o errexit -o nounset -o pipefail

    repo_files -z | xargs -0 -- chmod 0600 --
    repo_dirs  -z | xargs -0 -- chmod 0700 --
    chmod 0700 .git
)

repo_doslint() (
    set -o errexit -o nounset -o pipefail

    local -a paths
    local path

    while IFS= read -d '' -r path; do
        paths+=("$path")
    done < <( repo_files -z )

    doslint ${paths[@]+"${paths[@]}"}
)

repo_lint() (
    set -o errexit -o nounset -o pipefail

    local -a paths
    local path

    while IFS= read -d '' -r path; do
        paths+=("$path")
    done < <( repo_files -z )

    lint ${paths[@]+"${paths[@]}"}
)

repo_backup() (
    set -o errexit -o nounset -o pipefail

    local repo_dir
    repo_dir="$( pwd )"
    local repo_name
    repo_name="$( basename -- "$repo_dir" )"
    local backup_dir="$repo_dir"

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

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

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

repo_backup_dropbox() (
    set -o errexit -o nounset -o pipefail

    repo_backup "$USERPROFILE/Dropbox/backups"
)