Created
May 5, 2020 20:01
-
-
Save rockon1985/fb73db75e696dd3fe1c90b0fcb055372 to your computer and use it in GitHub Desktop.
Python script to initialize a git repo and generate several commits in it
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # GIT COMMITS CREATOR # | |
| # ====================== | |
| # WARNING!!!!! | |
| # _____ | |
| # / \ | |
| # | () () | | |
| # \ ^ / | |
| # ||||| | |
| # ||||| | |
| # This script creates new commits in your existing folder. | |
| # THE AUTHOR CERTAINLY HOPES YOU KNOW WHAT YOU'RE DOING | |
| # About: This script initializes an empty repository in a folder | |
| # and created several commits adding few files and folders | |
| # at a time. The script needs to be placed in the target folder | |
| # itself in which you need to create the commits. | |
| # Do not worry the script file would not add/commit the script file itself | |
| # it ignores itself while creating commits | |
| # To run the script you need python 3+ installed | |
| # Author: Ketan Saxena | |
| # Usage: Just copy this file in target folder and run `python git_create_commits.py` | |
| # GITHUB_REPO_LINK = link to your remote repository | |
| # GITHUB_EMAIL = It should have the author email for all the commits | |
| # GITHUB_USERNAME = It should have the author name for all the commits | |
| # | |
| from os import walk | |
| import random | |
| import os | |
| # IMPORTANT: Please update below variables before executing this script | |
| GITHUB_REPO_LINK="https://github.com/username/repo-name.git" | |
| GITHUB_USERNAME="Test User" | |
| GITHUB_EMAIL="test.user@github.com" | |
| os.system("git init") | |
| os.system("git remote add origin % s"% GITHUB_REPO_LINK) | |
| os.system("git config user.name '% s'"% GITHUB_USERNAME) | |
| os.system("git config user.email % s"% GITHUB_EMAIL) | |
| for (dirpath, dirnames, filenames) in walk("./"): | |
| random_directories = random.sample(dirnames, random.randint(1, len(dirnames))) | |
| for dirname in dirnames: | |
| print dirname | |
| # iterating some random directories | |
| # and making extra commit with only few files from that random directory | |
| # if dirname in random_directories: | |
| print("iterating subdirectory % s ..."% dirname) | |
| all_files = os.popen("ls -p ./% s | grep -v /$"% dirname).read().split("\n") | |
| all_files.remove('') | |
| if len(all_files) > 0: | |
| few_random_files = random.sample(all_files, random.randint(1, len(all_files))) | |
| few_random_files = map(lambda x: "% s/% s" %(dirname, x), few_random_files) | |
| few_random_files = " ".join(few_random_files) | |
| os.system("git add % s"% few_random_files) | |
| os.system("git commit -m 'Added files % s'"% few_random_files) | |
| # Commiting each of the directory separately | |
| os.system("git add % s"% dirname) | |
| os.system("git commit -m 'Add % s directory'"% dirname) | |
| # Commiting each of the file separately | |
| for filename in filenames: | |
| if(filename == __file__): | |
| continue | |
| print filename | |
| os.system("git add % s"% filename) | |
| os.system("git commit -m 'Add the file % s'"% filename) | |
| break | |
| # TODO: Spread the commits over a long period in history | |
| print("================ Now Spreading the commits") | |
| commits = os.popen("git log --pretty=oneline | awk -F ' ' '{print $1}' | tac").read().split("\n") | |
| for i in commits: | |
| print(i) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment