Skip to content

Instantly share code, notes, and snippets.

@jcarsique
Last active May 12, 2017 10:12
Show Gist options
  • Save jcarsique/29ca0df9166926183e2f to your computer and use it in GitHub Desktop.
Save jcarsique/29ca0df9166926183e2f to your computer and use it in GitHub Desktop.
Prepare Git repository for merge (workaround for https://github.com/robinst/git-merge-repos/issues/3 , used for https://jira.nuxeo.com/browse/NXP-15865)

Execute prepare_repo.sh on the repository(ies) to merge.

Clone https://github.com/robinst/git-merge-repos

Run ./run.sh /path/to/repo1:. /path/to/repo2:.

Merged repository is in merged-repo

Update .gitignore and remove those in sub-directories

git remote add origin ...
# Optimize repository
git reflog expire --expire=now --all
git gc --aggressive --prune=now
git fsck
# Push changes
git push --all -f --prune origin
git push --tags -f --prune origin
#!/bin/bash
##
## (C) Copyright 2014-2015 Nuxeo SA (http://nuxeo.com/) and contributors.
##
## All rights reserved. This program and the accompanying materials
## are made available under the terms of the GNU Lesser General Public License
## (LGPL) version 2.1 which accompanies this distribution, and is available at
## http://www.gnu.org/licenses/lgpl-2.1.html
##
## This library is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
## Lesser General Public License for more details.
##
## Contributors:
## Julien Carsique
## Thomas Roger
##
## Prepare a Git repository for merge with https://github.com/robinst/git-merge-repos
## Applies a workaround for https://github.com/robinst/git-merge-repos/issues/3 by moving the content into a subdir before
## the call to git-merge-repos/run.sh
## Usage: ./prepare_repo.sh <subdir> <JIRA ref>
set -e
rm -f /tmp/ref-*
fmt='
#set -x
ref=%(refname)
unique=`git rev-parse $ref`
ref=${ref#refs/remotes/origin/}
if [[ $ref != HEAD ]]; then
echo "Working on ref=$ref ($unique)"
git checkout -q $ref
if [ -f /tmp/ref-$unique ]; then
echo "Align on $(cat /tmp/ref-$unique)"
git reset --hard $(cat /tmp/ref-$unique)
else
git clean -fdq
mkdir MERGETMP
mkdir -p $(dirname $SUB_DIR)
git mv $(git ls-tree --name-only HEAD) MERGETMP/
git mv MERGETMP $SUB_DIR
# repeat as workaround for "error building trees" due to some filesystem latency
git commit -q -m"$JIRA: prepare repositories merge" -a || git commit -q -m"$JIRA: prepare repositories merge" -a
git rev-parse HEAD>/tmp/ref-$unique
fi
fi
'
eval=`git for-each-ref --shell --format="$fmt" refs/remotes`
SUB_DIR=$1 JIRA=$2 eval "$eval"
#set +x
fmt='
#set -x
ref=%(refname)
unique=`git rev-parse $ref`
ref=${ref#refs/tags/}
if [[ $ref != HEAD ]]; then
echo "Working on ref=$ref ($unique)"
git checkout -q $ref
if [ -f /tmp/ref-$unique ]; then
echo "Align on $(cat /tmp/ref-$unique)"
git tag -f $ref $(cat /tmp/ref-$unique)
else
git clean -fdq
mkdir MERGETMP
mkdir -p $(dirname $SUB_DIR)
git mv $(git ls-tree --name-only HEAD) MERGETMP
git mv MERGETMP $SUB_DIR
# repeat as workaround for "error building trees" due to some filesystem latency
git commit -q -m"$JIRA: prepare repositories merge" -a || git commit -q -m"$JIRA: prepare repositories merge" -a
git tag -f $ref
git rev-parse HEAD>/tmp/ref-$unique
fi
fi
'
eval=`git for-each-ref --shell --format="$fmt" refs/tags`
SUB_DIR=$1 JIRA=$2 eval "$eval"
@jcarsique
Copy link
Author

WARNING:

  • Commit message must be customized
  • Use git-up if you have local branches to avoid loosing commits

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment