forked from genome/genome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-project
executable file
·116 lines (98 loc) · 2.92 KB
/
create-project
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
#!/bin/bash
GIT_SRV=git
GIT_PATH=/srv/git
SKEL_URL=ssh://$GIT_SRV$GIT_PATH/example-c-project.git
DO_PUSH=1
[ -z "$TMPDIR" ] && TMPDIR=/tmp
function bail() {
echo $1
exit 1
}
function usage() {
echo "Usage: `basename $0` [-n] <project name>"
echo " Initialize a new c/c++ project"
echo ""
echo "Options:"
echo " -h|--help - this message"
echo " -n|--no-push - do not automatically commit and push the new subrepo"
}
function enforce_clean_master() {
branch=`git branch | perl -ne 's/^\* // && print'`
if [ "$branch" != "master" ]
then
bail "This program must be run on branch master, you are on branch $branch"
fi
git fetch || bail "git fetch failed"
git status -uno | grep -A1 '^# Your branch ' &&
bail "Your branch is not in sync with the remote repo, pull/push first"
git push origin master --dry-run > /dev/null 2>&1 ||
bail "git push --dry-run failed, please make sure your repo is in sync with the remote (pull/push)."
}
function create_git_repo() {
name=$1
repo_path=$GIT_PATH/$name.git
ssh $GIT_SRV "if [ ! -e $repo_path ]; then mkdir $repo_path && cd $repo_path && git init --bare --shared; else echo $repo_path already exists on server $GIT_SRV; exit 1; fi"
[ $? -ne 0 ] && {
echo "Aborted."
exit 1
}
(pushd $TMPDIR && git clone ssh://$GIT_SRV/$repo_path &&
pushd $name && touch .gitignore &&
git add .gitignore && git commit -m 'added .gitignore' && git push origin master &&
popd && rm -rf $name) || {
echo "Failed to set up repository $name"
exit 1
}
git submodule add ssh://$GIT_SRV/$repo_path || {
echo "Failed to set up submodule for $name"
exit 1
}
}
function create_project_skel() {
git archive --remote $SKEL_URL master | tar xf -
}
# main -------------------------------------------------------------------
enforce_clean_master
project_name=
while [ $# -gt 0 ]; do
case $1 in
-h|--help) usage; exit 0 ;;
-n|--no-push) DO_PUSH=0 ;;
-*) bail "Unexpected argument: $1" ;;
*) [ "$project_name" != "" ] && {
bail "Multiple project names specified: '$project_name' and '$1'"
}
project_name=$1
;;
esac
shift
done
if [ -z "$project_name" ]; then
echo "Error: no project name specified"
usage
exit 1
fi
[ -e $project_name ] && {
echo "Error: something named $project_name already exists in `pwd`"
ls -ld $project_name
exit 1
}
echo ""
echo "Create new project named $project_name? (y/n)"
while read ans; do
case $ans in
y*) break;;
n*) echo "Aborted"; exit 1;;
*) echo "Please anwser y or n";;
esac
done
create_git_repo $project_name
git add $project_name
[ $DO_PUSH -eq 1 ] && {
git pull
git commit -m "Added submodule $project_name"
git push
echo "** pushed changes"
}
git status
echo "Completed."