forked from audreyt/stackato-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract-directory
More file actions
executable file
·95 lines (77 loc) · 2.11 KB
/
Copy pathextract-directory
File metadata and controls
executable file
·95 lines (77 loc) · 2.11 KB
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
#!/bin/bash
DEST=../stackato-sample-repos
GITHUB_ORG=Stackato-Apps
GITHUB_URL=https://api.github.com/orgs/$GITHUB_ORG/repos
HOME=`pwd`
usage() {
die "$(cat <<eos
Usage:
./extract-directory <directory-path> [<repo-name>]
eos
)"
}
extract() {
dir=$1
repo=$2
if [ -z $dir ]; then usage; fi
if [ -z $repo ]; then
repo=`echo -n $dir | sed 's/[^\/]*\/\([^\/]*\).*/\1/'`
fi
if [ -z $repo ]; then usage; fi
if [ ! -d $dir ]; then die "'$dir' is not a directory'"; fi
if [ -d "$dir/.git" ]; then die "'$dir' has already been extracted!"; fi
dest_dir="$DEST/$repo"
if [ -d $dest_dir ]; then die "$dest_dir already exists"; fi
echo "Extracting '$dir' into '$dest_dir'"
dir_slash=`echo -n $dir | sed 's/\/\?$/\//'`
private_repo_url=git@github.com:$GITHUB_ORG/$repo.git
public_repo_url=git://github.com/$GITHUB_ORG/$repo.git
json='{"name":"'$repo'"}'
run "$CURL -XPOST $GITHUB_URL -d '$json'"
run "cp -r . $dest_dir"
run "cd $dest_dir"
run "git reset --hard"
run "git filter-branch --prune-empty --subdirectory-filter $dir_slash -- --all"
run "git clean -fxd"
run "git remote set-url origin $private_repo_url"
run "git push --all"
diff=`git diff remotes/origin/develop`
if [ -z "$diff" ]; then
git branch -d develop
git push origin --delete develop
fi
run "cd $HOME"
run "git rm -r $dir"
run "git add ."
run "git commit -m 'extracted $dir into Stackato-Apps repo'"
run "git submodule add $public_repo_url $dir"
run "git add ."
run "git commit -m 'Added $dir as a subdir to $public_repo_url'"
run "git push origin master"
}
check() {
if [ -z $GITHUB_USER ]; then die "Set GITHUB_USER first"; fi
if [ -z $GITHUB_PASS ]; then die "Set GITHUB_PASS first"; fi
export CURL="curl -v -i -k -u '$GITHUB_USER:$GITHUB_PASS'"
}
die() {
echo "$1"
exit 1
}
run() {
echo $1
if [ -z $EXTRACT_TEST_ONLY ]; then
eval $1
catch
fi
}
runx() {
echo $1
}
catch() {
if [ $? -ne 0 ]; then
die "Error: command failed"
fi
}
check
extract $1 $2