git.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import os
  2. import sys
  3. from io import RawIOBase
  4. from dulwich import porcelain
  5. from dulwich.porcelain import get_remote_repo
  6. from dulwich.repo import Repo
  7. from dulwich.walk import Walker
  8. import requests
  9. def ro():
  10. os.system("/etc/init.d/S01mount_ro start")
  11. def rw():
  12. os.system("/etc/init.d/S01mount_ro stop")
  13. class NoneStream(RawIOBase):
  14. """Fallback if stdout or stderr are unavailable, does nothing."""
  15. def read(self, size=-1):
  16. return None
  17. def readall(self):
  18. return None
  19. def readinto(self, b):
  20. return None
  21. def write(self, b):
  22. return None
  23. def log(localRep):
  24. repo = Repo(localRep)
  25. walker = Walker(repo, sorted(repo.get_refs().values(), reverse=True))
  26. for entry in walker:
  27. commit = entry.commit
  28. print(f"commit {commit.id.decode()}")
  29. print(f"Author: {commit.author.decode()} <{commit.author.decode()}>")
  30. print(f"Date: {commit.author_time}")
  31. print()
  32. print(f" {commit.message.decode()}")
  33. print()
  34. def __log__(localRep):
  35. repo = Repo(localRep)
  36. walker = Walker(repo, sorted(repo.get_refs().values(), reverse=True))
  37. for entry in walker:
  38. commit = entry.commit
  39. return f"commit {commit.id.decode()}", f"Author: {commit.author.decode()} <{commit.author.decode()}>", \
  40. f"Date: {commit.author_time}", f" {commit.message.decode()}"
  41. def showChanges(localRep, outStream=sys.stdout):
  42. porcelain.show(localRep, outstream=outStream)
  43. def activeBranch(localRep):
  44. return porcelain.active_branch(localRep)
  45. def checkout(localRep, branch, progStream=None):
  46. r = Repo(localRep)
  47. if progStream is None:
  48. progStream = NoneStream()
  49. try:
  50. porcelain.checkout_branch(r, 'origin/' + branch, outstream=progStream)
  51. except porcelain.CheckoutError as e:
  52. print("CheckoutError:", e)
  53. porcelain.reset(r, 'hard')
  54. porcelain.checkout_branch(r, 'origin/' + branch, outstream=progStream)
  55. def pull(localRep, progStream=None, depth=3):
  56. if progStream is None:
  57. progStream = NoneStream()
  58. porcelain.pull(localRep, refspecs=porcelain.active_branch(localRep), outstream=progStream, depth=depth)
  59. def reset(localRep):
  60. repo = Repo(localRep)
  61. head_ref = repo.head()
  62. head_commit = repo[head_ref]
  63. parent_commit = repo[head_commit.parents[0]]
  64. porcelain.reset(repo, 'hard', parent_commit.tree)
  65. def recovery(localRep):
  66. r = Repo(localRep)
  67. porcelain.reset(r, 'hard')
  68. def clone(source, target=None, depth: int = None):
  69. porcelain.clone(source, target, depth=depth)
  70. def isOnline(localRep):
  71. r = Repo(localRep)
  72. (remote_name, remote_location) = get_remote_repo(r, None)
  73. try:
  74. response = requests.get(remote_location)
  75. if response.status_code == 200:
  76. # print("Connection successful.")
  77. return True
  78. else:
  79. # print(f"Connection failed with status code: {response.status}")
  80. return False
  81. except Exception as e:
  82. # print(f"Connection failed: {e}")
  83. return False
  84. def swRemote(localRep, remote_url):
  85. r = Repo(localRep)
  86. porcelain.remote_remove(r,'origin')
  87. porcelain.remote_add(r,'origin',remote_url)
  88. def swRemote_force(localRep, remote_url):
  89. r = Repo(localRep)
  90. porcelain.remote_remove(r,'origin')
  91. porcelain.remote_add(r,'origin',remote_url)
  92. porcelain.fetch(localRep)
  93. porcelain.pull(localRep, refspecs=porcelain.active_branch(localRep), force=True)
  94. if __name__ == '__main__':
  95. localrep = "/home/backup"
  96. log(localrep)