From 16ebb72f3de03fe5ce9551b39224edadaa953eb8 Mon Sep 17 00:00:00 2001 From: Jim Edwards Date: Tue, 6 Feb 2024 13:18:18 -0700 Subject: [PATCH] Squashed '.lib/git-fleximod/' changes from 69a5dc5ae..ac3e6ddc7 ac3e6ddc7 Bump to 0.6.2 612fa615c Merge pull request #13 from jedwards4b/add_fxhash cd74fd797 Merge pull request #14 from jedwards4b/fix/dir_path 459ee470f merge to main f456ca059 fix issue with sparse checkout dir path ad0a976b3 add support for fxhash variable in .gitmodules git-subtree-dir: .lib/git-fleximod git-subtree-split: ac3e6ddc7f1fddc3066d81a61aa7a042683315cb --- git_fleximod/cli.py | 2 +- git_fleximod/git_fleximod.py | 76 ++++++++++++++++++++++++++++-------- pyproject.toml | 2 +- tbump.toml | 2 +- 4 files changed, 63 insertions(+), 19 deletions(-) diff --git a/git_fleximod/cli.py b/git_fleximod/cli.py index 39e83b97c0..719b156206 100644 --- a/git_fleximod/cli.py +++ b/git_fleximod/cli.py @@ -1,7 +1,7 @@ from pathlib import Path import argparse -__version__ = "0.6.1" +__version__ = "0.6.2" def find_root_dir(filename=".git"): d = Path.cwd() diff --git a/git_fleximod/git_fleximod.py b/git_fleximod/git_fleximod.py index 9e4969d6c1..1b6c357b94 100755 --- a/git_fleximod/git_fleximod.py +++ b/git_fleximod/git_fleximod.py @@ -63,10 +63,12 @@ def commandline_arguments(args=None): ) -def submodule_sparse_checkout(root_dir, name, url, path, sparsefile, tag="master"): +def submodule_sparse_checkout( + root_dir, name, url, path, sparsefile, tag="master", fxhash=None +): # first create the module directory - if not os.path.isdir(path): - os.makedirs(path) + if not os.path.isdir(os.path.join(root_dir,path)): + os.makedirs(os.path.join(root_dir,path)) # Check first if the module is already defined # and the sparse-checkout file exists git = GitInterface(root_dir, logger) @@ -121,12 +123,19 @@ def submodule_sparse_checkout(root_dir, name, url, path, sparsefile, tag="master shutil.copy(os.path.join(root_dir, path, sparsefile), gitsparse) # Finally checkout the repo - sprepo_git.git_operation("fetch", "--depth=1", "origin", "--tags") - sprepo_git.git_operation("checkout", tag) - print(f"Successfully checked out {name}") + if fxhash: + sprepo_git.git_operation("fetch", "origin", "--tags") + sprepo_git.git_operation("checkout", fxhash) + print(f"Successfully checked out {name:>20} at {fxhash}") + else: + sprepo_git.git_operation("fetch", "--depth=1", "origin", "--tags") + sprepo_git.git_operation("checkout", tag) + print(f"Successfully checked out {name:>20} at {tag}") -def single_submodule_checkout(root, name, path, url=None, tag=None, force=False): +def single_submodule_checkout( + root, name, path, url=None, tag=None, force=False, fxhash=None +): git = GitInterface(root, logger) repodir = os.path.join(root, path) if os.path.exists(os.path.join(repodir, ".git")): @@ -146,9 +155,12 @@ def single_submodule_checkout(root, name, path, url=None, tag=None, force=False) url = url.replace("git@github.com:", "https://github.com") git.git_operation("clone", url, path) smgit = GitInterface(repodir, logger) - if not tag: + if not tag and not fxhash: tag = smgit.git_operation("describe", "--tags", "--always").rstrip() - smgit.git_operation("checkout", tag) + if fxhash: + smgit.git_operation("checkout", fxhash) + else: + smgit.git_operation("checkout", tag) # Now need to move the .git dir to the submodule location rootdotgit = os.path.join(root, ".git") if os.path.isfile(rootdotgit): @@ -190,6 +202,9 @@ def submodules_status(gitmodules, root_dir): for name in gitmodules.sections(): path = gitmodules.get(name, "path") tag = gitmodules.get(name, "fxtag") + fxhash = gitmodules.get(name, "fxhash") + if tag and fxhash: + utils.fatal_error(f"{name:>20} cannot have both fxtag and fxhash") if not path: utils.fatal_error("No path found in .gitmodules for {}".format(name)) newpath = os.path.join(root_dir, path) @@ -211,21 +226,44 @@ def submodules_status(gitmodules, root_dir): f"e {name:>20} not checked out, out of sync at tag {atag}, expected tag is {tag}" ) testfails += 1 + elif fxhash: + n = len(fxhash) + smhash = rootgit.git_operation( + "ls-tree", "--object-only", f"--abbrev={n}", "HEAD", path + ) + if smhash == fxhash: + print(f" {name:>20} not checked out, aligned at hash {fxhash}") + else: + print( + f"s {name:>20} not checked out, out of sync at hash {smhash}, expected hash is {fxhash}" + ) + testfails += 1 else: - print(f"e {name:>20} has no fxtag defined in .gitmodules") + print(f"e {name:>20} has no fxtag nor fxhash defined in .gitmodules") testfails += 1 else: with utils.pushd(newpath): git = GitInterface(newpath, logger) atag = git.git_operation("describe", "--tags", "--always").rstrip() + ahash = git.git_operation("status").partition("\n")[0].split()[-1] if tag and atag != tag: print(f"s {name:>20} {atag} is out of sync with .gitmodules {tag}") testfails += 1 elif tag: print(f" {name:>20} at tag {tag}") + elif fxhash: + rootgit = GitInterface(root_dir, logger) + n = len(fxhash) + if ahash.startswith(fxhash): + print(f" {name:>20} at hash {fxhash}") + else: + print( + f"s {name:>20} {ahash} is out of sync with .gitmodules {fxhash}" + ) + testfails += 1 else: print( - f"e {name:>20} has no tag defined in .gitmodules, module at {atag}" + f"e {name:>20} has no fxtag nor fxhash defined in .gitmodules, module at {atag}" ) testfails += 1 @@ -247,6 +285,7 @@ def submodules_update(gitmodules, root_dir, force): return for name in gitmodules.sections(): fxtag = gitmodules.get(name, "fxtag") + fxhash = gitmodules.get(name, "fxhash") path = gitmodules.get(name, "path") url = gitmodules.get(name, "url") logger.info("name={} path={} url={} fxtag={}".format(name, path, url, fxtag)) @@ -276,12 +315,14 @@ def submodules_update(gitmodules, root_dir, force): if fxtag and fxtag not in tags: git.git_operation("fetch", newremote, "--tags") atag = git.git_operation("describe", "--tags", "--always").rstrip() - if fxtag and fxtag != atag: print(f"{name:>20} updated to {fxtag}") git.git_operation("checkout", fxtag) - elif not fxtag: - print(f"No fxtag found for submodule {name:>20}") + elif fxhash: + print(f"{name:>20} updated to {fxhash}") + git.git_operation("checkout", fxhash) + elif not (fxtag or fxhash): + print(f"No fxtag nor fxhash found for submodule {name:>20}") else: print(f"{name:>20} up to date.") @@ -298,6 +339,7 @@ def submodules_checkout(gitmodules, root_dir, requiredlist, force=False): fxrequired = gitmodules.get(name, "fxrequired") fxsparse = gitmodules.get(name, "fxsparse") fxtag = gitmodules.get(name, "fxtag") + fxhash = gitmodules.get(name, "fxhash") path = gitmodules.get(name, "path") url = gitmodules.get(name, "url") @@ -312,14 +354,16 @@ def submodules_checkout(gitmodules, root_dir, requiredlist, force=False): root_dir, name, url, path, fxsparse, fxtag ) ) - submodule_sparse_checkout(root_dir, name, url, path, fxsparse, tag=fxtag) + submodule_sparse_checkout( + root_dir, name, url, path, fxsparse, tag=fxtag, fxhash=fxhash + ) else: logger.debug( "Calling submodule_checkout({},{},{})".format(root_dir, name, path) ) single_submodule_checkout( - root_dir, name, path, url=url, tag=fxtag, force=force + root_dir, name, path, url=url, tag=fxtag, force=force, fxhash=fxhash ) diff --git a/pyproject.toml b/pyproject.toml index 225f7b39b0..f5828a58e7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "git-fleximod" -version = "0.6.1" +version = "0.6.2" description = "Extended support for git-submodule and git-sparse-checkout" authors = ["Jim Edwards "] maintainers = ["Jim Edwards "] diff --git a/tbump.toml b/tbump.toml index 03b63f1540..583cef45ae 100644 --- a/tbump.toml +++ b/tbump.toml @@ -2,7 +2,7 @@ github_url = "https://github.com/jedwards4b/git-fleximod/" [version] -current = "0.6.1" +current = "0.6.2" # Example of a semver regexp. # Make sure this matches current_version before