1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
| if __name__ == '__main__': repo_path = os.getcwd() try: repo = git.Repository(repo_path) except: print(red('[{:s}]不是一个git仓库').format(repo_path)) exit()
status = [item for item in repo.status().items() if item[1] != 16384] if len(list(status)) == 0: print(yellow('仓库[{:s}]未发生更改').format(repo_path)) else: create_items = [] modify_items = [] delete_items = [] for item in status: if item[1] == 128: create_items.append(item[0]) elif item[1] == 256: modify_items.append(item[0]) elif item[1] == 512: delete_items.append(item[0])
username = get_config(repo, 'user.name') useremail = get_config(repo, 'user.email') if username is None or useremail is None: exit()
for item in create_items: print(green('create: [{:s}]').format(item)) for item in modify_items: print(yellow('modify: [{:s}]').format(item)) for item in delete_items: print(red('delete: [{:s}]').format(item))
print('\nAuthor: {:s}[{:s}]'.format(gray(username), gray(useremail)))
while True: print(yellow('\n请输入commit信息(为空则自动填充时间)')) msg = input() msg = time.strftime('%Y年%m月%d日 %H:%M:%S', time.localtime( time.time())) if msg == '' else msg
if len(msg) > 50: print(gray('消息过长,请控制在50以内:')) print(msg[0:50]) continue break
if not git_add_all(repo): exit() hash = git_commit(repo, msg, username, useremail) if hash is None: print(red('提交失败!')) exit()
print(green('\n**********执行 commit 成功! **********')) commit = repo[hash]
hash = blue(str(commit.id)[0:10] + '...') time = yellow(time.strftime('%Y年%m月%d日 %H:%M:%S', time.localtime(commit.commit_time))) name = commit.author.name email = commit.author.email
diff = repo.diff(commit.id, commit.parent_ids[0] if len( commit.parent_ids) > 0 else '4b825dc642cb6eb9a060e54bf8d69288fbee4904') new_lines = 0 rmv_lines = 0 files_changed = [[], [], []] for patch in diff: line_stat = patch.line_stats new_lines += line_stat[2] rmv_lines += line_stat[1]
line_modi = '{:s} {:s}'.format(green('+{:d}'.format(line_stat[2])), red( '-{:d}'.format(line_stat[1]))) if patch.delta.is_binary == False else ''
if patch.delta.status == 2: files_changed[0].append('* {type} {file} {line_modi}'.format( file=gray(patch.delta.new_file.path), type=green('create'), line_modi=line_modi) ) elif patch.delta.status == 1: files_changed[2].append('* {type} {file} {line_modi}'.format( file=gray(patch.delta.new_file.path), type=red('delete'), line_modi=line_modi) ) else: files_changed[1].append('* {type} {file} {line_modi}'.format( file=gray(patch.delta.new_file.path), type=yellow('modify'), line_modi=line_modi) )
files_changed = files_changed[0] + files_changed[1] + files_changed[2]
msg = commit.message.replace('\n', '') print( '{hash} {time} {author} {new_lines} {rmv_lines} {msg}'.format( hash=hash, time=time, msg=msg, author=gray('{name}[{email}]'.format(name=name, email=email)), new_lines=green('+{:d}'.format(new_lines)), rmv_lines=red('-{:d}'.format(rmv_lines)), ) ) for item in files_changed: print(item)
print(green('\n**********开始push到远程仓库**********'))
if len(repo.remotes) == 0: print(gray("远程仓库列表为空,无需push")) exit() else: cur_branch = get_current_branch(repo) if cur_branch is None: print(red('分支读取出错,请通过git检查')) exit() print('Current Branch: [{:s}]'.format(blue(cur_branch)))
for remote in repo.remotes: try: print('\n正在推送远程仓库[{:s}]: {:s}'.format( blue(remote.name), green(remote.url))) os.system('git push {:s} {:s}'.format(remote.name, cur_branch)) print(yellow('ok')) except Exception as e: print(gray('失败: {:s}').format(red(str(e)))) exit()
|