2
0

porcelain.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. # porcelain.py -- Porcelain-like layer on top of Dulwich
  2. # Copyright (C) 2013 Jelmer Vernooij <jelmer@samba.org>
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # or (at your option) a later version of the License.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  17. # MA 02110-1301, USA.
  18. import os
  19. import sys
  20. from dulwich.client import get_transport_and_path
  21. from dulwich.repo import (BaseRepo, Repo)
  22. from dulwich.server import update_server_info as server_update_server_info
  23. """Simple wrapper that provides porcelain-like functions on top of Dulwich.
  24. Currently implemented:
  25. * archive
  26. * add
  27. * clone
  28. * commit
  29. * init
  30. * remove
  31. * update-server-info
  32. These functions are meant to behave similarly to the git subcommands.
  33. Differences in behaviour are considered bugs.
  34. """
  35. __docformat__ = 'restructuredText'
  36. def open_repo(path_or_repo):
  37. """Open an argument that can be a repository or a path for a repository."""
  38. if isinstance(path_or_repo, BaseRepo):
  39. return path_or_repo
  40. return Repo(path_or_repo)
  41. def archive(location, committish=None, outstream=sys.stdout,
  42. errstream=sys.stderr):
  43. """Create an archive.
  44. :param location: Location of repository for which to generate an archive.
  45. :param committish: Commit SHA1 or ref to use
  46. :param outstream: Output stream (defaults to stdout)
  47. :param errstream: Error stream (defaults to stderr)
  48. """
  49. client, path = get_transport_and_path(location)
  50. if committish is None:
  51. committish = "HEAD"
  52. client.archive(path, committish, outstream.write, errstream.write)
  53. def update_server_info(repo="."):
  54. """Update server info files for a repository.
  55. :param repo: path to the repository
  56. """
  57. r = open_repo(repo)
  58. server_update_server_info(r)
  59. def commit(repo=".", message=None):
  60. """Create a new commit.
  61. :param repo: Path to repository
  62. :param message: Optional commit message
  63. """
  64. # FIXME: Support --all argument
  65. # FIXME: Support --signoff argument
  66. r = open_repo(repo)
  67. r.do_commit(message=message)
  68. def init(path=".", bare=False):
  69. """Create a new git repository.
  70. :param path: Path to repository.
  71. :param bare: Whether to create a bare repository.
  72. """
  73. if not os.path.exists(path):
  74. os.mkdir(path)
  75. if bare:
  76. Repo.init_bare(path)
  77. else:
  78. Repo.init(path)
  79. def clone(source, target=None, bare=False, outstream=sys.stdout):
  80. """Clone a local or remote git repository.
  81. :param source: Path or URL for source repository
  82. :param target: Path to target repository (optional)
  83. :param bare: Whether or not to create a bare repository
  84. :param outstream: Optional stream to write progress to
  85. """
  86. client, host_path = get_transport_and_path(source)
  87. if target is None:
  88. target = host_path.split("/")[-1]
  89. if not os.path.exists(target):
  90. os.mkdir(target)
  91. if bare:
  92. r = Repo.init_bare(target)
  93. else:
  94. r = Repo.init(target)
  95. remote_refs = client.fetch(host_path, r,
  96. determine_wants=r.object_store.determine_wants_all,
  97. progress=outstream.write)
  98. r["HEAD"] = remote_refs["HEAD"]
  99. def add(repo=".", paths=None):
  100. """Add files to the staging area.
  101. :param repo: Repository for the files
  102. :param paths: Paths to add
  103. """
  104. # FIXME: Support patterns, directories, no argument.
  105. r = open_repo(repo)
  106. r.stage(paths)
  107. def rm(repo=".", paths=None):
  108. """Remove files from the staging area.
  109. :param repo: Repository for the files
  110. :param paths: Paths to remove
  111. """
  112. r = open_repo(repo)
  113. index = r.open_index()
  114. for p in paths:
  115. del index[p]
  116. index.write()