GIT.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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
  31. tkinter.Label(top, text='克隆URL:', bg=bg, fg=fg, font=FONT, width=width_B, height=height_B).grid(column=a_x,row=a_y)
  32. Dic_Var = tkinter.StringVar()#当前的Dic
  33. clone_url = tkinter.Entry(top, width=width_B * 2, textvariable=Dic_Var)
  34. clone_url.grid(column=a_x+1, row=a_y, columnspan=2, sticky=tkinter.E + tkinter.W)
  35. a_y += 1
  36. tkinter.Button(top, bg=bbg, fg=fg, text='克隆仓库', command=clone_git, font=FONT, width=width_B,
  37. height=height_B).grid(column=a_x, row=a_y, sticky=tkinter.E + tkinter.W)
  38. tkinter.Button(top, bg=bbg, fg=fg, text='打开仓库', command=init_git, font=FONT, width=width_B,
  39. height=height_B).grid(column=a_x+1, row=a_y, sticky=tkinter.E + tkinter.W)
  40. tkinter.Button(top, bg=bbg, fg=fg, text='查看文件', command=get_Git_Dir, font=FONT, width=width_B,
  41. height=height_B).grid(column=a_x+2, row=a_y, sticky=tkinter.E + tkinter.W)
  42. global Git_Box,Git_Dir
  43. a_y += 1
  44. Git_Box = tkinter.Listbox(top, width=width_B * 3, height=height_B * 4)
  45. Git_Box.grid(column=a_x, row=a_y, columnspan=3, rowspan=4, sticky=tkinter.E + tkinter.W + tkinter.S + tkinter.N)
  46. a_y += 4
  47. tkinter.Label(top, text='【仓库文件列表】', bg=bg, fg=fg, font=FONT, width=width_B * 3, height=height_B).grid(
  48. column=a_x,columnspan=3,row=a_y,sticky=tkinter.E + tkinter.W + tkinter.W + tkinter.S + tkinter.N) # 设置说明
  49. a_y += 1
  50. Git_Dir = tkinter.Listbox(top, width=width_B * 3, height=height_B * 4)
  51. Git_Dir.grid(column=a_x, row=a_y, columnspan=3, rowspan=4, sticky=tkinter.E + tkinter.W + tkinter.S + tkinter.N)
  52. a_y += 4
  53. tkinter.Button(top, bg=bbg, fg=fg, text='添加暂存区文件', command=Add_File, font=FONT, width=width_B,
  54. height=height_B).grid(column=a_x, row=a_y, sticky=tkinter.E + tkinter.W)
  55. tkinter.Button(top, bg=bbg, fg=fg, text='移除暂存区文件', command=init_git, font=FONT, width=width_B,
  56. height=height_B).grid(column=a_x+1, row=a_y, sticky=tkinter.E + tkinter.W)
  57. tkinter.Button(top, bg=bbg, fg=fg, text='提交到git', command=Commit_File, font=FONT, width=width_B,
  58. height=height_B).grid(column=a_x+2, row=a_y, sticky=tkinter.E + tkinter.W)
  59. global commit_m,head,master,no_ff
  60. a_y += 1
  61. tkinter.Label(top, text='提交描述:', bg=bg, fg=fg, font=FONT, width=width_B, height=height_B).grid(column=a_x,row=a_y)
  62. commit_m = tkinter.Entry(top, width=width_B * 2)
  63. commit_m.grid(column=a_x + 1, row=a_y, columnspan=2, sticky=tkinter.E + tkinter.W)
  64. a_y += 1
  65. tkinter.Button(top, bg=bbg, fg=fg, text='查看执行日志', command=lambda :not_Args(Git.reflog), 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=lambda :not_Args(Git.log), 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=lambda :not_Args(Git.status), 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=Back_version, font=FONT, width=width_B,
  73. height=height_B).grid(column=a_x, row=a_y, sticky=tkinter.E + tkinter.W)
  74. tkinter.Button(top, bg=bbg, fg=fg, text='文件回退', command=Back_File, font=FONT, width=width_B,
  75. height=height_B).grid(column=a_x+1, row=a_y, sticky=tkinter.E + tkinter.W)
  76. tkinter.Button(top, bg=bbg, fg=fg, text='删除文件', command=rm_file, font=FONT, width=width_B,
  77. height=height_B).grid(column=a_x+2, row=a_y, sticky=tkinter.E + tkinter.W)
  78. a_y += 1
  79. tkinter.Label(top, text='diff分支:', bg=bg, fg=fg, font=FONT, width=width_B, height=height_B).grid(column=a_x,row=a_y)
  80. master = tkinter.Entry(top, width=width_B * 2)
  81. master.grid(column=a_x + 1, row=a_y, columnspan=2, sticky=tkinter.E + tkinter.W)
  82. a_y += 1
  83. tkinter.Label(top, text='回退版本号:', bg=bg, fg=fg, font=FONT, width=width_B, height=height_B).grid(column=a_x,row=a_y)
  84. head = tkinter.Entry(top, width=width_B * 2)
  85. head.grid(column=a_x + 1, row=a_y, columnspan=2, sticky=tkinter.E + tkinter.W)
  86. a_y += 1
  87. tkinter.Button(top, bg=bbg, fg=fg, text='查看分支', command=lambda :not_Args(Git.check_Branch), 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=make_Branch, 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=switch_Branch, font=FONT, width=width_B,
  92. height=height_B).grid(column=a_x+2, row=a_y, sticky=tkinter.E + tkinter.W)
  93. a_y += 1
  94. tkinter.Button(top, bg=bbg, fg=fg, text='删除分支', command=lambda :Delete_Branch(1), font=FONT, width=width_B,
  95. height=height_B).grid(column=a_x, row=a_y, sticky=tkinter.E + tkinter.W)
  96. tkinter.Button(top, bg=bbg, fg=fg, text='丢弃分支', command=lambda :Delete_Branch(0), font=FONT, width=width_B,
  97. height=height_B).grid(column=a_x+1, row=a_y, sticky=tkinter.E + tkinter.W)
  98. tkinter.Button(top, bg=bbg, fg=fg, text='合并分支', command=switch_Branch, font=FONT, width=width_B,
  99. height=height_B).grid(column=a_x+2, row=a_y, sticky=tkinter.E + tkinter.W)
  100. no_ff = tkinter.Variable()
  101. a_y += 1
  102. tkinter.Button(top, bg=bbg, fg=fg, text='合并分支', command=merge_Branch, font=FONT, width=width_B,
  103. height=height_B).grid(column=a_x, row=a_y, sticky=tkinter.E + tkinter.W)
  104. tkinter.Button(top, bg=bbg, fg=fg, text='退出冲突处理', command=lambda :not_Args(Git.merge_abort), font=FONT, width=width_B,
  105. height=height_B).grid(column=a_x+2, row=a_y, sticky=tkinter.E + tkinter.W)
  106. tkinter.Checkbutton(top, bg=bg, fg=fg, activebackground=bg, activeforeground=fg, selectcolor=bg, text='使用快速合并',
  107. variable=no_ff).grid(column=a_x + 1, row=a_y, sticky=tkinter.W)
  108. no_ff.set(0)
  109. global BranchName,StashName,CommitName,BranchNOrigin
  110. a_y += 1
  111. tkinter.Label(top, text='分支名字:', bg=bg, fg=fg, font=FONT, width=width_B, height=height_B).grid(column=a_x,row=a_y)
  112. BranchName = tkinter.Entry(top, width=width_B * 2)
  113. BranchName.grid(column=a_x + 1, row=a_y, columnspan=2, sticky=tkinter.E + tkinter.W)
  114. a_y += 1
  115. tkinter.Label(top, text='关联远程分支:', bg=bg, fg=fg, font=FONT, width=width_B, height=height_B).grid(column=a_x,row=a_y)
  116. BranchNOrigin = tkinter.Entry(top, width=width_B * 2)
  117. BranchNOrigin.grid(column=a_x + 1, row=a_y, columnspan=2, sticky=tkinter.E + tkinter.W)
  118. a_y += 1
  119. tkinter.Button(top, bg=bbg, fg=fg, text='保存工作区', command=lambda :not_Args(Git.Save_stash), font=FONT, width=width_B,
  120. height=height_B).grid(column=a_x, row=a_y, sticky=tkinter.E + tkinter.W)
  121. tkinter.Button(top, bg=bbg, fg=fg, text='恢复工作区', command=lambda :Open_Stash(1), font=FONT, width=width_B,
  122. height=height_B).grid(column=a_x+1, row=a_y, sticky=tkinter.E + tkinter.W)
  123. tkinter.Button(top, bg=bbg, fg=fg, text='删除工作区', command=lambda :Open_Stash(0), font=FONT, width=width_B,
  124. height=height_B).grid(column=a_x+2, row=a_y, sticky=tkinter.E + tkinter.W)
  125. a_y += 1
  126. tkinter.Label(top, text='工作区序号:', bg=bg, fg=fg, font=FONT, width=width_B, height=height_B).grid(column=a_x,row=a_y)
  127. StashName = tkinter.Entry(top, width=width_B)
  128. StashName.grid(column=a_x + 1, row=a_y, sticky=tkinter.E + tkinter.W)
  129. tkinter.Button(top, bg=bbg, fg=fg, text='工作区列表', command=lambda :not_Args(Git.Stash_List), font=FONT, width=width_B,
  130. height=height_B).grid(column=a_x+2, row=a_y, sticky=tkinter.E + tkinter.W)
  131. a_y += 1
  132. tkinter.Label(top, text='commit:', bg=bg, fg=fg, font=FONT, width=width_B, height=height_B).grid(column=a_x,row=a_y)
  133. CommitName = tkinter.Entry(top, width=width_B)
  134. CommitName.grid(column=a_x + 1, row=a_y, sticky=tkinter.E + tkinter.W)
  135. tkinter.Button(top, bg=bbg, fg=fg, text='复制commit', command=cherry_pick, font=FONT, width=width_B,
  136. height=height_B).grid(column=a_x+2, row=a_y, sticky=tkinter.E + tkinter.W)
  137. a_x += 3
  138. tkinter.Label(top, text='', bg=bg, fg=fg, font=FONT, width=1).grid(column=a_x, row=a_y) # 设置说明
  139. a_x += 1
  140. a_y = 0
  141. tkinter.Label(top, text='【远程仓库】', bg=bg, fg=fg, font=FONT, width=width_B * 3, height=height_B).grid(
  142. column=a_x,columnspan=3,row=a_y,sticky=tkinter.E + tkinter.W + tkinter.W + tkinter.S + tkinter.N) # 设置说明
  143. a_y += 1
  144. tkinter.Button(top, bg=bbg, fg=fg, text='连接远程仓库', command=Add_remote, font=FONT, width=width_B,
  145. height=height_B).grid(column=a_x, row=a_y, sticky=tkinter.E + tkinter.W)
  146. tkinter.Button(top, bg=bbg, fg=fg, text='推送到远程仓库', command=lambda :Pull_Push_remote(1), font=FONT, width=width_B,
  147. height=height_B).grid(column=a_x+1, row=a_y, sticky=tkinter.E + tkinter.W)
  148. tkinter.Button(top, bg=bbg, fg=fg, text='从远程仓库抓取', command=lambda :Pull_Push_remote(0), font=FONT, width=width_B,
  149. height=height_B).grid(column=a_x+2, row=a_y, sticky=tkinter.E + tkinter.W)
  150. global RemoteSSH,RemoteName,RemoteBranch,LocalBranch,push_bind,allow_history
  151. a_y += 1
  152. tkinter.Label(top, text='远程仓库链接:', bg=bg, fg=fg, font=FONT, width=width_B, height=height_B).grid(column=a_x,row=a_y)
  153. RemoteSSH = tkinter.Entry(top, width=width_B)
  154. RemoteSSH.grid(column=a_x + 1,columnspan=2, row=a_y, sticky=tkinter.E + tkinter.W)
  155. a_y += 1
  156. tkinter.Label(top, text='远程仓库名:', bg=bg, fg=fg, font=FONT, width=width_B, height=height_B).grid(column=a_x,row=a_y)
  157. RemoteName = tkinter.Entry(top, width=width_B)
  158. RemoteName.grid(column=a_x + 1, row=a_y, sticky=tkinter.E + tkinter.W)
  159. tkinter.Button(top, bg=bbg, fg=fg, text='删除关联', command=cherry_pick, font=FONT, width=width_B,
  160. height=height_B).grid(column=a_x+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. RemoteBranch = tkinter.Entry(top, width=width_B)
  164. RemoteBranch.grid(column=a_x + 1,columnspan=2, row=a_y, sticky=tkinter.E + tkinter.W)
  165. a_y += 1
  166. tkinter.Label(top, text='本地分支:', bg=bg, fg=fg, font=FONT, width=width_B, height=height_B).grid(column=a_x,row=a_y)
  167. LocalBranch = tkinter.Entry(top, width=width_B)
  168. LocalBranch.grid(column=a_x + 1,columnspan=2, row=a_y, sticky=tkinter.E + tkinter.W)
  169. push_bind = tkinter.Variable()
  170. allow_history = tkinter.Variable()
  171. a_y += 1
  172. tkinter.Button(top, bg=bbg, fg=fg, text='分支绑定', command=Bind_remote, font=FONT, width=width_B,
  173. height=height_B).grid(column=a_x, row=a_y, sticky=tkinter.E + tkinter.W)
  174. tkinter.Checkbutton(top, bg=bg, fg=fg, activebackground=bg, activeforeground=fg, selectcolor=bg, text='无视历史记录',
  175. variable=allow_history).grid(column=a_x + 1, row=a_y, sticky=tkinter.W)
  176. tkinter.Checkbutton(top, bg=bg, fg=fg, activebackground=bg, activeforeground=fg, selectcolor=bg, text='推送时绑定',
  177. variable=push_bind).grid(column=a_x + 2, row=a_y, sticky=tkinter.W)
  178. allow_history.set(0)
  179. push_bind.set(0)
  180. tkinter.Label(top, text='【标签管理】', bg=bg, fg=fg, font=FONT, width=width_B * 3, height=height_B).grid(
  181. column=a_x,columnspan=3,row=a_y,sticky=tkinter.E + tkinter.W + tkinter.W + tkinter.S + tkinter.N) # 设置说明
  182. global TagName,TagMessage,TagCommit,Show_Key
  183. a_y += 1
  184. tkinter.Label(top, text='标签名字:', bg=bg, fg=fg, font=FONT, width=width_B, height=height_B).grid(column=a_x,row=a_y)
  185. TagName = tkinter.Entry(top, width=width_B)
  186. TagName.grid(column=a_x + 1,columnspan=2, row=a_y, sticky=tkinter.E + tkinter.W)
  187. a_y += 1
  188. tkinter.Label(top, text='标签描述:', bg=bg, fg=fg, font=FONT, width=width_B, height=height_B).grid(column=a_x,row=a_y)
  189. TagMessage = tkinter.Entry(top, width=width_B)
  190. TagMessage.grid(column=a_x + 1,columnspan=2, row=a_y, sticky=tkinter.E + tkinter.W)
  191. a_y += 1
  192. tkinter.Label(top, text='commit记录:', bg=bg, fg=fg, font=FONT, width=width_B, height=height_B).grid(column=a_x,row=a_y)
  193. TagCommit = tkinter.Entry(top, width=width_B)
  194. TagCommit.grid(column=a_x + 1,columnspan=2, row=a_y, sticky=tkinter.E + tkinter.W)
  195. a_y += 1
  196. tkinter.Label(top, text='查询关键字:',command=lambda :show_tag(0), bg=bg, fg=fg, font=FONT, width=width_B, height=height_B).grid(column=a_x,row=a_y)
  197. Show_Key = tkinter.Entry(top, width=width_B)
  198. Show_Key.grid(column=a_x + 1,columnspan=2, row=a_y, sticky=tkinter.E + tkinter.W)
  199. a_y += 1
  200. tkinter.Button(top, bg=bbg, fg=fg, text='应用标签', command=Bind_remote, font=FONT, width=width_B,
  201. height=height_B).grid(column=a_x, row=a_y, sticky=tkinter.E + tkinter.W)
  202. tkinter.Button(top, bg=bbg, fg=fg, text='查看已有标签', command=lambda :show_tag(0), font=FONT, width=width_B,
  203. height=height_B).grid(column=a_x+1, row=a_y, sticky=tkinter.E + tkinter.W)
  204. tkinter.Button(top, bg=bbg, fg=fg, text='查询数据', command=Bind_remote, font=FONT, width=width_B,
  205. height=height_B).grid(column=a_x+2, row=a_y, sticky=tkinter.E + tkinter.W)
  206. top.mainloop()
  207. def add_tag():
  208. global TagName, Git
  209. tag_name = TagName.get()
  210. commit = TagCommit.get()
  211. tag_message = TagMessage.get()
  212. do_Sys(Git.Add_Tag, (get_Name(), tag_name,commit,tag_message))
  213. update_Git_Dir()
  214. def show_tag(type_):
  215. global Show_Key, Git
  216. key = Show_Key.get()
  217. do_Sys({1:Git.Tag,0:Git.show_new}.get(type_,Git.show_new), (get_Name(), key))
  218. update_Git_Dir()
  219. def Pull_Push_remote(type_):
  220. global RemoteBranch, LocalBranch, Git, allow_history
  221. Remote = RemoteBranch.get()
  222. Local = LocalBranch.get()
  223. allow = bool(allow_history.get())
  224. u = bool(push_bind.get())
  225. do_Sys({0:Git.Pull_remote,1:Git.Push_remote}.get(type_,Git.Pull_remote), (get_Name(), Local, Remote, allow, u),
  226. break_time=10,text_n=f'此操作需要连接远程仓库,请稍等...',th=True)
  227. update_Git_Dir()
  228. def Bind_remote():
  229. global RemoteBranch, LocalBranch, Git
  230. Remote = RemoteBranch.get()
  231. Local = LocalBranch.get()
  232. do_Sys(Git.Bind_remote, (get_Name(), Local, Remote))
  233. update_Git_Dir()
  234. def Add_remote():
  235. global RemoteSSH, RemoteName, Git
  236. SSH = RemoteSSH.get()
  237. name = RemoteName.get()
  238. do_Sys(Git.Add_remote, (get_Name(), SSH, name))
  239. update_Git_Dir()
  240. def cherry_pick():
  241. global CommitName, Git
  242. commit = CommitName.get()
  243. do_Sys(Git.cherry_pick, (get_Name(), commit))
  244. update_Git_Dir()
  245. def Open_Stash(type_):
  246. global StashName, Git
  247. stash_num = StashName.get()
  248. if stash_num == '':stash_num = '0'
  249. do_Sys([Git.Drop_stash,Git.Apply_stash][type_], (get_Name(), stash_num))
  250. update_Git_Dir()
  251. def merge_Branch():
  252. global BranchName, Git, no_ff,commit_m
  253. m = commit_m.get()
  254. no = not bool(no_ff.get()) # 对于no_ff来说,True - 使用快速合并,所以要翻转
  255. if m.replace(' ','') == '' and no:
  256. tkinter.messagebox.showinfo('警告!', '非常遗憾,我不同意你commit而不添加任何描述!\n描述是很重要的!'
  257. '(如果你不想添加描述,请使用快速合并,但我并不建议!)')
  258. return False
  259. name = BranchName.get()
  260. do_Sys(Git.merge_Branch, (get_Name(), name, no, m))
  261. update_Git_Dir()
  262. def Delete_Branch(type_):
  263. global BranchName, Git
  264. name = BranchName.get()
  265. do_Sys(Git.Del_Branch, (get_Name(), name, type_))
  266. update_Git_Dir()
  267. def switch_Branch():
  268. global BranchName,Git
  269. name = BranchName.get()
  270. do_Sys(Git.switch_Branch, (get_Name(), name),break_time=1,show=False)
  271. update_Git_Dir()
  272. def make_Branch():
  273. global BranchName,Git,BranchNOrigin
  274. name = BranchName.get()
  275. origin = BranchNOrigin.get()
  276. do_Sys(Git.new_Branch, (get_Name(), name, origin),break_time=1,show=False)
  277. update_Git_Dir()
  278. def check_Branch():
  279. global Git,head
  280. dic = askopenfilenames(title=f'选择要删除的文件(取消为全选)')
  281. if dic == '': return False
  282. do_Sys(Git.rm,(get_Name(),dic))
  283. update_Git_Dir()
  284. def rm_file():
  285. global Git,head
  286. dic = askopenfilenames(title=f'选择要删除的文件(取消为全选)')
  287. if dic == '': return False
  288. do_Sys(Git.rm,(get_Name(),dic))
  289. update_Git_Dir()
  290. def Back_File():
  291. global Git,head
  292. dic = askopenfilenames(title=f'选择要add的文件(取消为全选)')
  293. if dic == '': return False
  294. do_Sys(Git.checkout_version,(get_Name(),dic))
  295. update_Git_Dir()
  296. def Back_version():
  297. global Git,head
  298. HEAD = head.get()
  299. if HEAD == '': HEAD = 'HEAD~1'
  300. do_Sys(Git.back_version,(get_Name(),HEAD))
  301. update_Git_Dir()
  302. def do_Sys(func,args,name='CoTan Git',break_time=2,show=True,text_n='',th=False):
  303. p = func(*args)
  304. def Out_Txt():
  305. nonlocal data
  306. dic = asksaveasfilename(title='选择文件保存位置',filetypes=[("TXT", ".txt")])
  307. try:
  308. if dic == '':return False
  309. if dic[-4] == '.txt':pass
  310. else:raise Exception
  311. except:
  312. dic += '.txt'
  313. with open(dic,'w',encoding='utf-8') as f:
  314. f.write(data)
  315. def Stop():nonlocal start;start = 0
  316. def keep():nonlocal start;start = float('inf')
  317. def pipe():pass
  318. def not_out():
  319. nonlocal text,data
  320. text.clear()
  321. text.insert(tkinter.END,data)
  322. start = time.time()
  323. data = ''
  324. if show:
  325. text, new_top = show_Now(Out_Txt,Stop,keep,not_out,pipe,name=name)
  326. if text_n != '':
  327. text.insert('0.0',f'载入前提示>>> {text_n}\n')
  328. new_top.update()
  329. top.update()
  330. def Update():
  331. nonlocal start
  332. while True:
  333. try:
  334. top.update()
  335. if show: new_top.update()
  336. if time.time() - start >= break_time:raise Exception
  337. except:
  338. start = 0
  339. break
  340. if th:
  341. j = threading.Thread(target=Update())
  342. j.start()
  343. while True:
  344. try:
  345. if not th:
  346. top.update()
  347. if show: new_top.update()
  348. except:
  349. break
  350. try:
  351. i = p.stdout.readline()#.decode(str_code)#不需要decode,因为Popen已经设置了universal_newlines=True
  352. if i.replace(' ','').replace('\n','') != '':
  353. if show:text.insert(tkinter.END,f'[out]> {i}')
  354. data += i
  355. if p.returncode == 0 or time.time() - start >= break_time:
  356. if show:text.insert(tkinter.END,'[END]')
  357. break
  358. elif p.returncode != None:
  359. raise Exception
  360. except:
  361. try:
  362. if show:text.insert(tkinter.END, '[ERROR]')
  363. raise Exception
  364. except:break
  365. p.kill()
  366. return data
  367. def not_Args(func):
  368. global Git
  369. name = get_Name()
  370. do_Sys(func,(name,))
  371. update_Git_Dir()
  372. def Commit_File():
  373. global Git,commit_m
  374. m = commit_m.get()
  375. if m.replace(' ','') == '':
  376. tkinter.messagebox.showinfo('警告!', '非常遗憾,我不同意你commit而不添加任何描述!\n描述是很重要的!')
  377. return False
  378. name = get_Name()
  379. do_Sys(Git.commit_File,(name,m))
  380. update_Git_Dir()
  381. def Diff_File():
  382. global Git,master
  383. MASTER = master.get()
  384. if MASTER == '':MASTER = 'HEAD'
  385. do_Sys(Git.diff_File,(get_Name(),MASTER))
  386. update_Git_Dir()
  387. def Add_File():
  388. global Git,Last_Name
  389. dic = askopenfilenames(title=f'选择要add的文件(取消为全选)')
  390. if dic == '':dic = '.'
  391. do_Sys(Git.add_File,(get_Name(),dic))
  392. update_Git_Dir()
  393. def update_Git_Dir():
  394. global Last_Name
  395. if Last_Name == None:return False
  396. Git_dir(Last_Name)
  397. def get_Git_Dir():
  398. name = get_Name()
  399. Git_dir(name)
  400. def Git_dir(name):
  401. global Git, Git_Dir,Last_Name
  402. dir_list = Git.get_Dir(name)
  403. try:#窗口可能已经关闭
  404. Git_Dir.delete(0,tkinter.END)
  405. Git_Dir.insert(tkinter.END,*dir_list)
  406. except:
  407. pass
  408. Last_Name = name
  409. def clone_git():#克隆仓库
  410. global clone_url
  411. new_Dic = askdirectory(title = '选择仓库地址')
  412. if new_Dic == '':return False
  413. Git.Clone_init(new_Dic, clone_url.get())
  414. Updata_GitBox()
  415. def init_git():#创建仓库
  416. global Git
  417. new_Dic = askdirectory(title = '选择仓库地址')
  418. if new_Dic == '':return False
  419. Git.Add_init(new_Dic,)
  420. Updata_GitBox()
  421. def get_Name(): # 获得名字统一接口
  422. global Git,Git_List,Git_Box
  423. try:
  424. return Git_List[Git_Box.curselection()[0]]
  425. except:
  426. try:
  427. return Git_List[0]
  428. except:
  429. return None
  430. def Updata_GitBox():
  431. global Git,Git_List,Git_Box
  432. Git_List = list(Git.get_git_Dic().keys())
  433. Git_Box.delete(0,tkinter.END)
  434. Git_Box.insert(tkinter.END,*Git_List)
  435. def show(data='',name='CoTan_Git'):
  436. global bg, ft1
  437. new_top = tkinter.Toplevel(bg=bg)
  438. new_top.title(name)
  439. new_top.geometry('+10+10') # 设置所在位置
  440. text = ScrolledText(new_top, font=('黑体', 11), height=50)
  441. text.pack(fill=tkinter.BOTH)
  442. text.insert('0.0', data)
  443. text.config(state=tkinter.DISABLED)
  444. new_top.resizable(width=False, height=False)
  445. def show_Now(out_func,close_func,keepFunc,not_out,pipeFunc,name='CoTan_Git >>> 高级命令行'):
  446. global bg
  447. new_top = tkinter.Toplevel(bg=bg)
  448. new_top.title(name)
  449. new_top.geometry('+10+10') # 设置所在位置
  450. new_top.resizable(width=False, height=False)
  451. class ScrolledText_new(ScrolledText):
  452. def __init__(self,*args,**kwargs):
  453. super(ScrolledText_new, self).__init__(*args,**kwargs)
  454. def insert(self, index, chars, *args):
  455. text.config(state=tkinter.NORMAL)
  456. super(ScrolledText_new, self).insert(index, chars, *args)
  457. text.config(state=tkinter.DISABLED)
  458. def clear(self):
  459. text.config(state=tkinter.NORMAL)
  460. self.delete('0.0',tkinter.END)
  461. text.config(state=tkinter.DISABLED)
  462. text = ScrolledText_new(new_top, font=('黑体', 11), height=30,width=100)
  463. text.grid(column=0, row=0,columnspan=5, sticky=tkinter.E + tkinter.W)
  464. text.config(state=tkinter.DISABLED)
  465. tkinter.Button(new_top, bg=bg, fg=fg, text='输出文档', font=('黑体', 11),width=20, height=2, command=out_func).grid(
  466. column=4, row=1, sticky=tkinter.E + tkinter.W)
  467. tkinter.Button(new_top, bg=bg, fg=fg, text='关闭子线程连接', font=('黑体', 11),width=20, height=2, command=close_func).grid(
  468. column=0, row=1, sticky=tkinter.E + tkinter.W)
  469. tkinter.Button(new_top, bg=bg, fg=fg, text='保持线程连接', font=('黑体', 11),width=20, height=2, command=keepFunc).grid(
  470. column=1, row=1, sticky=tkinter.E + tkinter.W)
  471. tkinter.Button(new_top, bg=bg, fg=fg, text='格式化输出', font=('黑体', 11),width=20, height=2, command=not_out).grid(
  472. column=2, row=1, sticky=tkinter.E + tkinter.W)
  473. tkinter.Button(new_top, bg=bg, fg=fg, text='文件管道输入', font=('黑体', 11),width=20, height=2, command=pipeFunc).grid(
  474. column=3, row=1, sticky=tkinter.E + tkinter.W)
  475. return text,new_top
  476. if __name__ == '__main__':
  477. Main()
  478. # show_Now()