Skip to content

Instantly share code, notes, and snippets.

@maidamai0
Last active April 7, 2021 08:38
Show Gist options
  • Select an option

  • Save maidamai0/76940f70ebc19190bc3cfd61e03339e8 to your computer and use it in GitHub Desktop.

Select an option

Save maidamai0/76940f70ebc19190bc3cfd61e03339e8 to your computer and use it in GitHub Desktop.
create some git commit to do some experiments such as merge and rebase
import subprocess
import time
import os
import argparse
class Commit:
def __init__(self, new: bool) -> None:
self.commit_num = 0
if new:
self.new_envionment()
else:
self.get_commit_count()
def run(self, loop: int):
for i in range(0, loop):
subprocess.call("git checkout feat")
self.commit(2)
subprocess.call("git checkout main")
self.commit(2)
def get_commit_count(self):
p = subprocess.Popen("git rev-list --all --count",
stdout=subprocess.PIPE)
self.commit_num = int(p.communicate()[0])
def new_envionment(self):
# os.remove on windows may fail for .git dir
# here I use the linux command rm.exe in the git/bin dirctory
subprocess.call("rm.exe -rf .git/")
for [root, dir, files] in os.walk("."):
for file in files:
if file == "commit.py":
continue
os.remove(os.path.join(root, file))
subprocess.call("git init")
self.commit(1)
subprocess.call("git checkout -b feat")
subprocess.call("git checkout main")
self.commit_num = 0
def commit(self, size: int):
for i in range(0, size):
subprocess.call(f"touch {self.commit_num}")
subprocess.call(f"git add .")
subprocess.call(
f'git commit -a -m "commit {self.commit_num}"', stdout=subprocess.DEVNULL)
print(f"commit {self.commit_num}")
time.sleep(1)
self.commit_num = self.commit_num + 1
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="git practice tool")
parser.add_argument(
"-n", action='store_true', help="clean and build a new envionment with some commits")
args = parser.parse_args()
cmt = Commit(args.n)
cmt.run(3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment