Last active
April 18, 2018 05:49
-
-
Save albertofwb/0297c1744dfaf2630c77d6008a49ac83 to your computer and use it in GitHub Desktop.
使用脚本模拟多次提交记录,主要是为了练习 git 二分法定位bug (git bisect) ,参见博客:http://www.oschina.net/translate/letting-git-bisect-help-you
This file contains 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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# Time : 2017/4/30 12:00 | |
# Author : Albert | |
# Email : albertofwb@gmail.com | |
# Site : http://cnblogs.com/albertofwb | |
# File : make_commits.py | |
# Editor : PyCharm | |
import os | |
import subprocess | |
TEST_FILENAME = "file.txt" | |
GIT_DIR = '.git' | |
END_NUMBER = 10 | |
def append_to_file(text, filename): | |
with open(filename, 'a+') as fp: | |
fp.write(text) | |
def git_commit(file_path, commit_msg): | |
add_cmd = "git add %s" % file_path | |
cmt_cmd = "git commit -m \"{CMT_MSG}\"".format(CMT_MSG=commit_msg) | |
multi_cmd = [add_cmd, cmt_cmd] | |
for cmd in multi_cmd: | |
print(cmd) | |
subprocess.check_output(cmd, shell=True) | |
def init_git(): | |
if os.path.exists(GIT_DIR): | |
print("[exit] detect .git already exists") | |
exit(1) | |
cmd = "git init ." | |
subprocess.check_output(cmd, shell=True) | |
def simulate_work(): | |
init_git() | |
file_path = TEST_FILENAME | |
end_num = END_NUMBER+1 | |
for i in range(1, end_num): | |
text = str(i) + '\n' | |
append_to_file(text, file_path) | |
git_commit(TEST_FILENAME, "append '{DATA}' to {FILE}".format(DATA=i, FILE=file_path)) | |
SCRIPT_FILE = "test.sh" | |
def generate_test_sh(): | |
script_path = SCRIPT_FILE | |
script_text = \ | |
'''#!/bin/bash | |
if [[ `grep 7 file.txt` ]]; then | |
exit 1 | |
else | |
exit 0 | |
fi''' | |
if os.path.exists(script_path): | |
print("%s already exists" % script_path) | |
else: | |
print("writing\n%s to %s" %(script_text, script_path)) | |
open(script_path, 'w').write(script_text) | |
if __name__ == '__main__': | |
simulate_work() | |
generate_test_sh() | |
os.system("git bisect run bash ./{file}".format(file=SCRIPT_FILE)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment