Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

559

560

561

562

563

564

565

566

567

568

569

570

571

572

573

574

575

576

577

578

579

580

581

582

583

584

585

586

587

588

589

590

591

592

593

594

595

596

597

598

599

600

601

602

603

604

605

606

607

608

609

610

611

612

613

614

615

616

617

618

619

620

621

622

623

624

625

626

627

628

629

630

631

632

633

634

635

636

637

638

639

640

641

642

643

644

645

646

647

648

649

650

651

652

653

654

655

656

657

658

659

660

661

662

663

664

665

666

667

668

669

670

671

672

673

674

675

676

677

678

679

680

681

682

683

684

685

686

687

688

689

690

691

692

693

694

695

696

697

698

699

700

701

702

703

704

705

706

707

708

709

710

711

712

713

714

715

716

717

718

719

720

721

722

723

724

725

726

727

728

729

730

731

732

733

734

735

736

737

738

739

740

741

742

743

744

745

746

747

748

749

750

751

752

753

754

755

756

757

758

759

760

761

762

763

764

765

766

767

768

769

770

771

772

773

774

775

776

777

778

779

780

781

782

783

784

785

786

787

788

789

790

791

792

793

794

795

796

797

798

799

800

801

802

803

804

805

806

807

808

809

810

811

812

813

814

815

816

817

818

819

820

821

822

823

824

825

826

827

828

829

830

831

832

833

834

835

836

837

838

839

840

841

842

843

844

845

846

847

848

849

850

851

852

853

854

855

856

857

858

859

860

861

862

863

864

865

866

867

868

869

870

871

872

873

874

875

876

877

878

879

880

881

882

883

884

885

886

887

888

889

890

891

892

893

894

895

896

897

898

899

900

901

902

903

904

905

906

907

908

909

910

911

912

913

914

915

916

917

918

919

920

921

922

923

924

925

926

927

928

929

930

931

932

933

934

935

936

937

938

939

940

941

942

943

944

945

946

947

948

949

950

951

952

953

954

955

956

957

958

959

960

961

962

963

964

965

966

967

968

969

970

971

972

973

974

975

976

977

978

979

980

981

import argparse 

import copy 

import json 

import os 

import re 

import sys 

from shutil import copyfile 

from typing import Union, Dict, AnyStr, Type, Any, List 

from git import Git 

from git.exc import GitCommandError 

 

from .auto_correct import add_final_dot, capitalize, unslugify 

from .constants import PRETTY_FORMAT, TICKET_NUMBER_OTHER 

from .labels import ( 

# get_all_branch_types, 

# get_branch_types, 

# get_ignore_commits_exact_words, 

get_ignore_commits_prefixes, 

# get_other_branch_type, 

# get_other_branch_type_key, 

# get_unreleased, 

# get_unreleased_key, 

# get_unreleased_key_label, 

get_settings, 

BRANCH_TYPE_OTHER, 

BRANCH_TYPES, 

IGNORE_COMMITS_EXACT_WORDS, 

UNRELEASED, 

UNRELEASED_LABEL, 

) 

from .fetchers import FetcherRegistry 

from .renderers import ( 

BaseRenderer, 

HistoricalMarkdownRenderer, 

MarkdownRenderer, 

RendererRegistry, 

RestructuredTextRenderer, 

) 

from .helpers import project_dir 

from .patterns import ( 

REGEX_PATTERN_BRANCH_NAME, 

REGEX_PATTERN_COMMIT, 

REGEX_PATTERN_COMMIT_LINE, 

REGEX_PATTERN_MERGED_BRANCH_NAME, 

REGEX_PATTERN_TAG, 

) 

 

DEBUG = os.environ.get('DEBUG', False) 

 

__author__ = 'Artur Barseghyan' 

__copyright__ = '2019 Artur Barseghyan' 

__license__ = 'GPL-2.0-only OR LGPL-2.0-or-later' 

__all__ = ( 

'generate_changelog', 

'generate_changelog_cli', 

'get_branch_type', 

'get_logs', 

'get_repository', 

'json_changelog', 

'json_changelog_cli', 

'make_config_file', 

'make_config_file_cli', 

'prepare_changelog', 

'prepare_releases_changelog', 

'validate_between', 

) 

 

 

def get_repository(path: str = None) -> Git: 

if not (path and os.path.exists(path) and os.path.isdir(path)): 

path = os.getcwd() 

 

return Git(path) 

 

 

def get_logs(between: str = None, path: str = None) -> Dict[str, Any]: 

"""Get lots of logs. 

 

:param between: 

:param path: 

:return: 

""" 

repository = get_repository(path) 

lower: Union[str, Type[None]] = None 

upper: Union[str, Type[None]] = None 

if between: 

try: 

rev_list_text = repository.rev_list(between) 

rev_list = rev_list_text.split('\n') 

if len(rev_list) >= 2: 

upper = rev_list[0] 

lower = rev_list[-1] 

except GitCommandError as err: 

pass 

 

# Merges log 

text_log_merges_args = [] 

if lower and upper: 

# Note, that ~1 addition make sure the lower range is inclusive 

text_log_merges_args.append( 

"{}~1..{}".format(lower, upper) 

) 

text_log_merges_args.extend([ 

"--pretty={}".format(PRETTY_FORMAT), 

"--source", 

# "--all", 

"--merges", 

]) 

text_log_merges = repository.log(*text_log_merges_args) 

log_merges = text_log_merges.split("\n") 

 

# Commits log 

text_log_args = [] 

if lower and upper: 

# Note, that ~1 addition make sure the lower range is inclusive 

text_log_args.append( 

"{}~1..{}".format(lower, upper) 

) 

text_log_args.extend([ 

"--pretty={}".format(PRETTY_FORMAT), 

"--source", 

# "--all" # TODO: remove 

]) 

 

text_log = repository.log(*text_log_args) 

log = text_log.split("\n") 

 

# Tags log 

text_log_tags_args = [] 

# if lower and upper: 

# text_log_tags_args.append( 

# "{}..{}".format(lower, upper) 

# ) 

text_log_tags_args.extend([ 

"--tags", 

"--source", 

"--oneline" 

]) 

text_log_tags = repository.log(*text_log_tags_args) 

log_tags = text_log_tags.split("\n") 

commit_tags_list = [s.split(' ', 1)[0].split('\t', 1) for s in log_tags] 

commit_tags = dict([l for l in commit_tags_list if len(l) > 1]) 

 

return { 

'TEXT_LOG_MERGES': text_log_merges, 

'LOG_MERGES': log_merges, 

'TEXT_LOG': text_log, 

'LOG': log, 

'TEXT_LOG_TAGS': text_log_tags, 

'LOG_TAGS': log_tags, 

'COMMIT_TAGS': commit_tags, 

} 

 

 

def get_branch_type(branch_type: AnyStr) -> str: 

"""Get branch type. 

 

:param branch_type: 

:return: 

""" 

branch_type = branch_type.lower() 

return branch_type if branch_type in BRANCH_TYPES else BRANCH_TYPE_OTHER 

 

 

# def generate_empty_tree() -> Dict[str, dict]: 

# """Generate empty tree. 

# 

# Example: 

# 

# { 

# 'feature': {}, 

# 'bugfix': {}, 

# 'hotfix': {}, 

# 'deprecation': {}, 

# 'other': { 

# TICKET_NUMBER_OTHER: { 

# # 'title': '', 

# 'commits': {} 

# } 

# }, 

# } 

# 

# :return: 

# """ 

# empty_tree = {} 

# for key, value in BRANCH_TYPES.items(): 

# empty_tree.update({key: {}}) 

# 

# empty_tree[BRANCH_TYPE_OTHER][TICKET_NUMBER_OTHER] = {'commits': {}} 

# return empty_tree 

 

 

def prepare_changelog( 

between: str = None, 

unique_commit_messages: bool = False, 

headings_only: bool = False, 

unreleased_only: bool = False, 

fetch_title: bool = False, 

fetch_description: bool = False, 

path: str = None 

) -> Dict[ 

str, Dict[str, Dict[str, Union[str, Dict[str, Union[str, str]]]]] 

]: 

"""Prepare changelog. 

 

:param between: 

:param unique_commit_messages: 

:param headings_only: 

:param unreleased_only: 

:param fetch_title: 

:param fetch_description: 

:param path: 

:return: 

""" 

# tree = generate_empty_tree() 

tree = {} 

 

cur_branch = None 

cur_branch_type = None 

branch_types = {} 

 

logs = get_logs(between=between, path=path) 

 

settings = get_settings() 

 

# First fill feature branches only 

for json_entry in logs['LOG_MERGES']: 

try: 

entry = json.loads(json_entry) 

except json.decoder.JSONDecodeError: 

continue # TODO: fix this (when commit message contains " symbols) 

merge_commit = True if ' ' in entry['merge'] else False 

if merge_commit: 

match = re.match(REGEX_PATTERN_MERGED_BRANCH_NAME, entry['title']) 

 

# Skip strange feature branches 

if not match: 

continue 

 

# If no pattern found, use other branch 

try: 

branch_type = get_branch_type(match.group('branch_type')) 

except AttributeError: 

branch_type = BRANCH_TYPE_OTHER 

 

# If no pattern found, use other ticket number 

try: 

ticket_number = match.group('ticket_number') 

except AttributeError: 

ticket_number = TICKET_NUMBER_OTHER 

 

branch_title = None 

branch_description = None 

if ( 

(fetch_title or fetch_description) 

and settings.get('fetchDataFrom') 

and settings.get('fetchDataFrom') in FetcherRegistry.REGISTRY 

and ticket_number != TICKET_NUMBER_OTHER 

): 

fetcher_cls = FetcherRegistry.REGISTRY[settings.get('fetchDataFrom')] 

fetcher = fetcher_cls() 

fetcher_data = fetcher.fetch_issue_data(ticket_number) 

if fetch_title: 

branch_title = fetcher_data['title'] 

if fetch_description: 

branch_description = fetcher_data['description'] 

 

if not branch_title: 

branch_title = match.group('branch_title') 

 

# For normal tree 

release = logs['COMMIT_TAGS'].get(entry['commit_abbr']) 

 

if branch_type not in tree: 

tree[branch_type] = {} 

 

if ticket_number not in tree[branch_type]: 

tree[branch_type][ticket_number] = { 

'commit_hash': entry['commit_hash'], 

'commit_abbr': entry['commit_abbr'], 

'date': entry['datetime'], 

'ticket_number': ticket_number, 

'branch_type': branch_type, 

'slug': branch_title, 

'title': unslugify(branch_title), 

'description': branch_description, 

'commits': {}, 

'release': release, 

} 

branch_types.update({ticket_number: branch_type}) 

 

if headings_only: 

return tree 

 

# Now go through commits 

for json_entry in filter(None, logs['LOG']): 

try: 

entry = json.loads(json_entry) 

except json.decoder.JSONDecodeError: 

continue # TODO: fix this (when commit message contains " symbols) 

merge_commit = True if ' ' in entry['merge'] else False 

if merge_commit: 

match = re.match(REGEX_PATTERN_MERGED_BRANCH_NAME, entry['title']) 

 

# Skip strange feature branches 

if not match: 

continue 

 

try: 

branch_type = get_branch_type(match.group('branch_type')) 

except AttributeError: 

branch_type = BRANCH_TYPE_OTHER 

 

try: 

ticket_number = match.group('ticket_number') 

except AttributeError: 

ticket_number = TICKET_NUMBER_OTHER 

 

branch_title = match.group('branch_title') 

cur_branch = copy.copy(ticket_number) 

cur_branch_type = copy.copy(branch_type) 

else: 

match = re.match(REGEX_PATTERN_COMMIT_LINE, entry['title']) 

try: 

ticket_number = match.group('ticket_number') 

commit_message = match.group('commit_message') 

except AttributeError: 

ticket_number = '' 

commit_message = entry['title'][:] 

 

commit_message = unslugify(commit_message) 

commit_message = capitalize(commit_message) 

commit_message = add_final_dot(commit_message) 

 

# Ignore the following messages 

if commit_message.lower() in IGNORE_COMMITS_EXACT_WORDS: 

continue 

 

ignore_commits_prefixes = tuple(get_ignore_commits_prefixes()) 

if commit_message.lower().startswith(ignore_commits_prefixes): 

continue 

 

commit_hash = commit_message \ 

if unique_commit_messages \ 

else entry['commit_hash'] 

 

release = logs['COMMIT_TAGS'].get(entry['commit_abbr']) 

 

if cur_branch: 

 

if cur_branch_type not in tree: 

tree[cur_branch_type] = {} 

 

if cur_branch not in tree[cur_branch_type]: 

tree[cur_branch_type][cur_branch] = {} 

 

if 'commits' not in tree[cur_branch_type][cur_branch]: 

tree[cur_branch_type][cur_branch]['commits'] = {} 

 

if cur_branch == ticket_number: 

tree[cur_branch_type][cur_branch]['commits'][commit_hash] = { # NOQA 

'commit_hash': entry['commit_hash'], 

'commit_abbr': entry['commit_abbr'], 

'author': entry['author'], 

'date': entry['datetime'], 

'ticket_number': ticket_number, 

'title': commit_message, 

} 

else: 

other_branch_type = branch_types.get( 

ticket_number, 

BRANCH_TYPE_OTHER 

) 

try: 

if other_branch_type not in tree: 

tree[other_branch_type] = {} 

 

if ticket_number not in tree[other_branch_type]: 

tree[other_branch_type][ticket_number] = {} 

 

if 'commits' not in tree[other_branch_type][ticket_number]: 

tree[other_branch_type][ticket_number]['commits'] = {} 

 

tree[other_branch_type][ticket_number]['commits'][commit_hash] = { # NOQA 

'commit_hash': entry['commit_hash'], 

'commit_abbr': entry['commit_abbr'], 

'author': entry['author'], 

'date': entry['datetime'], 

'ticket_number': ticket_number, 

'title': commit_message, 

} 

except: 

# TODO: Anything here? 

pass 

else: 

if BRANCH_TYPE_OTHER not in tree: 

tree[BRANCH_TYPE_OTHER] = {} 

 

if TICKET_NUMBER_OTHER not in tree[BRANCH_TYPE_OTHER]: 

tree[BRANCH_TYPE_OTHER][TICKET_NUMBER_OTHER] = {} 

 

if 'commits' not in tree[BRANCH_TYPE_OTHER][TICKET_NUMBER_OTHER]: 

tree[BRANCH_TYPE_OTHER][TICKET_NUMBER_OTHER]['commits'] = {} 

 

tree[BRANCH_TYPE_OTHER][TICKET_NUMBER_OTHER]['commits'][commit_hash] = { # NOQA 

'commit_hash': entry['commit_hash'], 

'commit_abbr': entry['commit_abbr'], 

'author': entry['author'], 

'date': entry['datetime'], 

'ticket_number': ticket_number, 

'title': commit_message, 

} 

 

return tree 

 

 

def prepare_releases_changelog( 

between: str = None, 

unique_commit_messages: bool = False, 

headings_only: bool = False, 

unreleased_only: bool = False, 

fetch_title: bool = False, 

fetch_description: bool = False, 

path: str = None 

) -> Dict[ 

str, Dict[str, Dict[str, Union[str, Dict[str, Union[str, str]]]]] 

]: 

"""Prepare releases changelog. 

 

:param between: 

:param unique_commit_messages: 

:param headings_only: 

:param unreleased_only: 

:param fetch_title: 

:param fetch_description: 

:param path: 

:return: 

""" 

logs = get_logs(between=between, path=path) 

settings = get_settings() 

# releases = [UNRELEASED] + [tag for tag in logs['COMMIT_TAGS'].values()] 

# releases_tree = {tag: generate_empty_tree() for tag in releases} 

releases_tree = {} 

 

cur_branch = None 

cur_branch_type = None 

branch_types = {} 

 

# First fill feature branches only 

for json_entry in logs['LOG_MERGES']: 

try: 

entry = json.loads(json_entry) 

except json.decoder.JSONDecodeError: 

continue # TODO: fix this (when commit message contains " symbols) 

merge_commit = True if ' ' in entry['merge'] else False 

if merge_commit: 

match = re.match(REGEX_PATTERN_MERGED_BRANCH_NAME, entry['title']) 

 

# Skip strange feature branches 

if not match: 

continue 

 

# If no pattern found, use other branch 

try: 

branch_type = get_branch_type(match.group('branch_type')) 

except AttributeError: 

branch_type = BRANCH_TYPE_OTHER 

 

# If no pattern found, use other ticket number 

try: 

ticket_number = match.group('ticket_number') 

except AttributeError: 

ticket_number = TICKET_NUMBER_OTHER 

 

branch_title = None 

branch_description = None 

 

if ( 

(fetch_title or fetch_description) 

and settings.get('fetchDataFrom') 

and settings.get('fetchDataFrom') in FetcherRegistry.REGISTRY 

and ticket_number != TICKET_NUMBER_OTHER 

): 

fetcher_cls = FetcherRegistry.REGISTRY[settings.get('fetchDataFrom')] 

fetcher = fetcher_cls() 

fetcher_data = fetcher.fetch_issue_data(ticket_number) 

if fetch_title: 

branch_title = fetcher_data['title'] 

if fetch_description: 

branch_description = fetcher_data['description'] 

 

if not branch_title: 

branch_title = match.group('branch_title') 

 

# For normal tree 

release = logs['COMMIT_TAGS'].get(entry['commit_abbr']) 

if not release: 

release = UNRELEASED 

 

if release not in releases_tree: 

releases_tree[release] = {} 

 

if branch_type not in releases_tree[release]: 

releases_tree[release][branch_type] = {} 

 

if ticket_number not in releases_tree[release][branch_type]: 

releases_tree[release][branch_type][ticket_number] = { 

'commit_hash': entry['commit_hash'], 

'commit_abbr': entry['commit_abbr'], 

'date': entry['datetime'], 

'ticket_number': ticket_number, 

'branch_type': branch_type, 

'slug': branch_title, 

'title': unslugify(branch_title), 

'description': branch_description, 

'commits': {}, 

'release': release, 

} 

branch_types.update({ticket_number: branch_type}) 

 

if headings_only: 

if unreleased_only: 

return {UNRELEASED: releases_tree.get(UNRELEASED, {})} 

return releases_tree 

 

# Now go through commits 

for json_entry in filter(None, logs['LOG']): 

try: 

entry = json.loads(json_entry) 

except json.decoder.JSONDecodeError: 

continue # TODO: fix this (when commit message contains " symbols) 

 

merge_commit = True if ' ' in entry['merge'] else False 

if merge_commit: 

match = re.match(REGEX_PATTERN_MERGED_BRANCH_NAME, entry['title']) 

 

# Skip strange feature branches 

if not match: 

continue 

 

try: 

branch_type = get_branch_type(match.group('branch_type')) 

except AttributeError: 

branch_type = BRANCH_TYPE_OTHER 

 

try: 

ticket_number = match.group('ticket_number') 

except AttributeError: 

ticket_number = TICKET_NUMBER_OTHER 

 

branch_title = match.group('branch_title') 

cur_branch = copy.copy(ticket_number) 

cur_branch_type = copy.copy(branch_type) 

else: 

match = re.match(REGEX_PATTERN_COMMIT_LINE, entry['title']) 

try: 

ticket_number = match.group('ticket_number') 

commit_message = match.group('commit_message') 

except AttributeError: 

ticket_number = '' 

commit_message = entry['title'][:] 

 

commit_message = unslugify(commit_message) 

commit_message = capitalize(commit_message) 

commit_message = add_final_dot(commit_message) 

 

# Ignore the following messages 

if commit_message.lower() in IGNORE_COMMITS_EXACT_WORDS: 

continue 

 

ignore_commits_prefixes = tuple(get_ignore_commits_prefixes()) 

if commit_message.lower().startswith(ignore_commits_prefixes): 

continue 

 

commit_hash = commit_message \ 

if unique_commit_messages \ 

else entry['commit_hash'] 

 

release = logs['COMMIT_TAGS'].get(entry['commit_abbr']) 

if not release: 

release = UNRELEASED 

 

if cur_branch: 

 

if release not in releases_tree: 

releases_tree[release] = {} 

 

if cur_branch_type not in releases_tree[release]: 

releases_tree[release][cur_branch_type] = {} 

 

if cur_branch not in releases_tree[release][cur_branch_type]: 

releases_tree[release][cur_branch_type][cur_branch] = {} 

 

if 'commits' not in releases_tree[release][cur_branch_type][cur_branch]: 

releases_tree[release][cur_branch_type][cur_branch]['commits'] = {} 

 

if cur_branch == ticket_number: 

releases_tree[release][cur_branch_type][cur_branch]['commits'][commit_hash] = { # NOQA 

'commit_hash': entry['commit_hash'], 

'commit_abbr': entry['commit_abbr'], 

'author': entry['author'], 

'date': entry['datetime'], 

'ticket_number': ticket_number, 

'title': commit_message, 

} 

else: 

other_branch_type = branch_types.get( 

ticket_number, 

BRANCH_TYPE_OTHER 

) 

try: 

if release not in releases_tree: 

releases_tree[release] = {} 

 

if other_branch_type not in releases_tree[release]: 

releases_tree[release][other_branch_type] = {} 

 

if ticket_number not in releases_tree[release][other_branch_type]: 

releases_tree[release][other_branch_type][ticket_number] = {} 

 

if 'commits' not in releases_tree[release][other_branch_type][ticket_number]: 

releases_tree[release][other_branch_type][ticket_number]['commits'] = {} 

 

releases_tree[release][other_branch_type][ticket_number]['commits'][commit_hash] = { # NOQA 

'commit_hash': entry['commit_hash'], 

'commit_abbr': entry['commit_abbr'], 

'author': entry['author'], 

'date': entry['datetime'], 

'ticket_number': ticket_number, 

'title': commit_message, 

} 

except: 

# TODO: Anything here? 

pass 

else: 

if release not in releases_tree: 

releases_tree[release] = {} 

 

if BRANCH_TYPE_OTHER not in releases_tree[release]: 

releases_tree[release][BRANCH_TYPE_OTHER] = {} 

 

if TICKET_NUMBER_OTHER not in releases_tree[release][BRANCH_TYPE_OTHER]: 

releases_tree[release][BRANCH_TYPE_OTHER][TICKET_NUMBER_OTHER] = {} 

 

if 'commits' not in releases_tree[release][BRANCH_TYPE_OTHER][TICKET_NUMBER_OTHER]: 

releases_tree[release][BRANCH_TYPE_OTHER][TICKET_NUMBER_OTHER]['commits'] = {} 

 

releases_tree[release][BRANCH_TYPE_OTHER][TICKET_NUMBER_OTHER]['commits'][commit_hash] = { # NOQA 

'commit_hash': entry['commit_hash'], 

'commit_abbr': entry['commit_abbr'], 

'author': entry['author'], 

'date': entry['datetime'], 

'ticket_number': ticket_number, 

'title': commit_message, 

} 

 

if unreleased_only: 

return {UNRELEASED: releases_tree.get(UNRELEASED, {})} 

 

return releases_tree 

 

 

def validate_between(between: str = None) -> bool: 

"""Validate between. 

 

:param between: 

:return: 

""" 

if between: 

pass # TODO 

return True 

 

 

# def get_latest_release(path: str = None) -> str: 

# """Get latest release. 

# 

# Command: 

# 

# git describe --match '*.*' --abbr=0 

# 

# :return: 

# """ 

# repository = get_repository(path) 

# return repository.describe('--match', '*.*', '--abbr=0') 

 

 

def get_latest_releases(limit: int = 2, path: str = None) -> list: 

"""Get latest <limit> releases. 

 

Command: 

 

git tag --sort=-version:refname -l '*.*' | head -n <limit> 

 

:return: 

""" 

repository = get_repository(path) 

return repository.tag( 

'--sort=-version:refname', '--list', '*.*' 

).split('\n')[:limit] 

 

 

def json_changelog(between: str = None, 

include_other: bool = True, 

show_releases: bool = False, 

latest_release: bool = False, 

headings_only: bool = False, 

fetch_title: bool = False, 

fetch_description: bool = False, 

path: str = None): 

if latest_release: 

latest_two_releases = get_latest_releases(limit=2, path=path) 

latest_two_releases = latest_two_releases[::-1] 

if len(latest_two_releases): 

between = '..'.join(latest_two_releases) 

 

if not show_releases: 

tree = prepare_changelog( 

between=between, 

unique_commit_messages=True, 

headings_only=headings_only, 

path=path 

) 

return tree 

else: 

releases_tree = prepare_releases_changelog( 

between=between, 

unique_commit_messages=True, 

headings_only=headings_only, 

path=path 

) 

return releases_tree 

 

 

def json_changelog_cli() -> Type[None]: 

"""Generate changelog (JSON format).""" 

parser = argparse.ArgumentParser(description='Generate JSON changelog') 

parser.add_argument( 

'between', 

nargs='?', 

default=None, 

help="Range, might be tag or a commit or a branch.", 

) 

parser.add_argument( 

'--no-other', 

dest="no_other", 

default=False, 

action='store_true', 

help="No `Other` section", 

) 

parser.add_argument( 

'--show-releases', 

dest="show_releases", 

default=False, 

action='store_true', 

help="Show releases", 

) 

parser.add_argument( 

'--latest-release', 

dest="latest_release", 

default=False, 

action='store_true', 

help="Generate changelog for the latest release only", 

) 

parser.add_argument( 

'--headings-only', 

dest="headings_only", 

default=False, 

action='store_true', 

help="Generate headings only (no commit messages, only branch titles)", 

) 

parser.add_argument( 

'--fetch-title', 

dest="fetch_title", 

default=False, 

action='store_true', 

help="Fetch title", 

) 

parser.add_argument( 

'--fetch-description', 

dest="fetch_description", 

default=False, 

action='store_true', 

help="Fetch description", 

) 

 

args = parser.parse_args(sys.argv[1:]) 

between = args.between if validate_between(args.between) else None 

include_other = not args.no_other 

show_releases = args.show_releases 

latest_release = args.latest_release 

headings_only = args.headings_only 

fetch_title = args.fetch_title 

fetch_description = args.fetch_description 

 

print( 

json_changelog( 

between=between, 

include_other=include_other, 

show_releases=show_releases, 

latest_release=latest_release, 

headings_only=headings_only, 

fetch_title=fetch_title, 

fetch_description=fetch_description 

) 

) 

 

 

def generate_changelog(between: str = None, 

include_other: bool = True, 

show_releases: bool = False, 

latest_release: bool = False, 

headings_only: bool = False, 

unreleased_only: bool = False, 

fetch_title: bool = False, 

fetch_description: bool = False, 

renderer_cls: Union[ 

Type[HistoricalMarkdownRenderer], 

Type[MarkdownRenderer], 

Type[RestructuredTextRenderer] 

] = MarkdownRenderer, 

path: str = None) -> str: 

"""Generate changelog (markdown format).""" 

 

# if show_latest_release and between: 

# raise Exception( 

# "--show-latest-release can't be used in combination with specific" 

# "tags/commits/branches range." 

# ) 

if latest_release: 

latest_two_releases = get_latest_releases(limit=2, path=path) 

latest_two_releases = latest_two_releases[::-1] 

if len(latest_two_releases): 

between = '..'.join(latest_two_releases) 

 

renderer = renderer_cls() 

 

if not show_releases: 

tree = prepare_changelog( 

between=between, 

unique_commit_messages=True, 

headings_only=headings_only, 

unreleased_only=unreleased_only and show_releases, 

fetch_title=fetch_title, 

fetch_description=fetch_description, 

path=path 

) 

 

return renderer.render_changelog( 

tree=tree, 

include_other=include_other, 

headings_only=headings_only, 

fetch_description=fetch_description 

) 

else: 

releases_tree = prepare_releases_changelog( 

between=between, 

unique_commit_messages=True, 

headings_only=headings_only, 

unreleased_only=unreleased_only and show_releases, 

fetch_title=fetch_title, 

fetch_description=fetch_description, 

path=path 

) 

 

return renderer.render_releases_changelog( 

releases_tree=releases_tree, 

include_other=include_other, 

headings_only=headings_only, 

fetch_description=fetch_description 

) 

 

 

def generate_changelog_cli() -> Type[None]: 

parser = argparse.ArgumentParser(description='Generate changelog') 

parser.add_argument( 

'between', 

nargs='?', 

default=None, 

help="Range, might be tag or a commit or a branch.", 

) 

parser.add_argument( 

'--no-other', 

dest="no_other", 

default=False, 

action='store_true', 

help="No `Other` section", 

) 

parser.add_argument( 

'--show-releases', 

dest="show_releases", 

default=False, 

action='store_true', 

help="Show releases", 

) 

parser.add_argument( 

'--latest-release', 

dest="latest_release", 

default=False, 

action='store_true', 

help="Generate changelog for the latest release only", 

) 

parser.add_argument( 

'--headings-only', 

dest="headings_only", 

default=False, 

action='store_true', 

help="Generate headings only (no commit messages, only branch titles)", 

) 

parser.add_argument( 

'--unreleased-only', 

dest="unreleased_only", 

default=False, 

action='store_true', 

help="Show unreleased only. Works in combination with --show-releases", 

) 

parser.add_argument( 

'--fetch-title', 

dest="fetch_title", 

default=False, 

action='store_true', 

help="Fetch title", 

) 

parser.add_argument( 

'--fetch-description', 

dest="fetch_description", 

default=False, 

action='store_true', 

help="Fetch description", 

) 

parser.add_argument( 

'--renderer', 

dest="renderer", 

default=MarkdownRenderer.uid, 

action='store', 

help="Renderer", 

choices=list(RendererRegistry.REGISTRY.keys()) 

) 

args = parser.parse_args(sys.argv[1:]) 

between = args.between if validate_between(args.between) else None 

include_other = not args.no_other 

show_releases = args.show_releases 

latest_release = args.latest_release 

headings_only = args.headings_only 

unreleased_only = args.unreleased_only 

fetch_title = args.fetch_title 

fetch_description = args.fetch_description 

renderer_uid = args.renderer 

 

renderer_cls = RendererRegistry.get(renderer_uid, MarkdownRenderer) 

 

print( 

generate_changelog( 

between=between, 

include_other=include_other, 

show_releases=show_releases, 

latest_release=latest_release, 

headings_only=headings_only, 

unreleased_only=unreleased_only, 

fetch_title=fetch_title, 

fetch_description=fetch_description, 

renderer_cls=renderer_cls 

) 

) 

 

 

def make_config_file() -> bool: 

"""Make (or overwrite) config file. 

 

:return: 

""" 

source_filename = project_dir('.matyan.ini') 

dest_filename = os.path.join(os.getcwd(), '.matyan.ini') 

try: 

copyfile(source_filename, dest_filename) 

return True 

except Exception as err: 

return False 

 

 

def make_config_file_cli(): 

return not make_config_file()