git-well 0.1.1__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.
@@ -0,0 +1,102 @@
1
+ Metadata-Version: 2.1
2
+ Name: git_well
3
+ Version: 0.1.1
4
+ Summary: The git_well module
5
+ Home-page: UNKNOWN
6
+ Author: Jon Crall
7
+ Author-email: erotemic@gmail.com
8
+ License: Apache 2
9
+ Platform: UNKNOWN
10
+ Classifier: Development Status :: 1 - Planning
11
+ Classifier: Intended Audience :: Developers
12
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
13
+ Classifier: Topic :: Utilities
14
+ Classifier: License :: OSI Approved :: Apache Software License
15
+ Classifier: Programming Language :: Python :: 3.7
16
+ Classifier: Programming Language :: Python :: 3.8
17
+ Classifier: Programming Language :: Python :: 3.9
18
+ Classifier: Programming Language :: Python :: 3.10
19
+ Classifier: Programming Language :: Python :: 3.11
20
+ Requires-Python: >=3.7
21
+ Description-Content-Type: text/x-rst
22
+ Provides-Extra: all
23
+ Provides-Extra: all-strict
24
+ Provides-Extra: optional
25
+ Provides-Extra: optional-strict
26
+ Provides-Extra: runtime-strict
27
+ Provides-Extra: tests
28
+ Provides-Extra: tests-strict
29
+
30
+ The git_well Module
31
+ ===================
32
+
33
+
34
+ |Pypi| |PypiDownloads| |GithubActions| |Codecov|
35
+
36
+ Git Well is a collection of git command line tools and is also a Python
37
+ module..
38
+
39
+ Installing this module installs the ``git-well`` command, which is a modal CLI
40
+ into several new git commands. These git commands are also exposed as
41
+ standalone "git" executables.
42
+
43
+ In other words after you:
44
+
45
+ .. code:: bash
46
+
47
+ pip install git-well
48
+
49
+
50
+ Then you can run
51
+
52
+ .. code:: bash
53
+
54
+ # Show all the commands exposed by this repo.
55
+ git well --help
56
+
57
+ git well sync --help
58
+
59
+ # OR
60
+
61
+ git sync
62
+
63
+
64
+ Top Level CLI:
65
+
66
+ .. code::
67
+
68
+ usage: git-well [-h] {branch_upgrade,squash_streaks,sync,branch_cleanup,track_upstream} ...
69
+
70
+ options:
71
+ -h, --help show this help message and exit
72
+
73
+ commands:
74
+ {branch_upgrade,squash_streaks,sync,branch_cleanup,track_upstream}
75
+ specify a command to run
76
+ branch_upgrade Upgrade to the latest "dev" branch. I.e. search for the branch
77
+ squash_streaks Squashes consecutive commits that meet a specified criteiron.
78
+ sync Sync a git repo with a remote server via ssh
79
+ branch_cleanup Cleanup branches that have been merged into main.
80
+ track_upstream Set the branch upstream with sensible defaults if possible.
81
+
82
+
83
+ The tools in this module are derived from:
84
+
85
+ * https://github.com/Erotemic/git-sync
86
+ * https://github.com/Erotemic/local/tree/main/git_tools
87
+
88
+
89
+
90
+ .. |Pypi| image:: https://img.shields.io/pypi/v/git_well.svg
91
+ :target: https://pypi.python.org/pypi/git_well
92
+
93
+ .. |PypiDownloads| image:: https://img.shields.io/pypi/dm/git_well.svg
94
+ :target: https://pypistats.org/packages/git_well
95
+
96
+ .. |GithubActions| image:: https://github.com/Erotemic/git_well/actions/workflows/tests.yml/badge.svg?branch=main
97
+ :target: https://github.com/Erotemic/git_well/actions?query=branch%3Amain
98
+
99
+ .. |Codecov| image:: https://codecov.io/github/Erotemic/git_well/badge.svg?branch=main&service=github
100
+ :target: https://codecov.io/github/Erotemic/git_well?branch=main
101
+
102
+
@@ -0,0 +1,71 @@
1
+ The git_well Module
2
+ ===================
3
+
4
+
5
+ |Pypi| |PypiDownloads| |GithubActions| |Codecov|
6
+
7
+ Git Well is a collection of git command line tools and is also a Python
8
+ module..
9
+
10
+ Installing this module installs the ``git-well`` command, which is a modal CLI
11
+ into several new git commands. These git commands are also exposed as
12
+ standalone "git" executables.
13
+
14
+ In other words after you:
15
+
16
+ .. code:: bash
17
+
18
+ pip install git-well
19
+
20
+
21
+ Then you can run
22
+
23
+ .. code:: bash
24
+
25
+ # Show all the commands exposed by this repo.
26
+ git well --help
27
+
28
+ git well sync --help
29
+
30
+ # OR
31
+
32
+ git sync
33
+
34
+
35
+ Top Level CLI:
36
+
37
+ .. code::
38
+
39
+ usage: git-well [-h] {branch_upgrade,squash_streaks,sync,branch_cleanup,track_upstream} ...
40
+
41
+ options:
42
+ -h, --help show this help message and exit
43
+
44
+ commands:
45
+ {branch_upgrade,squash_streaks,sync,branch_cleanup,track_upstream}
46
+ specify a command to run
47
+ branch_upgrade Upgrade to the latest "dev" branch. I.e. search for the branch
48
+ squash_streaks Squashes consecutive commits that meet a specified criteiron.
49
+ sync Sync a git repo with a remote server via ssh
50
+ branch_cleanup Cleanup branches that have been merged into main.
51
+ track_upstream Set the branch upstream with sensible defaults if possible.
52
+
53
+
54
+ The tools in this module are derived from:
55
+
56
+ * https://github.com/Erotemic/git-sync
57
+ * https://github.com/Erotemic/local/tree/main/git_tools
58
+
59
+
60
+
61
+ .. |Pypi| image:: https://img.shields.io/pypi/v/git_well.svg
62
+ :target: https://pypi.python.org/pypi/git_well
63
+
64
+ .. |PypiDownloads| image:: https://img.shields.io/pypi/dm/git_well.svg
65
+ :target: https://pypistats.org/packages/git_well
66
+
67
+ .. |GithubActions| image:: https://github.com/Erotemic/git_well/actions/workflows/tests.yml/badge.svg?branch=main
68
+ :target: https://github.com/Erotemic/git_well/actions?query=branch%3Amain
69
+
70
+ .. |Codecov| image:: https://codecov.io/github/Erotemic/git_well/badge.svg?branch=main&service=github
71
+ :target: https://codecov.io/github/Erotemic/git_well?branch=main
@@ -0,0 +1,11 @@
1
+ """
2
+ Basic
3
+ """
4
+ __version__ = '0.1.1'
5
+ __author__ = 'Jon Crall'
6
+ __author_email__ = 'erotemic@gmail.com'
7
+ __url__ = 'https://github.com/Erotemic/git_well/'
8
+
9
+ __mkinit__ = """
10
+ mkinit /home/joncrall/code/git_well/git_well/__init__.py
11
+ """
@@ -0,0 +1,7 @@
1
+ from .main import main
2
+ if __name__ == '__main__':
3
+ """
4
+ CommandLine:
5
+ python ~/code/git_well/git_well/__main__.py
6
+ """
7
+ main()
@@ -0,0 +1,72 @@
1
+ import ubelt as ub
2
+ from packaging.version import parse as Version
3
+
4
+
5
+ try:
6
+ from rich import print as rich_print
7
+ except Exception:
8
+ rich_print = print
9
+
10
+
11
+ def find_merged_branches(repo, main_branch='main'):
12
+ # git branch --merged main
13
+ # main_branch = 'main'
14
+ merged_branches = [p.strip() for p in repo.git.branch(merged=main_branch).split('\n') if p.strip()]
15
+ merged_branches = ub.oset(merged_branches) - {main_branch}
16
+ return merged_branches
17
+
18
+
19
+ def dev_branches(repo):
20
+ branch_infos = []
21
+ for line in repo.git.branch('-r').split('\n'):
22
+ line = line.strip().split('->')[-1].strip()
23
+ for remote in repo.remotes:
24
+ if line.startswith(remote.name):
25
+ info = {
26
+ 'remote': remote,
27
+ 'branch_name': line.lstrip(remote.name + '/'),
28
+ 'full_name': line,
29
+ }
30
+ branch_infos.append(info)
31
+
32
+ for branch in repo.branches:
33
+ info = {
34
+ 'remote': None,
35
+ 'branch': branch,
36
+ 'branch_name': branch.name,
37
+ 'datetime': branch.commit.committed_datetime,
38
+ }
39
+ branch_infos.append(info)
40
+
41
+ dev_infos = []
42
+ for info in branch_infos:
43
+ if info['branch_name'].startswith('dev/'):
44
+ vstr = info['branch_name'].split('/')[-1]
45
+ try:
46
+ info['version'] = Version(vstr)
47
+ except Exception:
48
+ ...
49
+ else:
50
+ # if not isinstance(info['version'], LegacyVersion):
51
+ dev_infos.append(info)
52
+
53
+ versioned_dev_branches = sorted(dev_infos, key=lambda x: x['version'])
54
+ return versioned_dev_branches
55
+
56
+
57
+ def confirm(msg):
58
+ try:
59
+ from rich import prompt
60
+ ret = prompt.Confirm.ask('Remove dev branches?')
61
+ except ImportError:
62
+ while True:
63
+ ans = input(msg + ' [y/n]')
64
+ if ans in {'y', 'yes'}:
65
+ ret = True
66
+ break
67
+ elif ans in {'n', 'no'}:
68
+ ret = False
69
+ break
70
+ else:
71
+ print('invalid response')
72
+ return ret
@@ -0,0 +1,49 @@
1
+
2
+ def make_dummy_git_repo():
3
+ import ubelt as ub
4
+ repo_dpath = ub.Path.appdir('git_well', 'tests', 'dummy-git-repo')
5
+ repo_dpath.delete().ensuredir()
6
+
7
+ import git
8
+ repo = git.Repo.init(repo_dpath, initial_branch='main')
9
+
10
+ repo.git.config('user.email', 'demo.user@zombo.com', local=True)
11
+ repo.git.config('user.name', 'Demo User', local=True)
12
+
13
+ fpath = (repo_dpath / 'data1.txt')
14
+ fpath.write_text('data')
15
+
16
+ repo.git.add(fpath)
17
+ repo.git.commit(a=True, m="Add Data1")
18
+
19
+ for idx in range(5):
20
+ fpath.write_text(f'data{idx}')
21
+ repo.git.commit(a=True, m="wip")
22
+
23
+ repo.git.checkout(b='branch1')
24
+ fpath = (repo_dpath / 'data2.txt')
25
+ for idx in range(5):
26
+ fpath.write_text(f'data{idx}')
27
+ repo.git.add(fpath)
28
+ repo.git.commit(a=True, m="wip")
29
+
30
+ repo.git.checkout('main')
31
+
32
+ repo.git.checkout(b='branch2')
33
+ fpath = (repo_dpath / 'data3.txt')
34
+ for idx in range(5):
35
+ fpath.write_text(f'data{idx}')
36
+ repo.git.add(fpath)
37
+ repo.git.commit(a=True, m="wip")
38
+
39
+ repo.git.checkout('main')
40
+ repo.git.merge('branch1')
41
+ repo.git.merge('branch2')
42
+
43
+ fpath = (repo_dpath / 'data3.txt')
44
+ for idx in range(10):
45
+ fpath.write_text(f'data{idx}')
46
+ repo.git.add(fpath)
47
+ repo.git.commit(a=True, m="wip")
48
+
49
+ return repo_dpath
@@ -0,0 +1,64 @@
1
+ #!/usr/bin/env python
2
+ import git
3
+ import ubelt as ub
4
+ import scriptconfig as scfg
5
+ from git_well._utils import find_merged_branches, dev_branches, rich_print
6
+
7
+
8
+ class CleanDevBranchConfig(scfg.DataConfig):
9
+ """
10
+ Cleanup branches that have been merged into main.
11
+ """
12
+ __command__ = 'branch_cleanup'
13
+
14
+ repo_dpath = scfg.Value('.', help='location of the repo')
15
+ keep_last = scfg.Value(1, help='previous number of dev branches to keep')
16
+ remove_merged = scfg.Value(False, isflag=True, help='if True, remove other merged branhes as well')
17
+
18
+ @classmethod
19
+ def main(cls, cmdline=1, **kwargs):
20
+ """
21
+ import sys, ubelt
22
+ sys.path.append(ubelt.expandpath('~/local/git_tools'))
23
+ from git_devbranch import * # NOQA
24
+ cls = CleanDevBranchConfig
25
+ cmdline = 0
26
+ kwargs = {}
27
+ """
28
+ config = cls.cli(cmdline=cmdline, data=kwargs)
29
+ rich_print('config = {}'.format(ub.urepr(config, nl=1)))
30
+ keep_last = config.keep_last
31
+ resolved_repo = ub.Path(config.repo_dpath).resolve()
32
+ print(f'resolved_repo={resolved_repo}')
33
+ repo = git.Repo(resolved_repo)
34
+
35
+ # TODO: fix * prefixed in front of branch
36
+ versioned_dev_branches = dev_branches(repo)
37
+ local_dev_branches = [b for b in versioned_dev_branches if b['remote'] is None]
38
+ versioned_branch_names = list(ub.unique([b['branch_name'] for b in local_dev_branches]))
39
+ remove_branches = versioned_branch_names[0:-keep_last]
40
+
41
+ try:
42
+ merged_branches = find_merged_branches(repo, 'main')
43
+ except Exception:
44
+ merged_branches = find_merged_branches(repo, 'origin/main')
45
+ remove_branches = list(ub.oset(remove_branches) | ub.oset(merged_branches) - {'release'})
46
+
47
+ print('remove_branches = {}'.format(ub.repr2(remove_branches, nl=1)))
48
+ if not remove_branches:
49
+ print('Local devbranches are already clean')
50
+ else:
51
+ from ._utils import confirm
52
+ if confirm('Remove dev branches?'):
53
+ repo.git.branch(*remove_branches, '-D')
54
+
55
+ __cli__ = CleanDevBranchConfig
56
+ main = __cli__.main
57
+
58
+
59
+ if __name__ == '__main__':
60
+ """
61
+ CommandLine:
62
+ python ~/code/git_well/git_well/git_branch_cleanup.py
63
+ """
64
+ __cli__.main()
@@ -0,0 +1,89 @@
1
+ #!/usr/bin/env python
2
+ """
3
+ A git tool for handling the dev/<version> branch patterns
4
+
5
+ See the GitDevbranchConfig for functionality
6
+
7
+ Notes:
8
+ to remove branches from remotes
9
+
10
+ f"git push origin --delete {branch_name}"
11
+
12
+ to see results:
13
+
14
+ git fetch --prune
15
+
16
+ Requires:
17
+ scriptconfig
18
+ git-python
19
+ packaging
20
+ ubelt
21
+ """
22
+ import git
23
+ import ubelt as ub
24
+ import scriptconfig as scfg
25
+ from git_well._utils import dev_branches, rich_print
26
+
27
+
28
+ class UpdateDevBranch(scfg.DataConfig):
29
+ """
30
+ Upgrade to the latest "dev" branch. I.e. search for the branch
31
+ ``dev/<version>`` with the greatest semantic version.
32
+ """
33
+ __command__ = 'branch_upgrade'
34
+ repo_dpath = scfg.Value('.', position=1, help='location of the repo')
35
+
36
+ @classmethod
37
+ def main(cls, cmdline=1, **kwargs):
38
+ config = cls.cli(cmdline=cmdline, data=kwargs)
39
+ rich_print('config = {}'.format(ub.urepr(config, nl=1)))
40
+ repo = git.Repo(config['repo_dpath'])
41
+
42
+ versioned_dev_branches = dev_branches(repo)
43
+ if len(versioned_dev_branches) == 0:
44
+ raise Exception('There are no versioned branches')
45
+
46
+ version = max(versioned_dev_branches, key=lambda x: x['version'])['version']
47
+ final_cand = [d for d in versioned_dev_branches if d['version'] == version]
48
+
49
+ latest = None
50
+ for c in final_cand:
51
+ if c.get('branch', None) is not None:
52
+ latest = c['branch']
53
+
54
+ print('Remember to pull and fetch before running this command')
55
+ # Need to fetch from remote
56
+ if latest is None:
57
+ info = final_cand[-1]
58
+ print('info = {}'.format(ub.repr2(info, nl=1)))
59
+ print('Latest seems to be on a remote')
60
+ info['branch_name']
61
+ repo.git.checkout(info['branch_name'])
62
+ # raise NotImplementedError
63
+ else:
64
+ # dev_branches = [b for b in repo.branches if b.name.startswith('dev/')]
65
+ # branch_versions = sorted(dev_branches, key=lambda x: Version(x.name.split('/')[-1]))
66
+ # latest = branch_versions[-1]
67
+ try:
68
+ active_branch_name = repo.active_branch.name
69
+ except TypeError:
70
+ active_branch_name = None
71
+ if active_branch_name == latest.name:
72
+ print('Already on the latest dev branch')
73
+ else:
74
+ print('active_branch_name = {!r}'.format(active_branch_name))
75
+ print('latest = {!r}'.format(latest))
76
+ repo.git.checkout(latest.name)
77
+
78
+
79
+ main = UpdateDevBranch.main
80
+ __cli__ = UpdateDevBranch
81
+
82
+
83
+ if __name__ == '__main__':
84
+ """
85
+ CommandLine:
86
+ python ~/code/git_well/git_well/git_devbranch.py clean --remove_merged
87
+ python ~/local/git_tools/git_devbranch.py clean --remove_merged
88
+ """
89
+ main()