-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgit-sub-branch
executable file
·42 lines (37 loc) · 1.08 KB
/
git-sub-branch
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
#!/bin/sh
if [ -z "$1" -o -z "$2" ]
then
echo "Usage:\n\tgit sub-branch <sub-dir> <branch>"
exit 1;
fi
sumDir="."
tree=`git cat-file commit HEAD | grep '^tree ' | awk '{print \$2}'`
# Find the tree ID for the sub-directory
for dir in `echo "$1" | tr "/" "\n"`
do
sumDir="$sumDir/$dir"
if [ ! -z "$dir" ]
then
tree=`git cat-file -p "$tree" | grep "\t${dir}$" | awk '{print $3}'`
if [ -z "$tree" ]
then
echo "Could not find sub-dir: ${sumDir}"
exit 1
fi
fi
done
# Copy latest commit message
git log -n 1 --format="%B" > .git/.SUB_BRANCH_MSG
if (git rev-parse --verify "$2" &> /dev/null)
then
echo "\n\n# adding to branch: $2\n" >> .git/.SUB_BRANCH_MSG
$EDITOR .git/.SUB_BRANCH_MSG
commitMsg=`cat .git/.SUB_BRANCH_MSG | sed '/^#/d'`
newCommit=`cat .git/.SUB_BRANCH_MSG | git commit-tree "${tree}" -p "$2" -m "$commitMsg"`
else
echo "\n\n# new branch: $2\n" >> .git/.SUB_BRANCH_MSG
$EDITOR .git/.SUB_BRANCH_MSG
commitMsg=`cat .git/.SUB_BRANCH_MSG | sed '/^#/d'`
newCommit=`cat .git/.SUB_BRANCH_MSG | git commit-tree "${tree}" -m "$commitMsg"`
fi
git branch -f "$2" "$newCommit"