您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

95 行
2.7KB

  1. import git
  2. from git.exc import InvalidGitRepositoryError
  3. from git.config import GitConfigParser
  4. from io import BytesIO
  5. import weakref
  6. __all__ = ('sm_section', 'sm_name', 'mkhead', 'find_first_remote_branch',
  7. 'SubmoduleConfigParser')
  8. #{ Utilities
  9. def sm_section(name):
  10. """:return: section title used in .gitmodules configuration file"""
  11. return 'submodule "%s"' % name
  12. def sm_name(section):
  13. """:return: name of the submodule as parsed from the section name"""
  14. section = section.strip()
  15. return section[11:-1]
  16. def mkhead(repo, path):
  17. """:return: New branch/head instance"""
  18. return git.Head(repo, git.Head.to_full_path(path))
  19. def find_first_remote_branch(remotes, branch_name):
  20. """Find the remote branch matching the name of the given branch or raise InvalidGitRepositoryError"""
  21. for remote in remotes:
  22. try:
  23. return remote.refs[branch_name]
  24. except IndexError:
  25. continue
  26. # END exception handling
  27. # END for remote
  28. raise InvalidGitRepositoryError("Didn't find remote branch '%r' in any of the given remotes" % branch_name)
  29. #} END utilities
  30. #{ Classes
  31. class SubmoduleConfigParser(GitConfigParser):
  32. """
  33. Catches calls to _write, and updates the .gitmodules blob in the index
  34. with the new data, if we have written into a stream. Otherwise it will
  35. add the local file to the index to make it correspond with the working tree.
  36. Additionally, the cache must be cleared
  37. Please note that no mutating method will work in bare mode
  38. """
  39. def __init__(self, *args, **kwargs):
  40. self._smref = None
  41. self._index = None
  42. self._auto_write = True
  43. super(SubmoduleConfigParser, self).__init__(*args, **kwargs)
  44. #{ Interface
  45. def set_submodule(self, submodule):
  46. """Set this instance's submodule. It must be called before
  47. the first write operation begins"""
  48. self._smref = weakref.ref(submodule)
  49. def flush_to_index(self):
  50. """Flush changes in our configuration file to the index"""
  51. assert self._smref is not None
  52. # should always have a file here
  53. assert not isinstance(self._file_or_files, BytesIO)
  54. sm = self._smref()
  55. if sm is not None:
  56. index = self._index
  57. if index is None:
  58. index = sm.repo.index
  59. # END handle index
  60. index.add([sm.k_modules_file], write=self._auto_write)
  61. sm._clear_cache()
  62. # END handle weakref
  63. #} END interface
  64. #{ Overridden Methods
  65. def write(self):
  66. rval = super(SubmoduleConfigParser, self).write()
  67. self.flush_to_index()
  68. return rval
  69. # END overridden methods
  70. #} END classes