Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions dvc/cli.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""DVC command line interface"""
import argparse
import logging
import os
import sys

from .command import (
Expand Down Expand Up @@ -191,6 +192,14 @@ def get_main_parser():
help="Show program's version.",
)

parser.add_argument(
"--cd",
default=os.path.curdir,
metavar="<path>",
help="Change to directory before executing.",
type=str,
)

# Sub commands
subparsers = parser.add_subparsers(
title="Available Commands",
Expand Down
5 changes: 5 additions & 0 deletions dvc/command/base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import os
from abc import ABC, abstractmethod

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -32,6 +33,8 @@ def __init__(self, args):
from dvc.repo import Repo
from dvc.updater import Updater

os.chdir(args.cd)

self.repo = Repo()
self.config = self.repo.config
self.args = args
Expand All @@ -55,3 +58,5 @@ def run(self):
class CmdBaseNoRepo(CmdBase):
def __init__(self, args): # pylint: disable=super-init-not-called
self.args = args

os.chdir(args.cd)
6 changes: 3 additions & 3 deletions dvc/command/repro.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
class CmdRepro(CmdBase):
def run(self):
saved_dir = os.path.realpath(os.curdir)
if self.args.cwd:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is always initialized with os.curdir if not given a value. Alternative would be if self.args.cwd != os.curdir

os.chdir(self.args.cwd)
os.chdir(self.args.cwd)

# Dirty hack so the for loop below can at least enter once
if self.args.all_pipelines:
Expand Down Expand Up @@ -97,7 +96,8 @@ def add_parser(subparsers, parent_parser):
"-c",
"--cwd",
default=os.path.curdir,
help="Directory within your repo to reproduce from.",
help="Directory within your repo to reproduce from. Note: deprecated "
"by `dvc --cd <path>`.",
metavar="<path>",
)
repro_parser.add_argument(
Expand Down
20 changes: 19 additions & 1 deletion tests/func/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,19 +160,37 @@ def test(self):

class TestFindRoot(TestDvc):
def test(self):
os.chdir("..")
class Cmd(CmdBase):
def run(self):
pass

class A:
quiet = False
verbose = True
cd = os.path.pardir

args = A()
with self.assertRaises(DvcException):
Cmd(args)


class TestCd(TestDvc):
def test(self):
class Cmd(CmdBase):
def run(self):
pass

class A:
quiet = False
verbose = True
cd = os.path.pardir

parent_dir = os.path.realpath(os.path.pardir)
args = A()
with self.assertRaises(DvcException):
Cmd(args)
current_dir = os.path.realpath(os.path.curdir)
self.assertEqual(parent_dir, current_dir)


def test_unknown_command_help(capsys):
Expand Down