-
Notifications
You must be signed in to change notification settings - Fork 0
/
import-external-repo.sh
executable file
·138 lines (123 loc) · 3.58 KB
/
import-external-repo.sh
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
137
138
#!/bin/bash
#
# Coller ce fichier à la racine du dépôt git dans lequel importer les sources d'un autre dépôt git.
#
# Commande pour utiliser ce script BASH :
#
# sh import-external-repo.sh [email protected]:O-clock-XXXXX/nom-de-depot.git nom-de-la-branche-a-creer [nom-de-la-branche-source]
#
# - l'argument "[email protected]:O-clock-XXXXX/nom-de-depot.git" correspond au lien permettant de cloner le dépôt
# - l'argument "nom-de-la-branche-a-creer" correspond au nom de la branche dans laquelle les sources seront récupérées (on n'écrase pas les sources actuelles)
# - l'argument OPTIONNEL "nom-de-la-branche-source" correspond au nom de la branche que l'on souhaite copier depuis le dépôt source (== "master" si non précisé)
if [ $# -eq 0 ] || [ $# -eq 1 ]
then
echo " "
echo "Usage: sh import-external-repo.sh [email protected]:O-clock-XXXXX/repository-name.git branch-name-to-create [branch-name-source]"
echo " "
exit 0
fi
# Check if gitignore exists
if [ -f .gitignore ]
then
if ! grep -Fxq import-external-repo.sh .gitignore
then
git add .
git stash
echo " "
echo " --- Updating .gitignore file --- "
echo "" >> .gitignore
echo "import-external-repo.sh" >> .gitignore
echo " --- OK ---"
echo " "
git add .gitignore
git commit -m "import-external-repo.sh added to .gitignore"
git stash apply
git stash drop
fi
else
git add .
git stash
echo " "
echo " --- Creating .gitignore file --- "
echo "import-external-repo.sh" > .gitignore
echo " --- OK ---"
echo " "
git add .gitignore
git commit -m "import-external-repo.sh added to .gitignore"
git stash apply
git stash drop
fi
stashCommand=""
if [[ `git status --porcelain` ]]
then
# Changes
echo " "
echo "Il y a des modifiations non commitées"
read -p "Voulez-vous les écraser ou les garder (E = écraser / G = garder / A = abandon) ? " choice
if [ "$choice" = "E" ] || [ "$choice" = "e" ]
then
git add .
git stash
stashCommand="drop"
elif [ "$choice" = "G" ] || [ "$choice" = "g" ]
then
git add .
git stash
stashCommand="apply"
else
echo "--- Abandon ---"
echo "Vous devriez exécuter la commande \"git status\" afin de vérifier les modifications non commitées"
echo "et ensuite annuler les modifications ou bien les commiter"
exit 0
fi
fi
# Get argument
cloneURL=$1
branchName=$2
currentDate=`date +%d/%m/%Y`
# Create branch
git checkout -b $branchName
# Clone in subdirectory
echo " "
if [ $# -eq 3 ]
then
# Get argument
sourceBranchName=$3
echo " --- Cloning from $sourceBranchName --- "
git clone -b $sourceBranchName $cloneURL $branchName
commitName="import $currentDate from $sourceBranchName"
else
echo " --- Cloning from master --- "
git clone $cloneURL $branchName
commitName="import $currentDate from master"
fi
# Copy all files
echo " "
echo " --- Copying --- "
cp -Rf $branchName/* .
# Delete clone
rm -Rf $branchName
# Stage files
echo " "
echo " --- Creating commit --- "
git add .
# Commit
git commit -m "$commitName"
# Push branch to origin
echo " "
echo " --- Pushing new branch --- "
git push --set-upstream origin $branchName
# Restore or delete Stash
if [ "$stashCommand" = "apply" ]
then
echo " "
echo " --- Retrieving old source --- "
git stash apply
git stash drop
elif [ "$stashCommand" = "drop" ]
then
git stash drop
fi
echo " "
echo " --- Import successfull --- "
echo " "