Skip to content

Commit 8ef36d0

Browse files
authored
Merge pull request seeraven#62 from seeraven/feature/recurse-submodules
Support git clone options to checkout submodules during clone operation
2 parents 628b6ba + 71f9130 commit 8ef36d0

19 files changed

Lines changed: 181 additions & 66 deletions

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Upcoming Version
44

5+
- Support flags `--recursive`, `--recurse-submodules` and `--remote-submodules` of `git clone`.
6+
57
## v1.0.19
68

79
- Updated pip dependencies

src/git_cache/commands/checkout.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from ..command_execution import getstatusoutput, simple_call_command
2222
from ..database import Database
2323
from ..git_mirror import GitMirror
24+
from ..git_options import GitOptions
2425
from .helpers import get_mirror_url
2526

2627
# -----------------------------------------------------------------------------
@@ -33,7 +34,7 @@
3334
# Function Definitions
3435
# -----------------------------------------------------------------------------
3536
# pylint: disable=too-many-locals
36-
def git_checkout(git_options):
37+
def git_checkout(git_options: GitOptions) -> int:
3738
"""Handle a git checkout command.
3839
3940
Args:

src/git_cache/commands/cleanup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
# -----------------------------------------------------------------------------
3131
# Function Definitions
3232
# -----------------------------------------------------------------------------
33-
def git_cleanup():
33+
def git_cleanup() -> int:
3434
"""Handle a git cleanup command.
3535
3636
Return:

src/git_cache/commands/clone.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@
1717
# Module Import
1818
# -----------------------------------------------------------------------------
1919
import logging
20+
import os
21+
from typing import List
2022

2123
from ..command_execution import simple_call_command
2224
from ..git_mirror import GitMirror
25+
from ..git_options import GitOptions
2326
from .helpers import use_mirror_for_remote_url
2427

2528
# -----------------------------------------------------------------------------
@@ -31,10 +34,11 @@
3134
# -----------------------------------------------------------------------------
3235
# Function Definitions
3336
# -----------------------------------------------------------------------------
34-
def git_clone(git_options):
37+
def git_clone(called_as: List[str], git_options: GitOptions) -> int:
3538
"""Handle a git clone command.
3639
3740
Args:
41+
called_as (list): The arguments used for the command call.
3842
git_options (obj): The GitOptions object.
3943
4044
Return:
@@ -50,7 +54,24 @@ def git_clone(git_options):
5054
if mirror_path:
5155
if use_mirror_for_remote_url(remote_url):
5256
mirror = GitMirror(url=remote_url)
53-
return mirror.clone_from_mirror(git_options)
57+
retval = mirror.clone_from_mirror(git_options)
58+
if retval == 0:
59+
if ("--recurse-submodules" in git_options.command_options) or (
60+
"--recursive" in git_options.command_options
61+
):
62+
LOG.debug("Initializing submodules by calling 'git submodule update --init --recursive'.")
63+
if len(git_options.command_args) > 1:
64+
target_dir = git_options.command_args[1]
65+
else:
66+
target_dir = os.path.basename(mirror.url).replace(".git", "")
67+
command = called_as + git_options.global_options
68+
command += ["-C", target_dir]
69+
command += ["submodule", "update", "--init", "--recursive"]
70+
if "--remote-submodules" in git_options.command_options:
71+
command += ["--remote"]
72+
retval = simple_call_command(command)
73+
74+
return retval
5475

5576
LOG.debug("Remote URL does not match the UrlPatterns. Using original git command.")
5677
else:

src/git_cache/commands/delete.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
# Module Import
1818
# -----------------------------------------------------------------------------
1919
import logging
20+
from typing import List
2021

2122
from ..database import Database
2223
from ..git_mirror import GitMirror
@@ -30,7 +31,7 @@
3031
# -----------------------------------------------------------------------------
3132
# Function Definitions
3233
# -----------------------------------------------------------------------------
33-
def git_delete_mirror(mirror_list):
34+
def git_delete_mirror(mirror_list: List[str]) -> int:
3435
"""Handle a git delete-mirror command.
3536
3637
Args:

src/git_cache/commands/lfs_fetch.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from ..command_execution import simple_call_command
2222
from ..database import Database
2323
from ..git_mirror import GitMirror
24+
from ..git_options import GitOptions
2425
from .helpers import get_current_ref, get_mirror_url
2526

2627
# -----------------------------------------------------------------------------
@@ -33,7 +34,7 @@
3334
# Function Definitions
3435
# -----------------------------------------------------------------------------
3536
# pylint: disable=too-many-locals
36-
def git_lfs_fetch(git_options):
37+
def git_lfs_fetch(git_options: GitOptions) -> int:
3738
"""Handle a git lfs fetch command.
3839
3940
Args:

src/git_cache/commands/lfs_pull.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from ..command_execution import simple_call_command
2222
from ..database import Database
2323
from ..git_mirror import GitMirror
24+
from ..git_options import GitOptions
2425
from .helpers import get_current_ref, get_mirror_url
2526

2627
# -----------------------------------------------------------------------------
@@ -32,7 +33,7 @@
3233
# -----------------------------------------------------------------------------
3334
# Function Definitions
3435
# -----------------------------------------------------------------------------
35-
def git_lfs_pull(git_options):
36+
def git_lfs_pull(git_options: GitOptions) -> int:
3637
"""Handle a git lfs pull command.
3738
3839
Args:

src/git_cache/commands/ls_remote.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from ..config import Config
2323
from ..database import Database
2424
from ..git_mirror import GitMirror
25+
from ..git_options import GitOptions
2526
from .helpers import get_mirror_url, use_mirror_for_remote_url
2627

2728
# -----------------------------------------------------------------------------
@@ -34,7 +35,7 @@
3435
# Function Definitions
3536
# -----------------------------------------------------------------------------
3637
# pylint: disable=too-many-locals
37-
def git_ls_remote(git_options):
38+
def git_ls_remote(git_options: GitOptions) -> int:
3839
"""Handle a git ls-remote command.
3940
4041
The git ls-remote command updates the mirror if no repository is specified
@@ -48,6 +49,7 @@ def git_ls_remote(git_options):
4849
Returns 0 on success, otherwise the return code of the last failed
4950
command.
5051
"""
52+
assert git_options.command is not None, "Internal error: git command is NOT ls-remote!"
5153
config = Config()
5254
repository = None
5355
mirror_url = None

src/git_cache/commands/pull.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from ..config import Config
2323
from ..database import Database
2424
from ..git_mirror import GitMirror
25+
from ..git_options import GitOptions
2526
from .helpers import get_current_ref, get_mirror_url
2627

2728
# -----------------------------------------------------------------------------
@@ -34,7 +35,7 @@
3435
# Function Definitions
3536
# -----------------------------------------------------------------------------
3637
# pylint: disable=too-many-locals
37-
def git_pull(git_options):
38+
def git_pull(git_options: GitOptions) -> int:
3839
"""Handle a git pull command.
3940
4041
Args:

src/git_cache/commands/submodule_init.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import logging
2020

2121
from ..command_execution import getstatusoutput, simple_call_command
22+
from ..git_options import GitOptions
2223
from .helpers import get_mirror_url, get_pull_url
2324

2425
# -----------------------------------------------------------------------------
@@ -30,7 +31,7 @@
3031
# -----------------------------------------------------------------------------
3132
# Function Definitions
3233
# -----------------------------------------------------------------------------
33-
def git_submodule_init(git_options):
34+
def git_submodule_init(git_options: GitOptions) -> int:
3435
"""Handle a git submodule init command.
3536
3637
The 'submodule init' command initializes the git configuration for the

0 commit comments

Comments
 (0)