Skip to content

Commit

Permalink
GIT Mass operations
Browse files Browse the repository at this point in the history
Pull, rebase, list and cleanup
  • Loading branch information
slemrmartin committed Jun 21, 2019
1 parent fbe3239 commit bc26ae4
Show file tree
Hide file tree
Showing 10 changed files with 255 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
git_utils/config.dev.sh
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
* [Developing using a Vagrant VM](vagrant_developer_vm.md)
* [Setting up Kubernetes for use with ManageIQ](providers/kubernetes.md)
* [Contributing to the API](https://github.com/ManageIQ/manageiq-api/blob/master/CONTRIBUTING.md)
* [GIT Helpers](git_utils/README.md)

### ManageIQ technical documentation
* [Architecture](architecture.md)
Expand Down
24 changes: 24 additions & 0 deletions git_utils/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
## GIT Helpers for ManageIQ

Following scripts iterate over core and plugins and performs useful mass operations.

### Config

[config.sh](config.sh) file contains basic settings, like your home and plugins directory of ManageIQ.
If you want to redefine some value, create `config.dev.sh` file (excluded from git) and override variable.

### Repositories

List of repositories are either generated automatically (`$custom_repo_list == 0`) or you can define your own array manually
(`$custom_repo_list == 1`). See config for example.

### Scripts

- [pull.sh](pull.sh): Checkouts all unchanged repos to master and pulls changes
- [rebase.sh](rebase.sh): For all unchanged repos does the same as pull.sh and then rebases your current branch
- [list-branches.sh](list-branches.sh): Prints current branches in all repos
- [list-changes.sh](list-changes.sh): Prints changes in all repos
- [cleanup.sh](scripts/git/cleanup.sh): Deletes branches merged to master
- Without args: local branches only
- With `--remote-cleanup` arg: also deletes merged remote branches

50 changes: 50 additions & 0 deletions git_utils/cleanup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/bin/bash
# Script for git cleanup - delete merged branches
# Switches to master branches
#
# Usage: cleanup.sh [--remote-cleanup]

source config.sh

case $1 in
-f|--remote-cleanup) remote_cleanup=1;;
esac

cd ${manageiq_root}

for path in ${repositories[@]}
do
repo=$(basename ${path})
echo "${repo} -------------------------------------------------------"
cd ${path}

# This has to be run from master
git checkout master

# Update our list of remotes
git fetch
git remote prune origin

# Remove local fully merged branches
local_merged=$(git branch --merged master | grep -v 'master$')
if [[ -z ${local_merged} ]]; then
echo "No local branches to delete"
else
echo ${local_merged} | xargs git branch -d
fi


has_upstream=`git branch -a | grep upstream | wc -l`

if [[ ${remote_cleanup} -eq 1 && "$has_upstream" -gt "0" ]]
then
remote_merged=$(git branch -r --merged master | sed 's/ *origin\///' | grep -v 'master$')
if [[ -z ${remote_merged} ]]; then
echo "No remote branches to delete"
else
# Remove remote fully merged branches
echo ${remote_merged} | xargs -I% git push origin :%
fi
fi
cd ..
done
22 changes: 22 additions & 0 deletions git_utils/config.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env bash

# You can override these values in file config.dev.sh
homedir=~
eval homedir=${homedir}
manageiq_root="${homedir}/Projects/ManageIQ/upstream"
plugins_dir="plugins"

if [[ -f "./config.dev.sh" ]]; then
source "config.dev.sh"
fi

# $custom_repo_list:
# - when 0, then all repos in $manageiq_root and plugins are used
# - when 1, user-defined list ${repositories} is used.
# Can be defined in config.dev.sh (see example below)
custom_repo_list=0
#repositories=("${manageiq_root}"
# "${manageiq_root}/${plugins_dir}/manageiq-ui-classic"
# "${manageiq_root}/${plugins_dir}/manageiq-api")

source repositories.sh
17 changes: 17 additions & 0 deletions git_utils/list-branches.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash
# List of current branches in MiQ repositories

source config.sh

cd ${manageiq_root}

for path in ${repositories[@]}
do
cd ${path}
current_branches=$(git branch)
echo ""
echo $(basename ${path})
echo "${current_branches}"
cd ..
done

33 changes: 33 additions & 0 deletions git_utils/list-changes.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env bash

# Script for list your changes in all repos
#
# NOTE: Edit your variables below!
source config.sh

cd ${manageiq_root}

empty_line=1

for path in ${repositories[@]}
do
cd ${path}
repo=$(basename ${path})
current_branch=$(git rev-parse --abbrev-ref HEAD)

if git diff-index --quiet HEAD --; then
echo "${repo}: No Changes (${current_branch})"
empty_line=0
else
if [[ ${empty_line} -eq 0 ]]; then
echo ""
fi
echo "<$repo> -------------------------------------------------------"
git status -s -b
echo "</$repo> ------------------------------------------------------"
echo ""
empty_line=1
fi

cd ..
done
45 changes: 45 additions & 0 deletions git_utils/pull.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/bin/bash

# Script for git updating git branch master topology inventory plugins
# Switches to master branches
source config.sh

cd ${manageiq_root}

modified_repos=()

for path in ${repositories[@]}
do
repo=$(basename ${path})

echo "${repo} -------------------------------------------------------"
cd ${path}
current_branch=$(git rev-parse --abbrev-ref HEAD)


if git diff-index --quiet HEAD --; then
git fetch --all --prune
git checkout master

has_upstream=`git branch -a | grep upstream | wc -l`

if [[ "$has_upstream" -gt "0" ]]; then
git pull upstream master
else
git pull origin master
fi
else
echo "Changes in branch ${current_branch}"
modified_repos+=(${repo})
fi

cd ..
done

echo ""
echo "Repositories with changes (cannot apply git pull):"
for repo in ${modified_repos[@]}
do
echo ${repo}
done

49 changes: 49 additions & 0 deletions git_utils/rebase.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/bin/bash

# Script for git updating master git branch an rebase current working branch

source config.sh

cd ${manageiq_root}

modified_repos=()

for path in ${repositories[@]}
do
echo "${repo} -------------------------------------------------------"
cd ${path}
current_branch=$(git rev-parse --abbrev-ref HEAD)


if git diff-index --quiet HEAD --; then
echo "* Updating (rebase) branch ${current_branch}"
git fetch --all --prune
git checkout master

has_upstream=`git branch -a | grep upstream | wc -l`

if [[ "$has_upstream" -gt "0" ]]; then
git pull upstream master
else
git pull origin master
fi

if [[ ${current_branch} -ne "master" ]]; then
git checkout ${current_branch}
git rebase master
fi
else
echo "Cannot update branch ${current_branch}: Modified files present"
modified_repos+=(${repo})
fi

cd ..
done

echo ""
echo "Repositories with changes (cannot apply git rebase):"
for repo in ${modified_repos[@]}
do
echo ${repo}
done

13 changes: 13 additions & 0 deletions git_utils/repositories.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env bash

if [[ ${custom_repo_list} -eq 0 ]]; then
repositories=("${manageiq_root}")
idx=1
for dir in ${manageiq_root}/${plugins_dir}/*
do
if [[ -d ${dir} ]]; then
repositories[${idx}]=${dir}
idx=$((idx+1))
fi
done
fi

0 comments on commit bc26ae4

Please sign in to comment.