#!/bin/bash
# Usage: -h for help
if [ "$1" == '-h' ]; then
cat <<BOO
Will create an appropriate p4gf_config and check it into Perforce
using the current directory name as the repo name. Must be in a Git
repository to run. After creating the config, will provide the
git commands needed to finish the initial conversion into Perforce.
Assumes:
- git, p4 and submit-file.sh are executable and in your PATH
- You can log into the target Perforce server
- You know where in the Perforce server you want to save the repo
- You know the HTTPS or SSH address for the Git Fusion server
BOO
exit 0
fi
# ynq "prompt"
yn=''
ynq ()
{
yn=''
while [ "$yn" == '' ]; do
read -p "$1 (y/n/q) " yn
if [ "$yn" == 'q' ]; then
echo 'Aborting...'
exit 1
fi
if [ "$yn" != 'y' ] && [ "$yn" != 'n' ]; then
echo ' Please answer y n or q'
yn=''
fi
done
}
bin_exists ()
{
if [ "$1" == '' ]; then
echo 'Must provide an argument'
exit 1
fi
check=$(which "$1")
if [ "$check" == '' ]; then
echo "Please make sure '$1' is executable and in your PATH"
exit 1
fi
}
validateP4Login ()
{
p4user=$(p4 set | grep P4USER | sed -e 's/^P4USER=//')
p4port=$(p4 set | grep P4PORT | sed -e 's/^P4PORT=//')
if [ "$p4user" == '' ] || [ "$p4port" == '' ]; then
if [ "$p4user" == '' ]; then
p4user="$USER"
fi
if [ "$p4port" == '' ]; then
p4port='perforce:1666'
fi
echo 'Please define P4USER and P4PORT, then login'
echo "e.g., export P4PORT=$p4port"
echo " export P4USER=$p4user"
echo ' p4 login'
exit 1
fi
logged=$(p4 login -s 2>&1 | grep -c ' ticket expires in ')
if [ "$logged" -ne 1 ]; then
echo 'Target Perforce server:'
echo " P4USER=$p4user"
echo " P4PORT=$p4port"
echo 'Please run "p4 login" for the target Perforce server'
exit 1
fi
}
bin_exists git
bin_exists p4
bin_exists submit-file.sh
validateP4Login
git branch --list > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo 'Please run inside of the git repository to be converted'
exit 1
fi
REPO=${PWD/*\//}
unknown=$(p4 users | egrep -c '^unknown_git ')
if [ "$unknown" -ne 1 ]; then
echo 'Git Fusion is not configured for anonymous authors.'
echo 'In order to convert this repo, either'
echo '- all of the authors must exist as Perforce users, or'
echo '- configure this repo to use the Pusher as the change owner, or'
echo '- quit this script and create a Perforce "unknown_git" user.'
ynq 'Continue?'
if [ "$yn" == 'n' ]; then
echo 'Aborting...'
exit 1
fi
fi
read -p 'What is the IP address or machine name of Git Fusion? ' GFH
if [ "$GFH" == '' ]; then
echo 'Aborting...'
exit 1
fi
read -p 'Which protocol are you using to connect Git to Git Fusion? (ssh/https) ' type
if [ "$type" == '' ]; then
echo 'Aborting...'
exit 1
fi
if [ "$type" == 'ssh' ]; then
GFH="git@$GFH:"
else
GFH="https://$GFH/"
fi
yn=''
while [ "$yn" == '' ] || [ "$yn" != 'y' ]; do
ynq "Use '$REPO' as the name of the repo?"
if [ "$yn" == 'n' ]; then
REPO=''
read -p 'What do you want to call the repo? ' REPO
if [ "$REPO" == '' ]; then
echo 'Aborting...'
exit 1
fi
fi
done
depotPath="//depot2/$REPO"
yn='n'
while [ "$yn" == 'n' ]; do
if [ "$depotPath" != '' ]; then
ynq "Use '$depotPath' as the target directory in Perforce?"
fi
if [ "$yn" == 'n' ]; then
depotPath=''
echo 'What depot directory do you want to use?'
read -p " (e.g., //depot/$REPO or //depot/main/$REPO) " depotPath
if [ "$depotPath" == '' ]; then
echo 'Aborting...'
exit 1
fi
fi
exists=$(p4 dirs "$depotPath" 2>&1 | grep -c ' - no such file(s).')
if [ "$exists" -ne 1 ]; then
echo ''
echo " There is something wrong with $depotPath. Try again."
echo ' Use the form //DEPOT_NAME/DIR_NAME{/DIR_NAME}*'
echo ' this directory must not exist.'
echo ''
depotPath=''
yn='n'
fi
done
ynq 'Do you want all future branches in Git to be full branches in Perforce?'
fullBranches="$yn"
pusherOwner='n'
if [ "$unknown" -ne 1 ]; then
ynq 'Do you to use the Pusher as the owner of all submitted changes?'
pusherOwner="$yn"
fi
cat > p4gf_config <<BOO
[@repo]
description = Initial Git Fusion configuration for $REPO
charset = utf8
BOO
if [ "$fullBranches" == 'y' ]; then
cat >> p4gf_config <<BOO
depot-branch-creation-enable = all
depot-branch-creation-depot-path = $depotPath/{git_branch_name}
BOO
fi
if [ "$pusherOwner" == 'y' ]; then
cat >> p4gf_config <<BOO
change-owner = pusher
BOO
fi
for b in $(git branch --list | sed -e 's/^[*]//' -e 's/ //g'); do
normalizedBranch=$(echo "$b" | tr "'#@%$/" '_' | tr '"' '_')
cat >> p4gf_config <<BOO
[$normalizedBranch]
view = $depotPath/$normalizedBranch/... ...
git-branch-name = $b
BOO
done
submit-file.sh p4gf_config "//.git-fusion/repos/$REPO" "Initial Repo Config for $REPO"
if [ "$?" -ne 0 ]; then
echo "Failed to submit p4gf_config to '//.git-fusion/repos/$REPO'"
exit 1
fi
git remote set-url origin "$GFH$REPO"
echo 'To convert repo, run: git push --all'