| 1 |
import os |
| 2 |
import sys |
| 3 |
import posixpath |
| 4 |
|
| 5 |
import click |
| 6 |
|
| 7 |
|
| 8 |
class Repo(object): |
| 9 |
def __init__(self, home): |
| 10 |
self.home = home |
| 11 |
self.config = {} |
| 12 |
self.verbose = False |
| 13 |
|
| 14 |
def set_config(self, key, value): |
| 15 |
self.config[key] = value |
| 16 |
if self.verbose: |
| 17 |
click.echo(' config[%s] = %s' % (key, value), file=sys.stderr) |
| 18 |
|
| 19 |
def __repr__(self): |
| 20 |
return '<Repo %r>' % self.home |
| 21 |
|
| 22 |
|
| 23 |
pass_repo = click.make_pass_decorator(Repo) |
| 24 |
|
| 25 |
|
| 26 |
@click.group() |
| 27 |
@click.option( |
| 28 |
'--repo-home', |
| 29 |
envvar='REPO_HOME', |
| 30 |
default='.repo', |
| 31 |
metavar='PATH', |
| 32 |
help='Changes the repository folder location.', |
| 33 |
) |
| 34 |
@click.option( |
| 35 |
'--config', |
| 36 |
nargs=2, |
| 37 |
multiple=True, |
| 38 |
metavar='KEY VALUE', |
| 39 |
help='Overrides a config key/value pair.', |
| 40 |
) |
| 41 |
@click.option('--verbose', '-v', is_flag=True, help='Enables verbose mode.') |
| 42 |
@click.version_option('1.0') |
| 43 |
@click.pass_context |
| 44 |
def cli(ctx, repo_home, config, verbose): |
| 45 |
"""Repo is a command line tool that showcases how to build complex |
| 46 |
command line interfaces with Click. |
| 47 |
|
| 48 |
This tool is supposed to look like a distributed version control |
| 49 |
system to show how something like this can be structured. |
| 50 |
""" |
| 51 |
# Create a repo object and remember it as as the context object. From |
| 52 |
# this point onwards other commands can refer to it by using the |
| 53 |
# @pass_repo decorator. |
| 54 |
ctx.obj = Repo(os.path.abspath(repo_home)) |
| 55 |
ctx.obj.verbose = verbose |
| 56 |
for key, value in config: |
| 57 |
ctx.obj.set_config(key, value) |
| 58 |
|
| 59 |
|
| 60 |
@cli.command() |
| 61 |
@click.argument('src') |
| 62 |
@click.argument('dest', required=False) |
| 63 |
@click.option( |
| 64 |
'--shallow/--deep', |
| 65 |
default=False, |
| 66 |
help='Makes a checkout shallow or deep. Deep by default.', |
| 67 |
) |
| 68 |
@click.option( |
| 69 |
'--rev', |
| 70 |
'-r', |
| 71 |
default='HEAD', |
| 72 |
help='Clone a specific revision instead of HEAD.', |
| 73 |
) |
| 74 |
@pass_repo |
| 75 |
def clone(repo, src, dest, shallow, rev): |
| 76 |
"""Clones a repository. |
| 77 |
|
| 78 |
This will clone the repository at SRC into the folder DEST. If DEST |
| 79 |
is not provided this will automatically use the last path component |
| 80 |
of SRC and create that folder. |
| 81 |
""" |
| 82 |
if dest is None: |
| 83 |
dest = posixpath.split(src)[-1] or '.' |
| 84 |
click.echo('Cloning repo %s to %s' % (src, os.path.abspath(dest))) |
| 85 |
repo.home = dest |
| 86 |
if shallow: |
| 87 |
click.echo('Making shallow checkout') |
| 88 |
click.echo('Checking out revision %s' % rev) |
| 89 |
|
| 90 |
|
| 91 |
@cli.command() |
| 92 |
@click.confirmation_option() |
| 93 |
@pass_repo |
| 94 |
def delete(repo): |
| 95 |
"""Deletes a repository. |
| 96 |
|
| 97 |
This will throw away the current repository. |
| 98 |
""" |
| 99 |
click.echo('Destroying repo %s' % repo.home) |
| 100 |
click.echo('Deleted!') |
| 101 |
|
| 102 |
|
| 103 |
@cli.command() |
| 104 |
@click.option( |
| 105 |
'--username', prompt=True, help="The developer's shown username." |
| 106 |
) |
| 107 |
@click.option('--email', prompt='E-Mail', help="The developer's email address") |
| 108 |
@click.password_option(help='The login password.') |
| 109 |
@pass_repo |
| 110 |
def setuser(repo, username, email, password): |
| 111 |
"""Sets the user credentials. |
| 112 |
|
| 113 |
This will override the current user config. |
| 114 |
""" |
| 115 |
repo.set_config('username', username) |
| 116 |
repo.set_config('email', email) |
| 117 |
repo.set_config('password', '*' * len(password)) |
| 118 |
click.echo('Changed credentials.') |
| 119 |
|
| 120 |
|
| 121 |
@cli.command() |
| 122 |
@click.option( |
| 123 |
'--message', |
| 124 |
'-m', |
| 125 |
multiple=True, |
| 126 |
help='The commit message. If provided multiple times each ' |
| 127 |
'argument gets converted into a new line.', |
| 128 |
) |
| 129 |
@click.argument('files', nargs=-1, type=click.Path()) |
| 130 |
@pass_repo |
| 131 |
def commit(repo, files, message): |
| 132 |
"""Commits outstanding changes. |
| 133 |
|
| 134 |
Commit changes to the given files into the repository. You will need to |
| 135 |
"repo push" to push up your changes to other repositories. |
| 136 |
|
| 137 |
If a list of files is omitted, all changes reported by "repo status" |
| 138 |
will be committed. |
| 139 |
""" |
| 140 |
if not message: |
| 141 |
marker = '# Files to be committed:' |
| 142 |
hint = ['', '', marker, '#'] |
| 143 |
for file in files: |
| 144 |
hint.append('# U %s' % file) |
| 145 |
message = click.edit('\n'.join(hint)) |
| 146 |
if message is None: |
| 147 |
click.echo('Aborted!') |
| 148 |
return |
| 149 |
msg = message.split(marker)[0].rstrip() |
| 150 |
if not msg: |
| 151 |
click.echo('Aborted! Empty commit message') |
| 152 |
return |
| 153 |
else: |
| 154 |
msg = '\n'.join(message) |
| 155 |
click.echo('Files to be committed: %s' % (files,)) |
| 156 |
click.echo('Commit message:\n' + msg) |
| 157 |
|
| 158 |
|
| 159 |
@cli.command(short_help='Copies files.') |
| 160 |
@click.option( |
| 161 |
'--force', is_flag=True, help='forcibly copy over an existing managed file' |
| 162 |
) |
| 163 |
@click.argument('src', nargs=-1, type=click.Path()) |
| 164 |
@click.argument('dst', type=click.Path()) |
| 165 |
@pass_repo |
| 166 |
def copy(repo, src, dst, force): |
| 167 |
"""Copies one or multiple files to a new location. This copies all |
| 168 |
files from SRC to DST. |
| 169 |
""" |
| 170 |
for fn in src: |
| 171 |
click.echo('Copy from %s -> %s' % (fn, dst)) |