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

import argparse 

import copy 

import json 

import os 

import re 

import sys 

from shutil import copyfile 

from pprint import pprint 

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

import git 

from git.exc import GitCommandError 

 

from .labels import ( 

get_all_branch_types, 

get_other_branch_type, 

get_branch_types, 

get_unreleased, 

get_other_branch_type_key, 

get_unreleased_key, 

get_unreleased_key_label, 

get_ignore_commits_exact_words, 

) 

from .helpers import project_dir 

from .patterns import ( 

REGEX_PATTERN_BRANCH_NAME, 

REGEX_PATTERN_MERGED_BRANCH_NAME, 

REGEX_PATTERN_COMMIT, 

REGEX_PATTERN_COMMIT_LINE, 

REGEX_PATTERN_TAG, 

) 

 

__author__ = 'Artur Barseghyan' 

__copyright__ = '2019 Artur Barseghyan' 

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

__all__ = ( 

'create_config_file', 

'generate_changelog_cli', 

'generate_empty_tree', 

'get_branch_type', 

'get_logs', 

'json_changelog_cli', 

'prepare_changelog', 

'prepare_releases_changelog', 

'validate_between', 

) 

 

REPOSITORY = git.Git(os.getcwd()) 

 

PRETTY_FORMAT = '{' \ 

'"commit_hash": "%H", ' \ 

'"commit_abbr": "%h", ' \ 

'"datetime": "%ci", ' \ 

'"title": "%s",' \ 

'"author": "%an", ' \ 

'"author_email": "%ae", ' \ 

'"merge": "%P"' \ 

'}' 

 

BRANCH_TYPE_OTHER = get_other_branch_type_key() # 'other' 

BRANCH_TYPES = get_all_branch_types() 

# BRANCH_TYPES = { 

# 'other': "Other", 

# 'feature': "Features", 

# 'bugfix': "Bugfixes", 

# 'hotfix': "Hotfixes", 

# 'deprecation': "Deprecations", 

# } 

TICKET_NUMBER_OTHER = 'other' 

# UNRELEASED = 'unreleased' 

UNRELEASED, UNRELEASED_LABEL = get_unreleased_key_label() 

 

IGNORE_COMMITS_EXACT_WORDS = get_ignore_commits_exact_words() 

 

 

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

"""Get lots of logs. 

 

:param between: 

:return: 

""" 

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: 

text_log_merges_args.append( 

"{}..{}".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: 

text_log_args.append( 

"{}..{}".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 = REPOSITORY.log( 

"--tags", 

"--source", 

"--oneline" 

) 

log_tags = text_log_tags.split("\n") 

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

 

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 

) -> Dict[ 

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

]: 

"""Prepare changelog. 

 

:param between: 

:param unique_commit_messages: 

:return: 

""" 

tree = generate_empty_tree() 

 

cur_branch = None 

cur_branch_type = None 

branch_types = {} 

 

logs = get_logs(between=between) 

 

# First fill feature branches only 

for json_entry in logs['LOG_MERGES']: 

entry = json.loads(json_entry) 

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 = match.group('branch_title') 

 

# For normal tree 

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

if ticket_number not in tree[branch_type]: 

tree[branch_type][ticket_number] = { 

'commit_hash': entry['commit_hash'], 

'commit_abbr': entry['commit_abbr'], 

'ticket_number': ticket_number, 

'branch_type': branch_type, 

'slug': branch_title, 

'title': branch_title.replace('-', ' ').title(), 

'commits': {}, 

'release': release, 

} 

branch_types.update({ticket_number: branch_type}) 

 

# 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'][:] 

 

# Ignore the following messages 

if commit_message.lower() in IGNORE_COMMITS_EXACT_WORDS: 

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 == 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: 

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: 

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 

) -> Dict[ 

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

]: 

"""Prepare releases changelog. 

 

:param between: 

:param unique_commit_messages: 

:return: 

""" 

logs = get_logs(between=between) 

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

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

 

cur_branch = None 

cur_branch_type = None 

branch_types = {} 

 

# First fill feature branches only 

for json_entry in logs['LOG_MERGES']: 

entry = json.loads(json_entry) 

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 = match.group('branch_title') 

 

# For normal tree 

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

if not release: 

release = UNRELEASED 

 

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'], 

'ticket_number': ticket_number, 

'branch_type': branch_type, 

'slug': branch_title, 

'title': branch_title.replace('-', ' ').title(), 

'commits': {}, 

'release': release, 

} 

branch_types.update({ticket_number: branch_type}) 

 

# 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'][:] 

 

# Ignore the following messages 

if commit_message.lower() in IGNORE_COMMITS_EXACT_WORDS: 

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 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: 

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: 

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, 

} 

 

return releases_tree 

 

 

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

"""Validate between. 

 

:param between: 

:return: 

""" 

if between: 

pass # TODO 

return True 

 

 

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", 

) 

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 

 

if not show_releases: 

tree = prepare_changelog( 

between=between, 

unique_commit_messages=True 

) 

pprint(tree) 

else: 

releases_tree = prepare_releases_changelog( 

between=between, 

unique_commit_messages=True 

) 

pprint(releases_tree) 

 

# if not include_other: 

# tree.pop(BRANCH_TYPE_OTHER) 

# pprint(tree) 

 

 

def generate_changelog() -> str: 

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

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( 

'--output-file', 

dest="show_releases", 

default=False, 

action='store_true', 

help="Show releases", 

) 

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 

 

changelog = [] 

 

if not show_releases: 

tree = prepare_changelog( 

between=between, 

unique_commit_messages=True 

) 

for branch_type, tickets in tree.items(): 

# Skip adding orphaned commits if explicitly asked not to. 

if branch_type == BRANCH_TYPE_OTHER and not include_other: 

continue 

 

# Do not add branch type if no related branches found 

if tickets: 

changelog.append("\n**{}**".format(BRANCH_TYPES.get(branch_type))) 

 

# Add tickets 

for ticket_number, ticket_data in tickets.items(): 

if branch_type != BRANCH_TYPE_OTHER: 

changelog.append( 

"\n*{} {}*\n".format(ticket_number, ticket_data['title']) 

) 

else: 

changelog.append('') 

 

for commit_hash, commit_data in ticket_data['commits'].items(): 

changelog.append( 

"- {} [{}]".format( 

commit_data['title'], 

commit_data['author'] 

) 

) 

else: 

releases_tree = prepare_releases_changelog( 

between=between, 

unique_commit_messages=True 

) 

for release, branches in releases_tree.items(): 

release_label = UNRELEASED_LABEL \ 

if release == UNRELEASED \ 

else release 

 

changelog.append("\n### {}".format(release_label)) 

for branch_type, tickets in branches.items(): 

# Skip adding orphaned commits if explicitly asked not to. 

if branch_type == BRANCH_TYPE_OTHER and not include_other: 

continue 

 

# Do not add branch type if no related branches found 

if tickets: 

changelog.append( 

"\n**{}**".format(BRANCH_TYPES.get(branch_type))) 

 

# Add tickets 

for ticket_number, ticket_data in tickets.items(): 

if branch_type != BRANCH_TYPE_OTHER: 

changelog.append( 

"\n*{} {}*\n".format(ticket_number, 

ticket_data['title']) 

) 

else: 

changelog.append('') 

 

for commit_hash, commit_data in ticket_data['commits'].items(): # NOQA 

changelog.append( 

"- {} [{}]".format( 

commit_data['title'], 

commit_data['author'] 

) 

) 

 

return '\n'.join(changelog) 

 

 

def generate_changelog_cli() -> Type[None]: 

print(generate_changelog()) 

 

 

def create_config_file() -> bool: 

"""Create 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 create_config_file_cli(): 

return not create_config_file()