git-gerrit 2.0.0__tar.gz → 2.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: git_gerrit
3
- Version: 2.0.0
3
+ Version: 2.1.0
4
4
  Summary: Gerrit review system command line tools.
5
5
  Home-page: https://github.com/meffie/git-gerrit
6
6
  Author: Michael Meffie
@@ -23,7 +23,6 @@ Git helpers for the Gerrit code review system, with an emphasis on the Gerrit
23
23
  old-style numeric identifiers.
24
24
  """
25
25
 
26
- from __future__ import print_function
27
26
  from __future__ import unicode_literals
28
27
 
29
28
  import os
@@ -50,7 +49,7 @@ from git_gerrit.error import GitGerritFormatError
50
49
  from git_gerrit.error import GitGerritNotFoundError
51
50
  from git_gerrit.error import GitGerritHookDirNotFound
52
51
 
53
- VERSION = "2.0.0"
52
+ VERSION = "2.1.0"
54
53
 
55
54
  _hush_linter = [
56
55
  GitGerritError,
@@ -104,6 +103,18 @@ sed -i '/^Change-Id:/d' "$1"
104
103
  """
105
104
 
106
105
 
106
+ def writeln(*items, out=None, newline=True):
107
+ """
108
+ Print items to an output stream.
109
+ """
110
+ if out is None:
111
+ out = sys.stdout
112
+ message = ' '.join([str(item) for item in items])
113
+ out.write('{0}'.format(message))
114
+ if newline:
115
+ out.write('\n')
116
+
117
+
107
118
  def _chmod(filename, mode):
108
119
  """
109
120
  Wrapper for os.chmod(); converts rwx... string to mode bits.
@@ -196,8 +207,9 @@ def cherry_pick(number, branch='origin/master', repodir=None):
196
207
  hash = commit['hash']
197
208
  break
198
209
  if not hash:
199
- sys.stderr.write(
200
- 'Failed to find gerrit number {0} on branch {1}.\n'.format(number, branch)
210
+ writeln(
211
+ 'Failed to find gerrit number {0} on branch {1}.'.format(number, branch),
212
+ out=sys.stderr
201
213
  )
202
214
  return 1
203
215
 
@@ -209,10 +221,14 @@ def cherry_pick(number, branch='origin/master', repodir=None):
209
221
  env['GERRIT_CHERRY_PICK'] = 'yes'
210
222
  code = 0
211
223
  try:
212
- print(git('cherry-pick', '-x', hash, _env=env))
224
+ writeln(git('cherry-pick', '-x', hash, _env=env))
213
225
  except sh.ErrorReturnCode as e:
214
- sys.stderr.write('Failed to cherry-pick {0}\n{1}\n'.format(hash, e.stderr))
226
+ writeln(
227
+ 'Failed to cherry-pick {0}\n{1}'.format(hash, e.stderr),
228
+ out=sys.stderr
229
+ )
215
230
  code = e.exit_code
231
+
216
232
  return code
217
233
 
218
234
 
@@ -261,35 +277,35 @@ def fetch(
261
277
  """
262
278
  config = Config(repodir)
263
279
  git = sh.Command('git').bake(_cwd=repodir, _tty_out=False)
264
- print('searching for gerrit {0}'.format(number))
280
+ writeln('searching for gerrit {0}'.format(number))
265
281
  change = current_change(number, repodir)
266
- print('found patchset number {0}'.format(change['patchset']))
282
+ writeln('found patchset number {0}'.format(change['patchset']))
267
283
  url = 'https://{0}/{1}'.format(config['host'], config['project'])
268
284
 
269
285
  if no_branch:
270
286
  refs = '{0}'.format(change['ref'])
271
- print('fetching {0} patchset {1}'.format(number, change['patchset']))
287
+ writeln('fetching {0} patchset {1}'.format(number, change['patchset']))
272
288
  git.fetch(url, refs, _cwd=repodir)
273
- print('fetched {0} to FETCH_HEAD'.format(number))
289
+ writeln('fetched {0} to FETCH_HEAD'.format(number))
274
290
  if checkout:
275
291
  git.checkout('FETCH_HEAD', _cwd=repodir)
276
- print('checked out FETCH_HEAD')
292
+ writeln('checked out FETCH_HEAD')
277
293
  else:
278
294
  if not branch:
279
- print('no branch specified')
295
+ writeln('no branch specified')
280
296
  return 1
281
297
  branch = branch.format(**change)
282
298
  if _branch_exists(branch):
283
- print('branch {0} already exists'.format(branch))
299
+ writeln('branch {0} already exists'.format(branch))
284
300
  return 1
285
301
  patchset = change['patchset']
286
302
  refs = '{0}:{1}'.format(change['ref'], branch)
287
- print('fetching {0},{1} to branch {2}'.format(number, patchset, branch))
303
+ writeln('fetching {0},{1} to branch {2}'.format(number, patchset, branch))
288
304
  git.fetch(url, refs, _cwd=repodir)
289
- print('fetched {0},{1} to branch {2}'.format(number, patchset, branch))
305
+ writeln('fetched {0},{1} to branch {2}'.format(number, patchset, branch))
290
306
  if checkout:
291
307
  git.checkout(branch, _cwd=repodir)
292
- print('checked out branch {0}'.format(branch))
308
+ writeln('checked out branch {0}'.format(branch))
293
309
 
294
310
 
295
311
  def install_hooks():
@@ -305,24 +321,26 @@ def install_hooks():
305
321
  # Install the gerrit commit-msg hook.
306
322
  commit_msg = os.path.join(hookdir, 'commit-msg')
307
323
  if os.path.exists(commit_msg):
308
- sys.stdout.write('{0} git hook already present.\n'.format(commit_msg))
324
+ writeln('{0} git hook already present.'.format(commit_msg))
309
325
  else:
310
326
  url = 'https://{0}/tools/hooks/commit-msg'.format(config['host'])
311
- sys.stdout.write('Downloading git hook to {0} ... '.format(commit_msg))
327
+ writeln('Downloading git hook to {0} ... '.format(commit_msg),
328
+ newline=False)
312
329
  urlretrieve(url, commit_msg)
313
330
  _chmod(commit_msg, 'rwxr-xr-x')
314
- sys.stdout.write('done.\n')
331
+ writeln('done.')
315
332
 
316
333
  # Install our custom prepare-commit-msg hook for cherry picking gerrits.
317
334
  prepare_commit_msg = os.path.join(hookdir, 'prepare-commit-msg')
318
335
  if os.path.exists(prepare_commit_msg):
319
- sys.stdout.write('%s hook already present.\n' % prepare_commit_msg)
336
+ writeln('{0} hook already present.'.format(prepare_commit_msg))
320
337
  else:
321
- sys.stdout.write('Writing file %s ... ' % prepare_commit_msg)
338
+ writeln('Writing file {0} ... '.format(prepare_commit_msg),
339
+ newline=False)
322
340
  with open(prepare_commit_msg, 'w') as f:
323
341
  f.write(GIT_HOOK)
324
342
  _chmod(prepare_commit_msg, 'rwxr-xr-x')
325
- sys.stdout.write('done.\n')
343
+ writeln('done.')
326
344
 
327
345
  return 0
328
346
 
@@ -411,31 +429,44 @@ def query(search, limit=None, details=False, repodir=None, **options):
411
429
  if 'current_revision' not in options:
412
430
  options['current_revision'] = True
413
431
 
414
- params = [('q', search)]
415
- if limit:
416
- params.append(('n', limit))
417
- for option in options:
418
- if options[option] is True:
419
- params.append(('o', option.upper()))
420
- query = '/changes/?{0}'.format(urlencode(params))
421
-
422
- remote = config.get('remote', default='origin')
423
- url = "https://{0}".format(config['host'])
424
- gerrit = pygerrit2.rest.GerritRestAPI(url)
425
- for change in gerrit.get(query):
426
- change['number'] = change['_number']
427
- change['hash'] = change['current_revision'] # alias
428
- change['patchset'] = change['revisions'][change['current_revision']]['_number']
429
- change['ref'] = change['revisions'][change['current_revision']]['ref']
430
- change['localref'] = change['ref'].replace('refs/', remote + '/')
431
- change['host'] = config['host']
432
- change['url'] = "https://{0}/{1}".format(config['host'], change['_number'])
433
- if 'topic' not in change:
434
- change['topic'] = 'no-topic' # default for --format "{topic}"
435
- if details:
436
- change_id = change['change_id']
437
- change['details'] = gerrit.get('/changes/{0}/detail'.format(change_id))
438
- yield change
432
+ start = 0
433
+ more_changes = False
434
+ while True: # do ... while more_changes
435
+ params = [('q', search)]
436
+ if limit:
437
+ params.append(('n', (limit - start)))
438
+ if start:
439
+ params.append(('S', start))
440
+ for option in options:
441
+ if options[option] is True:
442
+ params.append(('o', option.upper()))
443
+ query = '/changes/?{0}'.format(urlencode(params))
444
+
445
+ remote = config.get('remote', default='origin')
446
+ url = "https://{0}".format(config['host'])
447
+ gerrit = pygerrit2.rest.GerritRestAPI(url)
448
+ for change in gerrit.get(query):
449
+ start += 1
450
+ more_changes = change.get('_more_changes', False)
451
+ change['number'] = change['_number']
452
+ change['hash'] = change['current_revision'] # alias
453
+ change['patchset'] = change['revisions'][change['current_revision']]['_number']
454
+ change['ref'] = change['revisions'][change['current_revision']]['ref']
455
+ change['localref'] = change['ref'].replace('refs/', remote + '/')
456
+ change['host'] = config['host']
457
+ change['url'] = "https://{0}/{1}".format(config['host'], change['_number'])
458
+ if 'topic' not in change:
459
+ change['topic'] = 'no-topic' # default for --format "{topic}"
460
+ if details:
461
+ change_id = change['change_id']
462
+ change['details'] = gerrit.get('/changes/{0}/detail'.format(change_id))
463
+ yield change
464
+
465
+ if limit and start >= limit:
466
+ break
467
+
468
+ if not more_changes:
469
+ break
439
470
 
440
471
 
441
472
  def update(
@@ -497,7 +528,7 @@ def update(
497
528
  changeid = '{0},{1}'.format(number, change['patchset'])
498
529
  args.append(changeid)
499
530
  if verbose:
500
- print('running: ssh', '-p', port, host, 'gerrit', 'review', *args)
531
+ writeln('running: ssh', '-p', port, host, 'gerrit', 'review', *args)
501
532
  ssh('-p', port, host, 'gerrit', 'review', *args)
502
533
 
503
534
  args = []
@@ -508,7 +539,7 @@ def update(
508
539
  arg('branch', branch)
509
540
  args.append(number)
510
541
  if verbose:
511
- print('running: ssh', '-p', port, host, 'gerrit', 'set-reviewers', *args)
542
+ writeln('running: ssh', '-p', port, host, 'gerrit', 'set-reviewers', *args)
512
543
  ssh('-p', port, host, 'gerrit', 'set-reviewers', *args)
513
544
 
514
545
  return 0
@@ -26,6 +26,7 @@ import pprint
26
26
  import textwrap
27
27
 
28
28
  import git_gerrit
29
+ from git_gerrit import writeln
29
30
  from git_gerrit.unicode import asciitize, cook
30
31
  from git_gerrit.error import GitGerritError, GitGerritFormatError
31
32
 
@@ -41,11 +42,6 @@ def commands():
41
42
  return cmds
42
43
 
43
44
 
44
- def print_error(e):
45
- """Display error to stderr."""
46
- sys.stderr.write('ERROR: {0}\n'.format(e.message))
47
-
48
-
49
45
  def print_change(change, template='{number} {subject}', dump=False, out=None):
50
46
  """
51
47
  Format and print a change to the output stream. Retry by printing just the
@@ -60,16 +56,13 @@ def print_change(change, template='{number} {subject}', dump=False, out=None):
60
56
  None
61
57
  """
62
58
  template = cook(template)
63
- if not out:
64
- out = sys.stdout
65
59
  for pass_ in (1, 2):
66
60
  try:
67
61
  if dump:
68
62
  text = pprint.pformat(change)
69
63
  else:
70
64
  text = template.format(**change)
71
- out.write(text)
72
- out.write('\n')
65
+ writeln(text, out=out)
73
66
  return
74
67
  except KeyError as e:
75
68
  raise GitGerritFormatError(e)
@@ -90,7 +83,7 @@ def git_gerrit_version(argv=None):
90
83
  description=git_gerrit_version.__doc__.strip(),
91
84
  )
92
85
  parser.parse_args(argv)
93
- print(git_gerrit.VERSION)
86
+ writeln(git_gerrit.VERSION)
94
87
  return 0
95
88
 
96
89
 
@@ -131,7 +124,7 @@ git config options:
131
124
  try:
132
125
  git_gerrit.fetch(number, **args)
133
126
  except GitGerritError as e:
134
- print_error(e)
127
+ writeln(str(e), out=sys.stderr)
135
128
  return 1
136
129
 
137
130
 
@@ -179,7 +172,7 @@ Example:
179
172
  try:
180
173
  git_gerrit.cherry_pick(number, branch)
181
174
  except GitGerritError as e:
182
- print_error(e)
175
+ writeln(str(e), out=sys.stderr)
183
176
  return 1
184
177
 
185
178
 
@@ -222,7 +215,7 @@ git config options:
222
215
  try:
223
216
  git_gerrit.fetch(number, **args)
224
217
  except GitGerritError as e:
225
- print_error(e)
218
+ writeln(str(e), out=sys.stderr)
226
219
  return 1
227
220
 
228
221
 
@@ -234,11 +227,11 @@ def git_gerrit_help(argv=None):
234
227
  description=git_gerrit_help.__doc__.strip(),
235
228
  )
236
229
  parser.parse_args(argv)
237
- print('\nCommands for gerrit code review:\n')
230
+ writeln('\nCommands for gerrit code review:\n')
238
231
  for name, desc in commands():
239
- print(' {0:27} {1}'.format(name, desc))
240
- print('\nShow command details with:\n')
241
- print(' git gerrit-<command> -h')
232
+ writeln(' {0:27} {1}'.format(name, desc))
233
+ writeln('\nShow command details with:\n')
234
+ writeln(' git gerrit-<command> -h')
242
235
 
243
236
 
244
237
  def git_gerrit_install_hooks(argv=None):
@@ -257,7 +250,7 @@ git config options:
257
250
  try:
258
251
  git_gerrit.install_hooks()
259
252
  except GitGerritError as e:
260
- print_error(e)
253
+ writeln(str(e), out=sys.stderr)
261
254
  return 1
262
255
 
263
256
 
@@ -300,7 +293,7 @@ git config options:
300
293
  for change in git_gerrit.log(**args):
301
294
  print_change(change, template)
302
295
  except GitGerritError as e:
303
- print_error(e)
296
+ writeln(str(e), out=sys.stderr)
304
297
  return 1
305
298
 
306
299
 
@@ -357,7 +350,7 @@ git config options:
357
350
  for change in git_gerrit.query(search, **args):
358
351
  print_change(change, template, dump)
359
352
  except GitGerritError as e:
360
- print_error(e)
353
+ writeln(str(e), out=sys.stderr)
361
354
  return 1
362
355
 
363
356
 
@@ -423,18 +416,18 @@ examples:
423
416
  for change in git_gerrit.query(search):
424
417
  changes.append(change)
425
418
  except GitGerritError as e:
426
- print_error(e)
419
+ writeln(str(e), out=sys.stderr)
427
420
  return 1
428
421
 
429
422
  for change in changes:
430
423
  try:
431
424
  if dryrun:
432
- print("Skipping: %s %s" % (change['_number'], change['subject']))
425
+ writeln("Skipping: %s %s" % (change['_number'], change['subject']))
433
426
  else:
434
- print("Updating: %s %s" % (change['_number'], change['subject']))
427
+ writeln("Updating: %s %s" % (change['_number'], change['subject']))
435
428
  git_gerrit.update(change['_number'], **args)
436
429
  except GitGerritError as e:
437
- print_error(e)
430
+ writeln(str(e), out=sys.stderr)
438
431
  return 1
439
432
 
440
433
 
@@ -469,5 +462,5 @@ git config options:
469
462
  for commit in git_gerrit.unpicked(**args):
470
463
  print_change(commit, template)
471
464
  except GitGerritError as e:
472
- print_error(e)
465
+ writeln(str(e), out=sys.stderr)
473
466
  return 1
@@ -18,22 +18,20 @@
18
18
  # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19
19
  # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20
20
 
21
- from __future__ import print_function
22
21
  from __future__ import unicode_literals
23
- from sh.contrib import git
24
- from sh import ErrorReturnCode_1
22
+ import sh
25
23
  from git_gerrit.error import GitGerritConfigError
26
24
 
27
25
 
28
26
  class Config:
29
27
  def __init__(self, repodir=None):
30
- self.repodir = repodir
28
+ self.git = sh.Command('git').bake(_cwd=repodir, _tty_out=False)
31
29
 
32
30
  def _get(self, name):
33
31
  """Read a config value with 'git config --get'."""
34
32
  try:
35
- return git.config('--get', 'gerrit.%s' % name, _cwd=self.repodir).rstrip()
36
- except ErrorReturnCode_1:
33
+ return self.git.config('--get', 'gerrit.%s' % name).rstrip()
34
+ except sh.ErrorReturnCode_1:
37
35
  return None
38
36
 
39
37
  def __getitem__(self, variable):
@@ -20,7 +20,6 @@
20
20
 
21
21
  """unicode related functions"""
22
22
 
23
- from __future__ import print_function
24
23
  from __future__ import unicode_literals
25
24
  import sys
26
25
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: git_gerrit
3
- Version: 2.0.0
3
+ Version: 2.1.0
4
4
  Summary: Gerrit review system command line tools.
5
5
  Home-page: https://github.com/meffie/git-gerrit
6
6
  Author: Michael Meffie
@@ -13,4 +13,5 @@ git_gerrit.egg-info/dependency_links.txt
13
13
  git_gerrit.egg-info/entry_points.txt
14
14
  git_gerrit.egg-info/requires.txt
15
15
  git_gerrit.egg-info/top_level.txt
16
- tests/test_help.py
16
+ tests/test_api.py
17
+ tests/test_cli.py
@@ -0,0 +1,41 @@
1
+ import pytest
2
+ import git_gerrit
3
+
4
+
5
+ def test_config(mock_commands):
6
+ config = git_gerrit.Config()
7
+ assert config["project"] == "mayhem"
8
+ assert config["host"] == "gerrit.example.org"
9
+ assert config.get("remote", "origin") == "origin"
10
+ with pytest.raises(git_gerrit.error.GitGerritConfigError):
11
+ config["bogus"]
12
+
13
+
14
+ def test_query(output, mock_rest):
15
+
16
+ search = "12345"
17
+ changes = list(git_gerrit.query(search))
18
+ assert len(changes) == 1
19
+
20
+ change = changes[0]
21
+ assert change["number"] == 12345
22
+ assert change["hash"] == "0123456789abcdef0123456789abcdef01234567"
23
+ assert change["patchset"] == 7
24
+ assert change["ref"] == "refs/changes/45/12345/7"
25
+ assert change["localref"] == "origin/changes/45/12345/7"
26
+ assert change["host"] == "gerrit.example.org"
27
+ assert change["url"] == "https://gerrit.example.org/12345"
28
+ assert change["topic"] == "no-topic"
29
+ assert "details" not in change
30
+
31
+
32
+ def test_fetch(output, mock_rest):
33
+ git_gerrit.fetch(12345)
34
+ assert output[0] == "searching for gerrit 12345"
35
+ assert output[1] == "found patchset number 7"
36
+ assert output[2] == "fetching 12345,7 to branch gerrit/12345/7"
37
+ assert output[3] == "fetched 12345,7 to branch gerrit/12345/7"
38
+
39
+
40
+ def test_update(output, mock_rest):
41
+ git_gerrit.update(12345, message="test")
@@ -0,0 +1,55 @@
1
+ import git_gerrit
2
+ import git_gerrit.cli
3
+
4
+
5
+ def test_help(argv, output):
6
+ git_gerrit.cli.git_gerrit_help(argv)
7
+ assert len(output) > 1
8
+ assert "Commands for gerrit code review" in output[0]
9
+
10
+
11
+ def test_query(argv, output, mock_rest):
12
+ argv.append("12345")
13
+ git_gerrit.cli.git_gerrit_query(argv)
14
+ assert len(output) == 1
15
+ assert output[0] == "12345 Transmogrify the frobnicator"
16
+
17
+
18
+ def test_version(argv, output):
19
+ git_gerrit.cli.git_gerrit_version(argv)
20
+ assert len(output) == 1
21
+ assert output[0] == git_gerrit.VERSION
22
+
23
+
24
+ def test_checkout(argv, output, mock_rest):
25
+ argv.append("12345")
26
+ git_gerrit.cli.git_gerrit_checkout(argv)
27
+
28
+
29
+ def test_cherry_pick(argv, output, mock_rest):
30
+ argv.append("12345")
31
+ git_gerrit.cli.git_gerrit_cherry_pick(argv)
32
+
33
+
34
+ def test_fetch(argv, output, mock_rest):
35
+ argv.append("12345")
36
+ git_gerrit.cli.git_gerrit_fetch(argv)
37
+
38
+
39
+ def test_install_hook(argv, output, mock_rest):
40
+ git_gerrit.cli.git_gerrit_install_hooks(argv)
41
+
42
+
43
+ def test_log(argv, output, mock_rest):
44
+ git_gerrit.cli.git_gerrit_log(argv)
45
+
46
+
47
+ def test_unpicked(argv, output, mock_rest):
48
+ argv.append("stable")
49
+ git_gerrit.cli.git_gerrit_unpicked(argv)
50
+
51
+
52
+ def test_update(argv, output, mock_rest):
53
+ argv.append("12345")
54
+ argv.append("--message=test")
55
+ git_gerrit.cli.git_gerrit_update(argv)
@@ -1,6 +0,0 @@
1
- from git_gerrit.cli import git_gerrit_help
2
-
3
-
4
- def test_help():
5
- argv = [] # do not use sys.argv
6
- git_gerrit_help(argv)
File without changes
File without changes
File without changes
File without changes
File without changes