diff options
author | Ismaël Bouya <ismael.bouya@normalesup.org> | 2016-07-16 14:01:46 +0200 |
---|---|---|
committer | Ismaël Bouya <ismael.bouya@normalesup.org> | 2016-07-16 14:01:46 +0200 |
commit | d5a5b224b549dc17f472fc8a13270d6041f62546 (patch) | |
tree | d39832504f643a45fcaab3880fc3e76f4f4aa6ba | |
parent | c2590696efd0b91972c238ab892555f739751bc9 (diff) | |
download | Public-d5a5b224b549dc17f472fc8a13270d6041f62546.tar.gz Public-d5a5b224b549dc17f472fc8a13270d6041f62546.tar.zst Public-d5a5b224b549dc17f472fc8a13270d6041f62546.zip |
Backup git dirs and workdir to remote
-rwxr-xr-x | backup_git_dirs | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/backup_git_dirs b/backup_git_dirs new file mode 100755 index 0000000..9822bab --- /dev/null +++ b/backup_git_dirs | |||
@@ -0,0 +1,70 @@ | |||
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 | |||
24 | REMOTE=origin | ||
25 | REF_NS=auto_backup | ||
26 | |||
27 | function backup_refs() { | ||
28 | pushd "$1" > /dev/null | ||
29 | |||
30 | if git rev-parse --git-dir > /dev/null 2>&1; then | ||
31 | git for-each-ref --format='%(refname)' refs/$REF_NS | while read ref; do | ||
32 | git update-ref -d "$ref" | ||
33 | done | ||
34 | |||
35 | git for-each-ref --format='%(refname)' refs/heads refs/tags | while read ref; do | ||
36 | new_ref=$(echo "$ref" | sed -e "s@^refs/@refs/$REF_NS/@") | ||
37 | git update-ref $new_ref $ref | ||
38 | done | ||
39 | |||
40 | stash=0 | ||
41 | git reflog --pretty=tformat:'%H' refs/stash 2>/dev/null | while read ref; do | ||
42 | git update-ref refs/$REF_NS/stash/stash_$stash $ref | ||
43 | stash=$(($stash + 1)) | ||
44 | done | ||
45 | |||
46 | export GIT_INDEX_FILE=`mktemp /tmp/git-backup-index.XXXXXX` | ||
47 | git read-tree HEAD | ||
48 | head_commit=$(git show-ref --hash --head ^HEAD) | ||
49 | git add . | ||
50 | COMMIT=`git commit-tree $(git write-tree) -p $head_commit -m "Automatic backup"` | ||
51 | git update-ref refs/$REF_NS/workdir $COMMIT | ||
52 | rm -f "$GIT_INDEX_FILE" | ||
53 | unset GIT_INDEX_FILE | ||
54 | |||
55 | if git remote | grep -q "^$REMOTE$"; then | ||
56 | git push -q --force $REMOTE "refs/$REF_NS/*:refs/$REF_NS/*" | ||
57 | git for-each-ref --format='%(refname)' refs/$REF_NS | while read ref; do | ||
58 | git update-ref -d "$ref" | ||
59 | done | ||
60 | # Pour récupérer: | ||
61 | # git fetch $REMOTE --force "refs/$REF_NS/*:refs/$REF_NS/*" | ||
62 | fi | ||
63 | fi | ||
64 | |||
65 | popd > /dev/null | ||
66 | } | ||
67 | |||
68 | for directory in "$@"; do | ||
69 | backup_refs $directory | ||
70 | done | ||