]> git.immae.eu Git - perso/Immae/Projets/Scripts/Public.git/blame - backup_git_dirs
Fix some arguments in git backup
[perso/Immae/Projets/Scripts/Public.git] / backup_git_dirs
CommitLineData
d5a5b224
IB
1#!/bin/bash
2# The MIT License (MIT)
3#
4# Copyright (c) 2011-2016 Ismaël Bouya http://www.normalesup.org/~bouya/
5#
6# Permission is hereby granted, free of charge, to any person obtaining a copy
7# of this software and associated documentation files (the "Software"), to deal
8# in the Software without restriction, including without limitation the rights
9# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10# copies of the Software, and to permit persons to whom the Software is
11# furnished to do so, subject to the following conditions:
12#
13# The above copyright notice and this permission notice shall be included in
14# all copies or substantial portions of the Software.
15#
16# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22# THE SOFTWARE.
23
2d8938f6
IB
24
25# Feel like tipping/donating? https://www.immae.eu/licenses_and_tipping
26
d5a5b224
IB
27REMOTE=origin
28REF_NS=auto_backup
29
30function backup_refs() {
31 pushd "$1" > /dev/null
32
33 if git rev-parse --git-dir > /dev/null 2>&1; then
34 git for-each-ref --format='%(refname)' refs/$REF_NS | while read ref; do
35 git update-ref -d "$ref"
36 done
37
38 git for-each-ref --format='%(refname)' refs/heads refs/tags | while read ref; do
39 new_ref=$(echo "$ref" | sed -e "s@^refs/@refs/$REF_NS/@")
40 git update-ref $new_ref $ref
41 done
42
43 stash=0
44 git reflog --pretty=tformat:'%H' refs/stash 2>/dev/null | while read ref; do
45 git update-ref refs/$REF_NS/stash/stash_$stash $ref
46 stash=$(($stash + 1))
47 done
48
49 export GIT_INDEX_FILE=`mktemp /tmp/git-backup-index.XXXXXX`
50 git read-tree HEAD
51 head_commit=$(git show-ref --hash --head ^HEAD)
52 git add .
d64fa161 53 COMMIT=`git commit-tree --no-gpg-sign $(git write-tree) -p $head_commit -m "Automatic backup"`
d5a5b224
IB
54 git update-ref refs/$REF_NS/workdir $COMMIT
55 rm -f "$GIT_INDEX_FILE"
56 unset GIT_INDEX_FILE
57
58 if git remote | grep -q "^$REMOTE$"; then
d64fa161 59 git push --no-verify --no-signed -q --force $REMOTE "refs/$REF_NS/*:refs/$REF_NS/*"
d5a5b224
IB
60 git for-each-ref --format='%(refname)' refs/$REF_NS | while read ref; do
61 git update-ref -d "$ref"
62 done
63 # Pour récupérer:
64 # git fetch $REMOTE --force "refs/$REF_NS/*:refs/$REF_NS/*"
65 fi
66 fi
67
68 popd > /dev/null
69}
70
71for directory in "$@"; do
72 backup_refs $directory
73done