def set(self, var, value, where='dataset', reload=True, force=False): """Set a variable to a value. In opposition to `add`, this replaces the value of `var` if there is one already. Parameters ---------- var : str Variable name including any section like `git config` expects them, e.g. 'core.editor' value : str Variable value force: bool if set, replaces all occurrences of `var` by a single one with the given `value`. Otherwise raise if multiple entries for `var` exist already %s""" if where == 'override': self.overrides[var] = value if reload: self.reload(force=True) return from datalad.support.gitrepo import to_options self._run(to_options(replace_all=force) + [var, value], where=where, reload=reload, protocol=StdOutErrCapture)
def test_to_options(): class Some(object): def cmd_func(self, git_options=None, annex_options=None, options=None): git_options = git_options[:] if git_options else [] annex_options = annex_options[:] if annex_options else [] options = options[:] if options else [] faked_cmd_call = ['git'] + git_options + ['annex'] + \ annex_options + ['my_cmd'] + options return faked_cmd_call eq_(Some().cmd_func(options=to_options(m="bla", force=True)), ['git', 'annex', 'my_cmd', '--force', '-m', 'bla']) eq_( Some().cmd_func(git_options=to_options(C="/some/where"), annex_options=to_options(JSON=True), options=to_options(unused=True)), ['git', '-C', '/some/where', 'annex', '--JSON', 'my_cmd', '--unused']) eq_( Some().cmd_func(git_options=to_options( C="/some/where", split_single_char_options=False), annex_options=to_options(JSON=True), options=to_options(unused=True)), ['git', '-C/some/where', 'annex', '--JSON', 'my_cmd', '--unused'])
def test_to_options(): class Some(object): def cmd_func(self, git_options=None, annex_options=None, options=None): git_options = git_options[:] if git_options else [] annex_options = annex_options[:] if annex_options else [] options = options[:] if options else [] faked_cmd_call = ['git'] + git_options + ['annex'] + \ annex_options + ['my_cmd'] + options return faked_cmd_call eq_(Some().cmd_func(options=to_options(m="bla", force=True)), ['git', 'annex', 'my_cmd', '--force', '-m', 'bla']) eq_(Some().cmd_func(git_options=to_options(C="/some/where"), annex_options=to_options(JSON=True), options=to_options(unused=True)), ['git', '-C', '/some/where', 'annex', '--JSON', 'my_cmd', '--unused']) eq_(Some().cmd_func(git_options=to_options(C="/some/where", split_single_char_options=False), annex_options=to_options(JSON=True), options=to_options(unused=True)), ['git', '-C/some/where', 'annex', '--JSON', 'my_cmd', '--unused'])
def set(self, var, value, where='dataset', reload=True, force=False): """Set a variable to a value. In opposition to `add`, this replaces the value of `var` if there is one already. Parameters ---------- var : str Variable name including any section like `git config` expects them, e.g. 'core.editor' value : str Variable value force: bool if set, replaces all occurrences of `var` by a single one with the given `value`. Otherwise raise if multiple entries for `var` exist already %s""" from datalad.support.gitrepo import to_options self._run(to_options(replace_all=force) + [var, value], where=where, reload=reload, log_stderr=True)
def test_GitRepo_commit(path): gr = GitRepo(path) filename = get_most_obscure_supported_name() with open(op.join(path, filename), 'w') as f: f.write("File to add to git") gr.add(filename) gr.commit("Testing GitRepo.commit().") ok_clean_git(gr) eq_("Testing GitRepo.commit().{}".format(linesep), gr.repo.head.commit.message) with open(op.join(path, filename), 'w') as f: f.write("changed content") gr.add(filename) gr.commit("commit with options", options=to_options(dry_run=True)) # wasn't actually committed: ok_(gr.dirty) # commit with empty message: gr.commit() ok_clean_git(gr) # nothing to commit doesn't raise by default: gr.commit() # but does with careless=False: assert_raises(CommandError, gr.commit, careless=False) # committing untracked file raises: with open(op.join(path, "untracked"), "w") as f: f.write("some") assert_raises(FileNotInRepositoryError, gr.commit, files="untracked") # not existing file as well: assert_raises(FileNotInRepositoryError, gr.commit, files="not-existing")