38

Bulk Find and Replace in multiple files in multiple Git Repositories

 4 years ago
source link: https://itnext.io/bulk-find-and-replace-in-multiple-files-in-multiple-git-repositories-487c7c433b75?source=friends_link&gi=525d957751c8
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

You have 2 free member-only stories left this month.

Bulk Find and Replace in multiple files in multiple Git Repositories

Image for post
Image for post

Last week, I have needed to change some configs in our AWS CloudFormation templates in multiple Git repositories for multiple environments. (Sometimes we need to do similar changes like that.) So I created a simple Python script for making these changes.

This script is useful when you want to change multiple part of multiple files in multiple git repositories in bulk. Script creates a new branch from the given base branch and commits changes into it. Maybe pushing to remote also could be added with a small change.

I used this script for changing the auto scaling configs inside the CloudFormation templates for multiple micro services in our project.

How to run the script?

First clone repository

git clone https://github.com/omerkarabacak/bulk-find-and-replace-in-git-repositories.git

What to do before run?

In config.json file, change these;

  • Add your new branch names and repositories in repository_list
  • Add texts to find and replace find_and_replace_list
  • Add files to check in file_list
  • Change commit message commit_message
  • Change base branch base_branch
  • Change repository directory if needed repositories_directory
  • Change author name repository_author.name
  • Change author email repository_author.email
  • Change SSH key path if you need something specific ssh_key

You are ready to go!

How to run with Docker?

There are 2 options. You can build your Docker image with Dockerfile or use ready public Docker image from Docker Hub.

Option 1: Build your own Docker image and use it

Build Docker image:

docker build -t bulk-find-and-replace-in-git-repositories:1.0 .

Run with built local Docker image:

docker run --rm -v $(pwd)/config.json:/app/config.json -v $(pwd)/repositories:/app/repositories -v ~/.ssh/id_rsa:/root/.ssh/id_rsa bulk-find-and-replace-in-git-repositories:1.0

Option 2: Use public Docker image hosted on Docker Hub

Run with public Docker image:

docker run --rm -v $(pwd)/config.json:/app/config.json -v $(pwd)/repositories:/app/repositories -v ~/.ssh/id_rsa:/root/.ssh/id_rsa omerkarabacak/bulk-find-and-replace-in-git-repositories:1.0

How to run with Python virtual environment?

First create virtual environment

python3 -m venv env

Enter virtual environment

source env/bin/activate

Install dependencies

pip install -r requirements.txt

Run script

python findandreplace.py

Which Python version?

Tested with 3.6.8

UPDATE 1:

I have changed script to be more elastic and moved config part to a json file and also added Dockerfile to be run as Docker image. Please look at the updated README.md in repository.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK