1
0

GIT.py 37 KB

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