1
0

GIT.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  1. import tkinter
  2. import tkinter.messagebox
  3. from tkinter.filedialog import askopenfilename, asksaveasfilename,askdirectory,askopenfilenames
  4. import os
  5. import Git_Ctrl
  6. from tkinter.scrolledtext import ScrolledText
  7. import time
  8. import threading
  9. str_code = 'utf-8'
  10. Git = Git_Ctrl.git_Ctrol()#需要去掉
  11. def Main():
  12. global top,Git,PATH,bg,bbg,fg,Git_List,Last_Name
  13. PATH = os.getcwd()
  14. Git = Git_Ctrl.git_Ctrol()
  15. Git_List = []
  16. top = tkinter.Tk()
  17. Last_Name = None
  18. bg = '#FFFAFA' # 主颜色
  19. bbg = '#FFFAFA' # 按钮颜色
  20. fg = '#000000' # 文字颜色
  21. top["bg"] = bg
  22. FONT = ('黑体', 11) # 设置字体
  23. top.title('CoTan仓库管理器')
  24. top.resizable(width=False, height=False)
  25. top.geometry('+10+10') # 设置所在位置
  26. width_B = 13 # 标准宽度
  27. height_B = 2
  28. a_y = 0
  29. a_x = 0
  30. global clone_url,Git_Box,Git_Dir
  31. global commit_m,head,master,no_ff
  32. global TagName,TagMessage,TagCommit,Show_Key
  33. global RemoteSSH,RemoteName,RemoteBranch,LocalBranch,push_bind,allow_history
  34. global BranchName, StashName, CommitName, BranchNOrigin
  35. tkinter.Button(top, bg=bbg, fg=fg, text='克隆仓库', command=clone_git, font=FONT, width=width_B,
  36. height=height_B).grid(column=a_x, row=a_y, sticky=tkinter.E + tkinter.W)
  37. tkinter.Button(top, bg=bbg, fg=fg, text='打开仓库', command=init_git, font=FONT, width=width_B,
  38. height=height_B).grid(column=a_x+1, row=a_y, sticky=tkinter.E + tkinter.W)
  39. tkinter.Button(top, bg=bbg, fg=fg, text='查看文件', command=get_Git_Dir, font=FONT, width=width_B,
  40. height=height_B).grid(column=a_x+2, row=a_y, sticky=tkinter.E + tkinter.W)
  41. a_y += 1
  42. tkinter.Label(top, text='克隆URL:', bg=bg, fg=fg, font=FONT, width=width_B, height=height_B).grid(column=a_x,row=a_y)
  43. Dic_Var = tkinter.StringVar()#当前的Dic
  44. clone_url = tkinter.Entry(top, width=width_B * 2, textvariable=Dic_Var)
  45. clone_url.grid(column=a_x+1, row=a_y, columnspan=2, sticky=tkinter.E + tkinter.W)
  46. a_y += 1
  47. Git_Box = tkinter.Listbox(top, width=width_B * 3, height=height_B * 4)
  48. Git_Box.grid(column=a_x, row=a_y, columnspan=3, rowspan=4, sticky=tkinter.E + tkinter.W + tkinter.S + tkinter.N)
  49. a_y += 4
  50. tkinter.Label(top, text='【仓库文件列表】', bg=bg, fg=fg, font=FONT, width=width_B * 3, height=height_B).grid(
  51. column=a_x,columnspan=3,row=a_y,sticky=tkinter.E + tkinter.W + tkinter.W + tkinter.S + tkinter.N) # 设置说明
  52. a_y += 1
  53. Git_Dir = tkinter.Listbox(top, width=width_B * 3, height=height_B * 4)
  54. Git_Dir.grid(column=a_x, row=a_y, columnspan=3, rowspan=4, sticky=tkinter.E + tkinter.W + tkinter.S + tkinter.N)
  55. a_y += 4
  56. tkinter.Button(top, bg=bbg, fg=fg, text='添加暂存区文件', command=Add_File, font=FONT, width=width_B,
  57. height=height_B).grid(column=a_x, row=a_y, sticky=tkinter.E + tkinter.W)
  58. tkinter.Button(top, bg=bbg, fg=fg, text='移除暂存区文件', command=Reset_File, font=FONT, width=width_B,
  59. height=height_B).grid(column=a_x+1, row=a_y, sticky=tkinter.E + tkinter.W)
  60. tkinter.Button(top, bg=bbg, fg=fg, text='提交到git', command=Commit_File, font=FONT, width=width_B,
  61. height=height_B).grid(column=a_x+2, row=a_y, sticky=tkinter.E + tkinter.W)
  62. a_y += 1
  63. tkinter.Button(top, bg=bbg, fg=fg, text='查看执行日志', command=lambda :not_Args(Git.reflog), font=FONT, width=width_B,
  64. height=height_B).grid(column=a_x, row=a_y, sticky=tkinter.E + tkinter.W)
  65. tkinter.Button(top, bg=bbg, fg=fg, text='查看文件日志', command=log, font=FONT, width=width_B,
  66. height=height_B).grid(column=a_x+1, row=a_y, sticky=tkinter.E + tkinter.W)
  67. tkinter.Button(top, bg=bbg, fg=fg, text='查看状态', command=lambda :not_Args(Git.status), font=FONT, width=width_B,
  68. height=height_B).grid(column=a_x+2, row=a_y, sticky=tkinter.E + tkinter.W)
  69. global log_Type
  70. a_y += 1
  71. log_Type = []
  72. lable = ['显示轴','commit完全显示','简化显示']#复选框
  73. for i in range(3):
  74. log_Type.append(tkinter.IntVar())
  75. tkinter.Checkbutton(top,bg = bg,fg = fg,activebackground=bg,activeforeground=fg,selectcolor=bg, text=lable[i],
  76. variable=log_Type[-1]).grid(column=a_x+i, row=a_y, sticky=tkinter.W)
  77. log_Type[-1].set(1)
  78. a_y += 1
  79. tkinter.Button(top, bg=bbg, fg=fg, text='版本回退', command=Back_version, font=FONT, width=width_B,
  80. height=height_B).grid(column=a_x, row=a_y, sticky=tkinter.E + tkinter.W)
  81. tkinter.Button(top, bg=bbg, fg=fg, text='文件回退', command=Back_File, font=FONT, width=width_B,
  82. height=height_B).grid(column=a_x+1, row=a_y, sticky=tkinter.E + tkinter.W)
  83. tkinter.Button(top, bg=bbg, fg=fg, text='删除文件', command=rm_file, font=FONT, width=width_B,
  84. height=height_B).grid(column=a_x+2, row=a_y, sticky=tkinter.E + tkinter.W)
  85. a_y += 1
  86. tkinter.Button(top, bg=bbg, fg=fg, text='查看分支', command=lambda :not_Args(Git.check_Branch), font=FONT, width=width_B,
  87. height=height_B).grid(column=a_x, row=a_y, sticky=tkinter.E + tkinter.W)
  88. tkinter.Button(top, bg=bbg, fg=fg, text='新建分支', command=make_Branch, font=FONT, width=width_B,
  89. height=height_B).grid(column=a_x+1, row=a_y, sticky=tkinter.E + tkinter.W)
  90. tkinter.Button(top, bg=bbg, fg=fg, text='切换分支', command=switch_Branch, font=FONT, width=width_B,
  91. height=height_B).grid(column=a_x+2, row=a_y, sticky=tkinter.E + tkinter.W)
  92. a_y += 1
  93. tkinter.Button(top, bg=bbg, fg=fg, text='删除分支', command=lambda :Delete_Branch(1), font=FONT, width=width_B,
  94. height=height_B).grid(column=a_x, row=a_y, sticky=tkinter.E + tkinter.W)
  95. tkinter.Button(top, bg=bbg, fg=fg, text='丢弃分支', command=lambda :Delete_Branch(0), font=FONT, width=width_B,
  96. height=height_B).grid(column=a_x+1, row=a_y, sticky=tkinter.E + tkinter.W)
  97. tkinter.Button(top, bg=bbg, fg=fg, text='合并分支', command=switch_Branch, font=FONT, width=width_B,
  98. height=height_B).grid(column=a_x+2, row=a_y, sticky=tkinter.E + tkinter.W)
  99. no_ff = tkinter.Variable()
  100. a_y += 1
  101. tkinter.Button(top, bg=bbg, fg=fg, text='合并分支', command=merge_Branch, font=FONT, width=width_B,
  102. height=height_B).grid(column=a_x, row=a_y, sticky=tkinter.E + tkinter.W)
  103. tkinter.Button(top, bg=bbg, fg=fg, text='退出冲突处理', command=lambda :not_Args(Git.merge_abort), font=FONT, width=width_B,
  104. height=height_B).grid(column=a_x+2, row=a_y, sticky=tkinter.E + tkinter.W)
  105. tkinter.Checkbutton(top, bg=bg, fg=fg, activebackground=bg, activeforeground=fg, selectcolor=bg, text='使用快速合并',
  106. variable=no_ff).grid(column=a_x + 1, row=a_y, sticky=tkinter.W)
  107. no_ff.set(0)
  108. a_y += 1
  109. tkinter.Button(top, bg=bbg, fg=fg, text='保存工作区', command=lambda :not_Args(Git.Save_stash), font=FONT, width=width_B,
  110. height=height_B).grid(column=a_x, row=a_y, sticky=tkinter.E + tkinter.W)
  111. tkinter.Button(top, bg=bbg, fg=fg, text='恢复工作区', command=lambda :Open_Stash(1), font=FONT, width=width_B,
  112. height=height_B).grid(column=a_x+1, row=a_y, sticky=tkinter.E + tkinter.W)
  113. tkinter.Button(top, bg=bbg, fg=fg, text='删除工作区', command=lambda :Open_Stash(0), font=FONT, width=width_B,
  114. height=height_B).grid(column=a_x+2, row=a_y, sticky=tkinter.E + tkinter.W)
  115. a_x += 3
  116. tkinter.Label(top, text='', bg=bg, fg=fg, font=FONT, width=1).grid(column=a_x, row=a_y) # 设置说明
  117. a_x += 1
  118. a_y = 0
  119. tkinter.Label(top, text='【参数操作】', bg=bg, fg=fg, font=FONT, width=width_B * 3, height=height_B).grid(
  120. column=a_x,columnspan=3,row=a_y,sticky=tkinter.E + tkinter.W + tkinter.W + tkinter.S + tkinter.N) # 设置说明
  121. a_y += 1
  122. tkinter.Label(top, text='提交描述:', bg=bg, fg=fg, font=FONT, width=width_B, height=height_B).grid(column=a_x,row=a_y)
  123. commit_m = tkinter.Entry(top, width=width_B * 2)
  124. commit_m.grid(column=a_x + 1, row=a_y, columnspan=2, sticky=tkinter.E + tkinter.W)
  125. a_y += 1
  126. tkinter.Label(top, text='diff分支:', bg=bg, fg=fg, font=FONT, width=width_B, height=height_B).grid(column=a_x,row=a_y)
  127. master = tkinter.Entry(top, width=width_B * 2)
  128. master.grid(column=a_x + 1, row=a_y, columnspan=2, sticky=tkinter.E + tkinter.W)
  129. a_y += 1
  130. tkinter.Label(top, text='回退版本号:', bg=bg, fg=fg, font=FONT, width=width_B, height=height_B).grid(column=a_x,row=a_y)
  131. head = tkinter.Entry(top, width=width_B * 2)
  132. head.grid(column=a_x + 1, row=a_y, columnspan=2, sticky=tkinter.E + tkinter.W)
  133. a_y += 1
  134. tkinter.Label(top, text='本地分支:', bg=bg, fg=fg, font=FONT, width=width_B, height=height_B).grid(column=a_x,row=a_y)
  135. BranchName = tkinter.Entry(top, width=width_B * 2)
  136. BranchName.grid(column=a_x + 1, row=a_y, columnspan=2, sticky=tkinter.E + tkinter.W)
  137. a_y += 1
  138. tkinter.Label(top, text='远程分支:', bg=bg, fg=fg, font=FONT, width=width_B, height=height_B).grid(column=a_x,row=a_y)
  139. BranchNOrigin = tkinter.Entry(top, width=width_B * 2)
  140. BranchNOrigin.grid(column=a_x + 1, row=a_y, columnspan=2, sticky=tkinter.E + tkinter.W)
  141. a_y += 1
  142. tkinter.Label(top, text='远程仓库链接:', bg=bg, fg=fg, font=FONT, width=width_B, height=height_B).grid(column=a_x,row=a_y)
  143. RemoteSSH = tkinter.Entry(top, width=width_B)
  144. RemoteSSH.grid(column=a_x + 1,columnspan=2, row=a_y, sticky=tkinter.E + tkinter.W)
  145. a_y += 1
  146. tkinter.Label(top, text='远程仓库名:', bg=bg, fg=fg, font=FONT, width=width_B, height=height_B).grid(column=a_x,row=a_y)
  147. RemoteName = tkinter.Entry(top, width=width_B)
  148. RemoteName.grid(column=a_x + 1, row=a_y, columnspan=2,sticky=tkinter.E + tkinter.W)
  149. a_y += 1
  150. tkinter.Label(top, text='commit:', bg=bg, fg=fg, font=FONT, width=width_B, height=height_B).grid(column=a_x,row=a_y)
  151. CommitName = tkinter.Entry(top, width=width_B)
  152. CommitName.grid(column=a_x + 1, row=a_y, columnspan=2, sticky=tkinter.E + tkinter.W)
  153. a_y += 1
  154. tkinter.Label(top, text='标签名字:', bg=bg, fg=fg, font=FONT, width=width_B, height=height_B).grid(column=a_x,row=a_y)
  155. TagName = tkinter.Entry(top, width=width_B)
  156. TagName.grid(column=a_x + 1,columnspan=2, row=a_y, sticky=tkinter.E + tkinter.W)
  157. a_y += 1
  158. tkinter.Label(top, text='查询关键字:', bg=bg, fg=fg, font=FONT, width=width_B, height=height_B).grid(column=a_x,row=a_y)
  159. Show_Key = tkinter.Entry(top, width=width_B)
  160. Show_Key.grid(column=a_x + 1,columnspan=2, row=a_y, sticky=tkinter.E + tkinter.W)
  161. a_y += 1
  162. tkinter.Label(top, text='工作区序号:', bg=bg, fg=fg, font=FONT, width=width_B, height=height_B).grid(column=a_x,row=a_y)
  163. StashName = tkinter.Entry(top, width=width_B)
  164. StashName.grid(column=a_x + 1, row=a_y,columnspan=2, sticky=tkinter.E + tkinter.W)
  165. a_y += 1
  166. tkinter.Button(top, bg=bbg, fg=fg, text='连接远程仓库', command=Add_remote, font=FONT, width=width_B,
  167. height=height_B).grid(column=a_x, row=a_y, sticky=tkinter.E + tkinter.W)
  168. tkinter.Button(top, bg=bbg, fg=fg, text='推送到远程仓库', command=lambda :Pull_Push_remote(1), font=FONT, width=width_B,
  169. height=height_B).grid(column=a_x+1, row=a_y, sticky=tkinter.E + tkinter.W)
  170. tkinter.Button(top, bg=bbg, fg=fg, text='从远程仓库抓取', command=lambda :Pull_Push_remote(0), font=FONT, width=width_B,
  171. height=height_B).grid(column=a_x+2, row=a_y, sticky=tkinter.E + tkinter.W)
  172. push_bind = tkinter.Variable()
  173. allow_history = tkinter.Variable()
  174. a_y += 1
  175. tkinter.Button(top, bg=bbg, fg=fg, text='分支绑定', command=Bind_remote, font=FONT, width=width_B,
  176. height=height_B).grid(column=a_x, row=a_y, sticky=tkinter.E + tkinter.W)
  177. tkinter.Checkbutton(top, bg=bg, fg=fg, activebackground=bg, activeforeground=fg, selectcolor=bg, text='无视历史记录',
  178. variable=allow_history).grid(column=a_x + 1, row=a_y, sticky=tkinter.W)
  179. tkinter.Checkbutton(top, bg=bg, fg=fg, activebackground=bg, activeforeground=fg, selectcolor=bg, text='推送时绑定',
  180. variable=push_bind).grid(column=a_x + 2, row=a_y, sticky=tkinter.W)
  181. allow_history.set(0)
  182. push_bind.set(0)
  183. a_y += 1
  184. tkinter.Button(top, bg=bbg, fg=fg, text='应用标签', command=add_tag, font=FONT, width=width_B,
  185. height=height_B).grid(column=a_x, row=a_y, sticky=tkinter.E + tkinter.W)
  186. tkinter.Button(top, bg=bbg, fg=fg, text='查看已有标签', command=lambda :show_tag(1), font=FONT, width=width_B,
  187. height=height_B).grid(column=a_x+1, row=a_y, sticky=tkinter.E + tkinter.W)
  188. tkinter.Button(top, bg=bbg, fg=fg, text='查询commit记录',command=lambda :show_tag(0), font=FONT, width=width_B,
  189. height=height_B).grid(column=a_x+2, row=a_y, sticky=tkinter.E + tkinter.W)
  190. a_y += 1
  191. tkinter.Button(top, bg=bbg, fg=fg, text='推送标签', command=push_tag, font=FONT, width=width_B,
  192. height=height_B).grid(column=a_x, row=a_y, sticky=tkinter.E + tkinter.W)
  193. tkinter.Button(top, bg=bbg, fg=fg, text='推送所有标签', command=push_alltag, font=FONT, width=width_B,
  194. height=height_B).grid(column=a_x+1, row=a_y, sticky=tkinter.E + tkinter.W)
  195. tkinter.Button(top, bg=bbg, fg=fg, text='删除本地标签',command=del_Tag, font=FONT, width=width_B,
  196. height=height_B).grid(column=a_x+2, row=a_y, sticky=tkinter.E + tkinter.W)
  197. a_y += 1
  198. tkinter.Button(top, bg=bbg, fg=fg, text='删除远程标签', command=del_Tag_remote, font=FONT, width=width_B,
  199. height=height_B).grid(column=a_x, row=a_y, sticky=tkinter.E + tkinter.W)
  200. tkinter.Button(top, bg=bbg, fg=fg, text='删除远程分支', command=del_Branch_remote, font=FONT, width=width_B,
  201. height=height_B).grid(column=a_x+1, row=a_y, sticky=tkinter.E + tkinter.W)
  202. tkinter.Button(top, bg=bbg, fg=fg, text='刷新远程分支', command=Fetch_remote, font=FONT, width=width_B,
  203. height=height_B).grid(column=a_x+2, row=a_y, sticky=tkinter.E + tkinter.W)
  204. a_y += 1
  205. tkinter.Button(top, bg=bbg, fg=fg, text='commit补丁', command=cherry_pick, font=FONT, width=width_B,
  206. height=height_B).grid(column=a_x, row=a_y, sticky=tkinter.E + tkinter.W)
  207. tkinter.Button(top, bg=bbg, fg=fg, text='删除远程仓库', command=Del_remote, font=FONT, width=width_B,
  208. height=height_B).grid(column=a_x+1, row=a_y, sticky=tkinter.E + tkinter.W)
  209. tkinter.Button(top, bg=bbg, fg=fg, text='工作区列表', command=lambda :not_Args(Git.Stash_List), font=FONT, width=width_B,
  210. height=height_B).grid(column=a_x+2, row=a_y, sticky=tkinter.E + tkinter.W)
  211. # global Customize_Input, th_do, wait_do
  212. # a_y += 1
  213. # th_do = tkinter.Variable()
  214. # wait_do = tkinter.Variable()
  215. # tkinter.Checkbutton(top, bg=bg, fg=fg, activebackground=bg, activeforeground=fg, selectcolor=bg, text='多进程刷新',
  216. # variable=th_do).grid(column=0, row=a_y, sticky=tkinter.W)
  217. # tkinter.Checkbutton(top, bg=bg, fg=fg, activebackground=bg, activeforeground=fg, selectcolor=bg, text='异步显示',
  218. # variable=wait_do).grid(column=1, row=a_y, sticky=tkinter.W)
  219. # Customize_Input = tkinter.Entry(top, width=width_B * 3)
  220. # Customize_Input.grid(column=2, row=a_y, columnspan=4, sticky=tkinter.E + tkinter.W + tkinter.N + tkinter.S)
  221. #
  222. # tkinter.Button(top, bg=bbg, fg=fg, text='执行操作', command=Customize, font=FONT, width=width_B,
  223. # height=height_B).grid(column=a_x+2, row=a_y, sticky=tkinter.E + tkinter.W)
  224. # th_do.set(0)
  225. # wait_do.set(1)
  226. TagMessage = commit_m
  227. TagCommit = CommitName
  228. RemoteBranch = BranchNOrigin
  229. LocalBranch = BranchName
  230. top.mainloop()
  231. def clone_git():#克隆仓库
  232. global clone_url
  233. new_Dic = askdirectory(title = '选择仓库地址')
  234. if new_Dic == '':return False
  235. name = Git.Clone_init(new_Dic)
  236. Clone(name, clone_url.get())
  237. Updata_GitBox()
  238. def Clone(name,url):
  239. do_Sys(Git.Clone, (name, url),
  240. break_time=0,text_n=f'{url}:正在执行克隆操作',th=True,wait=True)
  241. Git.After_Clone(name)
  242. update_Git_Dir()
  243. def Customize():
  244. global Git, Customize_Input, th_do, wait_do
  245. command = Customize_Input.get()
  246. do_Sys(Git.Customize, (get_Name(), command),
  247. break_time=0,text_n=f'{command}:操作进行中',th=bool(th_do.get()),wait=bool(wait_do.get()))
  248. update_Git_Dir()
  249. def Fetch_remote():
  250. global RemoteBranch, LocalBranch, Git, RemoteName
  251. Branch = RemoteBranch.get()
  252. Remote = RemoteName.get()
  253. Local = LocalBranch.get()
  254. do_Sys(Git.Fetch, (get_Name(), Local, Remote, Branch),
  255. break_time=0,text_n=f'此操作需要连接远程仓库,请稍等...',th=True,wait=True)
  256. update_Git_Dir()
  257. def del_Tag():
  258. global Git, RemoteName, TagName
  259. Tag = TagName.get()
  260. do_Sys(Git.del_tag, (get_Name(),Tag))
  261. update_Git_Dir()
  262. def del_Branch_remote():
  263. global Git, RemoteName, TagName
  264. Remoto = RemoteName.get()
  265. Remoto_Branch = RemoteBranch.get()
  266. do_Sys(Git.del_Branch_remote, (get_Name(),Remoto,Remoto_Branch),break_time=0,text_n=f'此操作需要连接远程仓库,请稍等...',
  267. th=True,wait=True)
  268. update_Git_Dir()
  269. def del_Tag_remote():
  270. global Git, RemoteName, TagName
  271. Remoto = RemoteName.get()
  272. Tag = TagName.get()
  273. do_Sys(Git.del_Tag_remote, (get_Name(),Remoto,Tag),break_time=0,text_n=f'此操作需要连接远程仓库,请稍等...',
  274. th=True,wait=True)
  275. update_Git_Dir()
  276. def push_alltag():
  277. global Git, RemoteName
  278. Remoto = RemoteName.get()
  279. do_Sys(Git.push_allTag, (get_Name(),Remoto),break_time=0,text_n=f'此操作需要连接远程仓库,请稍等...',
  280. th=True,wait=True)
  281. update_Git_Dir()
  282. def push_tag():
  283. global TagName, Git, RemoteName
  284. tag_name = TagName.get()
  285. Remoto = RemoteName.get()
  286. do_Sys(Git.push_Tag, (get_Name(), tag_name,Remoto),break_time=0,text_n=f'此操作需要连接远程仓库,请稍等...',
  287. th=True,wait=True)
  288. update_Git_Dir()
  289. def add_tag():
  290. global TagName, Git, commit, tag_message
  291. tag_name = TagName.get()
  292. commit = TagCommit.get()
  293. tag_message = TagMessage.get()
  294. do_Sys(Git.Add_Tag, (get_Name(), tag_name,commit,tag_message),show=False)
  295. update_Git_Dir()
  296. def show_tag(type_):
  297. global Show_Key, Git
  298. key = Show_Key.get()
  299. do_Sys({1:Git.Tag,0:Git.show_new}.get(type_,Git.show_new), (get_Name(), key))
  300. update_Git_Dir()
  301. def Pull_Push_remote(type_):
  302. global RemoteBranch, LocalBranch, Git, allow_history, RemoteName
  303. Branch = RemoteBranch.get()
  304. Remote = RemoteName.get()
  305. Local = LocalBranch.get()
  306. allow = bool(allow_history.get())
  307. u = bool(push_bind.get())
  308. do_Sys({0:Git.Pull_remote,1:Git.Push_remote}.get(type_,Git.Pull_remote), (get_Name(), Local, Remote, Branch, allow, u),
  309. break_time=0,text_n=f'此操作需要连接远程仓库,请稍等...',th=True,wait=True)
  310. update_Git_Dir()
  311. def Bind_remote():
  312. global RemoteBranch, LocalBranch, Git
  313. Remote = RemoteBranch.get()
  314. Local = LocalBranch.get()
  315. do_Sys(Git.Bind_remote, (get_Name(), Local, Remote))
  316. update_Git_Dir()
  317. def Del_remote():
  318. global RemoteSSH, RemoteName, Git
  319. name = RemoteName.get()
  320. do_Sys(Git.Del_remote, (get_Name(), name))
  321. update_Git_Dir()
  322. def Add_remote():
  323. global RemoteSSH, RemoteName, Git
  324. SSH = RemoteSSH.get()
  325. name = RemoteName.get()
  326. do_Sys(Git.Add_remote, (get_Name(), SSH, name))
  327. update_Git_Dir()
  328. def cherry_pick():
  329. global CommitName, Git
  330. commit = CommitName.get()
  331. do_Sys(Git.cherry_pick, (get_Name(), commit))
  332. update_Git_Dir()
  333. def Open_Stash(type_):
  334. global StashName, Git
  335. stash_num = StashName.get()
  336. if stash_num == '':stash_num = '0'
  337. do_Sys([Git.Drop_stash,Git.Apply_stash][type_], (get_Name(), stash_num))
  338. update_Git_Dir()
  339. def merge_Branch():
  340. global BranchName, Git, no_ff,commit_m
  341. m = commit_m.get()
  342. no = not bool(no_ff.get()) # 对于no_ff来说,True - 使用快速合并,所以要翻转
  343. if m.replace(' ','') == '' and no:
  344. tkinter.messagebox.showinfo('警告!', '非常遗憾,我不同意你commit而不添加任何描述!\n描述是很重要的!'
  345. '(如果你不想添加描述,请使用快速合并,但我并不建议!)')
  346. return False
  347. name = BranchName.get()
  348. do_Sys(Git.merge_Branch, (get_Name(), name, no, m))
  349. update_Git_Dir()
  350. def Delete_Branch(type_):
  351. global BranchName, Git
  352. name = BranchName.get()
  353. do_Sys(Git.Del_Branch, (get_Name(), name, type_))
  354. update_Git_Dir()
  355. def switch_Branch():
  356. global BranchName,Git
  357. name = BranchName.get()
  358. do_Sys(Git.switch_Branch, (get_Name(), name),break_time=1,show=False)
  359. update_Git_Dir()
  360. def make_Branch():
  361. global BranchName,Git,BranchNOrigin
  362. name = BranchName.get()
  363. origin = BranchNOrigin.get()
  364. do_Sys(Git.new_Branch, (get_Name(), name, origin),break_time=1,show=False)
  365. update_Git_Dir()
  366. def check_Branch():
  367. global Git,head
  368. dic = askopenfilenames(title=f'选择要删除的文件(取消为全选)')
  369. if dic == '': return False
  370. do_Sys(Git.rm,(get_Name(),dic))
  371. update_Git_Dir()
  372. def rm_file():
  373. global Git,head
  374. dic = askopenfilenames(title=f'选择要删除的文件(取消为全选)')
  375. if dic == '': return False
  376. do_Sys(Git.rm,(get_Name(),dic))
  377. update_Git_Dir()
  378. def Back_File():
  379. global Git,head
  380. dic = askopenfilenames(title=f'选择要add的文件(取消为全选)')
  381. if dic == '': return False
  382. do_Sys(Git.checkout_version,(get_Name(),dic))
  383. update_Git_Dir()
  384. def Back_version():
  385. global Git,head
  386. HEAD = head.get()
  387. if HEAD == '': HEAD = 'HEAD~1'
  388. do_Sys(Git.back_version,(get_Name(),HEAD))
  389. update_Git_Dir()
  390. def do_Sys(func,args,name='CoTan Git',break_time=0,show=True,text_n='',th=False,wait=False,stop=True):
  391. p = func(*args)
  392. flat = True
  393. stopKey = Git_Ctrl.stopKey
  394. def Out_Txt():
  395. nonlocal data
  396. dic = asksaveasfilename(title='选择文件保存位置',filetypes=[("TXT", ".txt")])
  397. try:
  398. if dic == '':return False
  399. if dic[-4] == '.txt':pass
  400. else:raise Exception
  401. except:
  402. dic += '.txt'
  403. with open(dic,'w',encoding='utf-8') as f:
  404. f.write(data)
  405. kb = True
  406. sb = True
  407. def update_b():
  408. if not kb:
  409. b_list[1].config(state=tkinter.DISABLED)
  410. if not sb:
  411. b_list[0].config(state=tkinter.DISABLED)
  412. def Stop():
  413. nonlocal start,kb,sb
  414. start = 0
  415. sb = False
  416. kb = False
  417. update_b()
  418. def keep():
  419. nonlocal start,kb
  420. start = float('inf')
  421. kb = False
  422. update_b()
  423. def pipe():pass
  424. def not_out():
  425. nonlocal text,out_data,data,flat
  426. text.clear()
  427. if flat:
  428. text.insert(tkinter.END,data)
  429. else:
  430. text.insert(tkinter.END, out_data)
  431. flat = not flat
  432. start = time.time()
  433. data = ''
  434. out_data = ''#包含out的data
  435. if show:
  436. text, new_top, b_list = show_Now(Out_Txt,Stop,keep,not_out,pipe,name=name)#[close,keep]
  437. update_b()
  438. if text_n != '':
  439. text.insert('0.0',f'载入前提示>>> {text_n}\n')
  440. out_data += f'载入前提示>>> {text_n}\n'
  441. data += f'{text_n}\n'
  442. new_top.update()
  443. top.update()
  444. def Update():
  445. nonlocal start
  446. while True:
  447. try:
  448. top.update()
  449. if show:
  450. try:
  451. new_top.update()
  452. except:pass
  453. if time.time() - start >= break_time and break_time != 0:
  454. raise Exception
  455. elif break_time == 0 and start == 0:
  456. raise Exception
  457. except:
  458. start = 0
  459. break
  460. if th or not wait:
  461. j = threading.Thread(target=Update)#如果没有启动到多进程的效果,请检查Update是不是加了(),这里需要回调
  462. j.start()
  463. if wait:#等待后显示
  464. if break_time == 0:break_ti = None# 此处break_ti是为了别面覆盖break_time,因为Update进程需要用
  465. else:break_ti = break_time
  466. def wait_p():
  467. nonlocal start
  468. p.wait(break_ti)
  469. start = 0
  470. j = threading.Thread(target=wait_p) # 这么做不是多此一举,如果没有wait,进程并不会退出
  471. j.start()
  472. Update()#遇到sleep等主线程阻塞,top.update等会阻塞子线程,因此,必须保证主线程不会被wait所阻塞
  473. out = p.stdout.read().split('\n')
  474. for i in out:
  475. if show:
  476. try: # 如果界面被关掉了,会报错
  477. new_top.title(f'{name} : 运行中')
  478. except:
  479. text, new_top, b_list = show_Now(Out_Txt, Stop, keep, not_out, pipe,
  480. name=f'{name} : 运行中')
  481. update_b()
  482. text.insert(tkinter.END, out_data)
  483. if stop and i.replace(' ', '').replace('\n', '') != stopKey:
  484. text.insert(tkinter.END, f'[out]> {i}\n')
  485. data += i + '\n'
  486. out_data += f'[out]> {i}\n'
  487. if show:
  488. text.insert(tkinter.END, '[END]')
  489. out_data += f'[END]'
  490. data += f'[END]'
  491. start = 0
  492. else:#即时显示
  493. while True:
  494. #界面设置
  495. try:#如果界面被关掉了,会报错
  496. if show: new_top.title(f'{name} : 运行中')
  497. except:
  498. text, new_top, b_list = show_Now(Out_Txt, Stop, keep, not_out, pipe,
  499. name=f'{name} : 运行中')
  500. update_b()
  501. text.insert(tkinter.END,out_data)
  502. #界面刷新
  503. try:
  504. if not th:
  505. top.update()
  506. if show: new_top.update()
  507. except:
  508. break
  509. #输出字符
  510. try:
  511. i = p.stdout.readline()#.decode(str_code)#不需要decode,因为Popen已经设置了universal_newlines=True
  512. bool_text = i.replace(' ','').replace('\n','')
  513. if bool_text != '':
  514. if stop and bool_text == stopKey:
  515. start = 0
  516. else:
  517. if show: text.insert(tkinter.END, f'[out]> {i}')
  518. data += i
  519. out_data += f'[out]> {i}'
  520. if p.returncode == 0 or (time.time() - start >= break_time and break_time != 0) or (break_time == 0 and start == 0):
  521. if show:
  522. text.insert(tkinter.END,'[END]')
  523. out_data += f'[END]'
  524. data += f'[END]'
  525. break
  526. elif p.returncode != None:
  527. raise Exception
  528. except:
  529. try:
  530. if show:
  531. text.insert(tkinter.END, '[ERROR]')
  532. out_data += f'[ERROR]'
  533. data += f'[ERROR]'
  534. raise Exception
  535. except:break
  536. try: # 如果界面被关掉了,会报错
  537. if show: new_top.title(f'{name} : 运行完毕')
  538. except:pass
  539. p.kill()
  540. try:
  541. if show:
  542. b_list[0].config(state=tkinter.DISABLED)
  543. b_list[1].config(state=tkinter.DISABLED)
  544. except:pass
  545. return data
  546. def log():
  547. global Git,log_Type
  548. name = get_Name()
  549. graph = bool(log_Type[0].get())
  550. abbrev = bool(log_Type[1].get())
  551. pretty = bool(log_Type[2].get())
  552. do_Sys(Git.log,(name,graph,pretty,abbrev))
  553. update_Git_Dir()
  554. def not_Args(func):
  555. global Git
  556. name = get_Name()
  557. do_Sys(func,(name,))
  558. update_Git_Dir()
  559. def Commit_File():
  560. global Git,commit_m
  561. m = commit_m.get()
  562. if m.replace(' ','') == '':
  563. tkinter.messagebox.showinfo('警告!', '非常遗憾,我不同意你commit而不添加任何描述!\n描述是很重要的!')
  564. return False
  565. name = get_Name()
  566. do_Sys(Git.commit_File,(name,m))
  567. update_Git_Dir()
  568. def Diff_File():
  569. global Git,master
  570. MASTER = master.get()
  571. if MASTER == '':MASTER = 'HEAD'
  572. do_Sys(Git.diff_File,(get_Name(),MASTER))
  573. update_Git_Dir()
  574. def Reset_File():
  575. global Git,Last_Name
  576. dic = askopenfilenames(title=f'选择要撤销add的文件(取消为全选)')
  577. if dic == '':dic = '.'
  578. do_Sys(Git.reset_File,(get_Name(),dic))
  579. update_Git_Dir()
  580. def Add_File():
  581. global Git,Last_Name
  582. dic = askopenfilenames(title=f'选择要add的文件(取消为全选)')
  583. if dic == '':dic = '.'
  584. do_Sys(Git.add_File,(get_Name(),dic))
  585. update_Git_Dir()
  586. def update_Git_Dir():
  587. global Last_Name
  588. if Last_Name == None:return False
  589. Git_dir(Last_Name)
  590. def get_Git_Dir():
  591. name = get_Name()
  592. Git_dir(name)
  593. def Git_dir(name):
  594. global Git, Git_Dir,Last_Name
  595. dir_list = Git.get_Dir(name)
  596. try:#窗口可能已经关闭
  597. Git_Dir.delete(0,tkinter.END)
  598. Git_Dir.insert(tkinter.END,*dir_list)
  599. except:
  600. pass
  601. Last_Name = name
  602. def init_git():#创建仓库
  603. global Git
  604. new_Dic = askdirectory(title = '选择仓库地址')
  605. if new_Dic == '':return False
  606. Git.Add_init(new_Dic,)
  607. Updata_GitBox()
  608. def get_Name(): # 获得名字统一接口
  609. global Git,Git_List,Git_Box
  610. try:
  611. return Git_List[Git_Box.curselection()[0]]
  612. except:
  613. try:
  614. return Git_List[0]
  615. except:
  616. return None
  617. def Updata_GitBox():
  618. global Git,Git_List,Git_Box
  619. Git_List = list(Git.get_git_Dic().keys())
  620. Git_Box.delete(0,tkinter.END)
  621. Git_Box.insert(tkinter.END,*Git_List)
  622. def show(data='',name='CoTan_Git'):
  623. global bg, ft1
  624. new_top = tkinter.Toplevel(bg=bg)
  625. new_top.title(name)
  626. new_top.geometry('+10+10') # 设置所在位置
  627. text = ScrolledText(new_top, font=('黑体', 11), height=50)
  628. text.pack(fill=tkinter.BOTH)
  629. text.insert('0.0', data)
  630. text.config(state=tkinter.DISABLED)
  631. new_top.resizable(width=False, height=False)
  632. def show_Now(out_func,close_func,keepFunc,not_out,pipeFunc,name='CoTan_Git >>> 高级命令行'):
  633. global bg
  634. new_top = tkinter.Toplevel(bg=bg)
  635. new_top.title(name)
  636. new_top.geometry('+10+10') # 设置所在位置
  637. new_top.resizable(width=False, height=False)
  638. class ScrolledText_new(ScrolledText):
  639. def __init__(self,*args,**kwargs):
  640. super(ScrolledText_new, self).__init__(*args,**kwargs)
  641. def insert(self, index, chars, *args):
  642. text.config(state=tkinter.NORMAL)
  643. super(ScrolledText_new, self).insert(index, chars, *args)
  644. text.config(state=tkinter.DISABLED)
  645. def clear(self):
  646. text.config(state=tkinter.NORMAL)
  647. self.delete('0.0',tkinter.END)
  648. text.config(state=tkinter.DISABLED)
  649. text = ScrolledText_new(new_top, font=('黑体', 11), height=30,width=100)
  650. text.grid(column=0, row=0,columnspan=5, sticky=tkinter.E + tkinter.W)
  651. text.config(state=tkinter.DISABLED)
  652. tkinter.Button(new_top, bg=bg, fg=fg, text='输出文档', font=('黑体', 11),width=20, height=2, command=out_func).grid(
  653. column=4, row=1, sticky=tkinter.E + tkinter.W)
  654. close = tkinter.Button(new_top, bg=bg, fg=fg, text='关闭子线程连接', font=('黑体', 11),width=20, height=2, command=close_func)
  655. close.grid(column=0, row=1, sticky=tkinter.E + tkinter.W)
  656. keep = tkinter.Button(new_top, bg=bg, fg=fg, text='保持线程连接', font=('黑体', 11),width=20, height=2, command=keepFunc)
  657. keep.grid(column=1, row=1, sticky=tkinter.E + tkinter.W)
  658. tkinter.Button(new_top, bg=bg, fg=fg, text='格式化输出', font=('黑体', 11),width=20, height=2, command=not_out).grid(
  659. column=2, row=1, sticky=tkinter.E + tkinter.W)
  660. tkinter.Button(new_top, bg=bg, fg=fg, text='文件管道输入', font=('黑体', 11),width=20, height=2, command=pipeFunc).grid(
  661. column=3, row=1, sticky=tkinter.E + tkinter.W)
  662. return text,new_top,[close,keep]
  663. if __name__ == '__main__':
  664. Main()