summaryrefslogtreecommitdiffstats
path: root/rsync.sh
blob: d94677bcb11787673734afd16a6dbda0541bc899 (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
##
# systr_rsync_normal <src> <dst> [<link>]
#
# Perform normal rsync operations to transfer files to or from the
# repository.  If <link> is set, it is used as the --link-dest value.
##
function systr_rsync_normal
{
    if [ $# -lt 2 ]; then
        echo "Fatal: too few args to rsync_normal" >&2
        exit 1
    fi

    src="$1"
    dst="$2"

    if [ $# -gt 2 ]; then
        link="--link-dest=$3"
    else
        link=""
    fi

    rsync -az --info=progress2 --info=stats2 --delete \
        --exclude='*.systr' $link "$src" "$dst"
}

##
# systr_rsync_diff <src> <compare> <dst>
#
# Copy files from <src> to <dst>, but only if they've changed
# since the state in <compare>.  Empty directories are
# purged from <dst> afterward.
##
function systr_rsync_diff
{
    if [ $# -lt 3 ]; then
        echo "Fatal: too few args to rsync_diff" >&2
        exit 1
    fi

    src="$1"
    compare="--compare-dest=$2"
    dst="$3"

    rsync -az --delete --exclude='*.systr' $compare \
        "$src" "$dst"

    find "$dst" -type d -empty -delete
}

##
# systr_rsync_del <src> <compare> <dst>
#
# Delete files in <dst>, only if they have been deleted from
# <compare> to <src>.  Deleted files in <dst> are 'backed up',
# in rsync lingo, meaning they are actually renamed with a
# suffix appended to the original filename.
#
# If <src> is the worktree ("."), our file suffix will be
# '&' to signify files deleted by us.  Otherwise, the suffix
# will be '~' to signify files deleted by 'them'.
##
function systr_rsync_del
{
    if [ $# -lt 3 ]; then
        echo "Fatal: too few args to rsync_del" >&2
        exit 1
    fi

    src="$1"
    compare="--compare-dest=$2"
    dst="$3"

    if [[ "$src" == "." ]]; then
        suffix="--suffix=&"
    else
        suffix="--suffix=~"
    fi

    rsync -azb $suffix --existing --ignore-existing \
        --delete --exclude='*.systr' $compare \
        "$src" "$dst"
}

##
# systr_rsync_merge <src> <dst>
#
# Finish setting up a merge, by copying files from the
# temporary directory to the merge staging directory.
#
# Files in <dst> overwritten by this operation will
# be suffixed with '*' to signify files modified by
# both of us.
##
function systr_rsync_merge
{
    if [ $# -lt 2 ]; then
        echo "Fatal: too few args to rsync_merge" >&2
        exit 1
    fi

    src="$1"
    dst="$2"

    rsync -azb --suffix='*' --exclude='*.systr' \
        "$src" "$dst"
}

##
# systrunk clone <source> <destination>
#
# Recursively create a complete copy of a directory tree
# which, presumabally, contains systrunk repositories.
# This copy preserves all underlying hard-links as to
# preserve the disk space saved by systrunk commit.  Note:
# --delete is passed to rsync!
##
function systr_clone
{
    if [ $# -lt 2 ]; then
        echo "Fatal: too few args to clone" >&2
        exit 1
    fi

    src="$1"
    dst="$2"

    rsync -azH --info=progress2 --info=stats2 --delete "$src" "$dst"
}