Skip to content

Commit 2a4d57a

Browse files
committed
Make it possible to add new files to repositories
1 parent 00af0c6 commit 2a4d57a

File tree

2 files changed

+53
-9
lines changed

2 files changed

+53
-9
lines changed

vcs_repo_mgr/__init__.py

+38-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@
104104
)
105105

106106
# Semi-standard module versioning.
107-
__version__ = '0.27.2'
107+
__version__ = '0.28'
108108

109109
USER_CONFIG_FILE = os.path.expanduser('~/.vcs-repo-mgr.ini')
110110
"""The absolute pathname of the user-specific configuration file (a string)."""
@@ -872,6 +872,39 @@ def merge(self, revision=None):
872872
**self.get_author()
873873
))
874874

875+
def add_files(self, *pathnames, **kw):
876+
"""
877+
Stage new files in the working tree to be included in the next commit.
878+
879+
:param pathnames: Any positional arguments are expected to be pathnames
880+
relative to the root of the repository.
881+
:param all: If the keyword argument `all` is :data:`True` then all
882+
new files are added to the repository (in this case no
883+
pathnames should be given).
884+
:raises: :exc:`~exceptions.ValueError` when pathnames are given and the
885+
keyword argument `all` is also :data:`True`.
886+
887+
.. note:: Automatically creates the local repository on the first run.
888+
"""
889+
self.create()
890+
add_all = kw.get('all', False)
891+
logger.info("Staging working tree changes to be committed in %s ..", self.local)
892+
if pathnames and add_all:
893+
raise ValueError("You can't add specific pathnames using all=True!")
894+
if add_all:
895+
execute(self.get_command(
896+
method_name='add_files',
897+
attribute_name='add_command_all',
898+
local=self.local,
899+
))
900+
else:
901+
execute(self.get_command(
902+
method_name='add_files',
903+
attribute_name='add_command',
904+
local=self.local,
905+
filenames=pathnames,
906+
))
907+
875908
def commit(self, message, author=None):
876909
"""
877910
Commit changes to tracked files in the working tree.
@@ -1403,6 +1436,8 @@ class HgRepo(Repository):
14031436
hg -R {local} commit --user={author_combined} --message={message} --close-branch
14041437
''')
14051438
merge_command = 'hg -R {local} merge --rev={revision}'
1439+
add_command = 'hg --cwd {local} addremove {filenames}'
1440+
add_command_all = 'hg --cwd {local} addremove'
14061441
# The `hg remove --after' command is used to match the semantics of `git
14071442
# commit --all' however `hg remove --after' is _very_ verbose (it comments
14081443
# on every existing file in the repository) and unfortunately it ignores
@@ -1563,6 +1598,8 @@ class GitRepo(Repository):
15631598
-c user.email={author_email}
15641599
merge --no-commit --no-ff {revision}
15651600
''')
1601+
add_command = 'cd {local} && git add -- {filenames}'
1602+
add_command_all = 'cd {local} && git add --all .'
15661603
commit_command = compact('''
15671604
cd {local} && git
15681605
-c user.name={author_name}

vcs_repo_mgr/tests.py

+15-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
"""Automated tests for the `vcs-repo-mgr` package."""
2-
1+
# Version control system repository manager.
2+
#
33
# Author: Peter Odding <peter@peterodding.com>
4-
# Last Change: March 16, 2016
4+
# Last Change: March 18, 2016
55
# URL: https://github.com/xolox/python-vcs-repo-mgr
66

7+
"""Automated tests for the `vcs-repo-mgr` package."""
8+
79
# Standard library modules.
810
import hashlib
911
import logging
@@ -362,14 +364,19 @@ def check_checkout_support(self, repository):
362364
repository.checkout(revision=tag)
363365

364366
def check_commit_support(self, repository):
365-
"""Make sure we can make new commits."""
366-
logger.info("Testing commit() support ..")
367+
"""Make sure we can add files and make new commits."""
368+
logger.info("Testing add_files() and commit() support ..")
367369
try:
368370
# Make sure we start with a clean working tree.
369371
repository.checkout(clean=True)
370-
# Mutate a tracked file in the repository's working tree.
371-
self.mutate_working_tree(repository)
372-
# Make sure the working tree is no longer clean.
372+
# Pick a random filename to add to the repository.
373+
file_to_add = random_string()
374+
# Create the new file.
375+
with open(os.path.join(repository.local, file_to_add), 'w') as handle:
376+
handle.write("Testing, 1, 2, 3 ..\n")
377+
# Stage the addition of the new file.
378+
repository.add_files(file_to_add)
379+
# Make sure the working tree is dirty now.
373380
assert not repository.is_clean
374381
# Commit the change we made and ensure that commit() actually
375382
# creates a new revision in the relevant branch.

0 commit comments

Comments
 (0)