@@ -96,6 +96,7 @@ def check_repo_state(ucfg):
9696 os .chdir (top_dir )
9797 except OSError as exc :
9898 logging .exception ("Could not change to repo directory: %s" , exc )
99+ sys .exit (1 )
99100
100101 sorted_dir_list = sorted ([x for x in top_dir .iterdir () if x .is_dir ()])
101102 repo_name_list = []
@@ -113,6 +114,33 @@ def check_repo_state(ucfg):
113114 return is_state_valid
114115
115116
117+ def generate_change_data (mrepo , base_tag , dir_name , cur_tag ):
118+ """
119+ Generate a changelog (full or diff) for the given repository and drop
120+ it in the configured top_dir directory. Checks repo config for base
121+ tag to decide full vs diff.
122+
123+ :param mrepo: a Munch repo obj with Munch repos item
124+ :type mrepo: Munch obj
125+ :param base_tag: starting tag for change diff
126+ :type base_tag: str or None
127+ :dir_name: directory name for output (top_dir)
128+ :type dir_name: str
129+ :param cur_tag: ending tag for change diff (tag on current commit)
130+ :type cur_tag: str
131+ """
132+ base_cmd_str = 'gitchangelog'
133+ output_file = f'{ dir_name } /{ mrepo .name } -CHANGELOG.{ mrepo .item .repo_changelog_ext } '
134+
135+ if base_tag :
136+ base_cmd_str = base_cmd_str + f' { base_tag } ..{ cur_tag } '
137+
138+ logging .debug ('Running gitchangelog cmd: %s' , base_cmd_str )
139+ chg_data = sp .check_output (split (base_cmd_str ))
140+ Path (output_file ).write_bytes (chg_data )
141+ logging .info ('ChangeLog file: %s' , Path (output_file ))
142+
143+
116144def install_with_pip (pip_name , quiet = False ):
117145 """
118146 Install a python repository via pip; this should be done in a local
@@ -403,6 +431,43 @@ def process_git_repos(flags, repos, pull, quiet):
403431 os .chdir (work_dir )
404432
405433
434+ def process_repo_changes (ucfg ):
435+ """
436+ Generate a changelog for any repos with the ``repo_gen_changes`` flag
437+ set. Note we do not check repo state here, we just process each valid
438+ repo entry.
439+
440+ :param ucfg: Munch configuration object extracted from config file
441+ :type ucfg: Munch cfgobj
442+ :param quiet: pass the ``quiet`` cmd line flag to install cmd
443+ """
444+ work_dir , top_dir = resolve_top_dir (ucfg .top_dir )
445+ valid_repo_state = check_repo_state (ucfg )
446+ if not valid_repo_state :
447+ raise DirectoryTypeError ('Inconsistent directories; try running repolite first?' )
448+
449+ git_get_tags = 'git tag --sort=taggerdate'
450+ git_check_tag = 'git tag --points-at'
451+
452+ for item in [x for x in ucfg .repos if x .repo_enable and x .repo_gen_changes ]:
453+ repo = Munch ()
454+ repo .name = item .repo_name
455+ repo .item = Munch (item )
456+ git_dir = item .repo_alias if item .repo_alias else item .repo_name
457+ os .chdir (git_dir )
458+
459+ all_tags = sp .check_output (split (git_get_tags ), text = True ).splitlines ()
460+ logging .debug ('%s tag list: %s' , item .repo_name , all_tags )
461+ commit_tags = sp .check_output (split (git_check_tag ), text = True ).splitlines ()
462+ logging .debug ('commit tag(s): %s' , commit_tags )
463+ last_tag = all_tags [- 1 ] if all_tags and all_tags [- 1 ] in commit_tags else ''
464+
465+ generate_change_data (repo , item .repo_changelog_base , top_dir , last_tag )
466+
467+ os .chdir (top_dir )
468+ os .chdir (work_dir )
469+
470+
406471def process_repo_install (ucfg , quiet ):
407472 """
408473 Install any repos with the ``repo_install`` flag set. Note we do not
@@ -499,13 +564,13 @@ def main(argv=None): # pragma: no cover
499564 "-i" ,
500565 "--install" ,
501566 action = "store_true" ,
502- help = "Install existing repositories (python only)" ,
567+ help = "Install enabled repositories (python only)" ,
503568 )
504569 parser .add_argument (
505570 "-u" ,
506571 "--update" ,
507572 action = "store_true" ,
508- help = "Update existing repositories" ,
573+ help = "Update existing/enabled repositories" ,
509574 )
510575 parser .add_argument (
511576 "-s" ,
@@ -520,6 +585,12 @@ def main(argv=None): # pragma: no cover
520585 dest = "apply" ,
521586 help = "Apply the given tag (see TAG arg) or use one from config file" ,
522587 )
588+ parser .add_argument (
589+ "-g" ,
590+ "--changelog" ,
591+ action = "store_true" ,
592+ help = "Run gitchangelog in enabled repositories, create files in top_dir" ,
593+ )
523594 parser .add_argument (
524595 '-l' ,
525596 '--lock-config' ,
@@ -531,7 +602,7 @@ def main(argv=None): # pragma: no cover
531602 "tag" ,
532603 metavar = "TAG" ,
533604 nargs = '?' ,
534- help = "Tag string override for all repositories (apply with -a)" ,
605+ help = "Optional tag string override (apply with -a)" ,
535606 type = str ,
536607 )
537608
@@ -560,6 +631,9 @@ def main(argv=None): # pragma: no cover
560631 sys .exit (0 )
561632
562633 try :
634+ if opts .changelog :
635+ process_repo_changes (cfg )
636+ sys .exit (0 )
563637 if opts .install :
564638 process_repo_install (cfg , opts .quiet )
565639 sys .exit (0 )
0 commit comments