Added port handling
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
|
import sys
|
||||||
import yaml
|
import yaml
|
||||||
from pkgmgr.generate_alias import generate_alias
|
from pkgmgr.generate_alias import generate_alias
|
||||||
from pkgmgr.save_user_config import save_user_config
|
from pkgmgr.save_user_config import save_user_config
|
||||||
@@ -8,36 +9,48 @@ def create_repo(identifier, config_merged, user_config_path, bin_dir, remote=Fal
|
|||||||
"""
|
"""
|
||||||
Creates a new repository by performing the following steps:
|
Creates a new repository by performing the following steps:
|
||||||
|
|
||||||
1. Parses the identifier (provider/account/repository) and adds a new entry to the user config
|
1. Parses the identifier (provider:port/account/repository) and adds a new entry to the user config
|
||||||
if it is not already present.
|
if it is not already present. The provider part is split into provider and port (if provided).
|
||||||
2. Creates the local repository directory and initializes a Git repository.
|
2. Creates the local repository directory and initializes a Git repository.
|
||||||
3. If --remote is set, adds a remote, creates an initial commit (e.g. with a README.md), and pushes to remote.
|
3. If --remote is set, checks for an existing "origin" remote (removing it if found),
|
||||||
|
adds the remote using a URL built from provider, port, account, and repository,
|
||||||
|
creates an initial commit (e.g. with a README.md), and pushes to the remote.
|
||||||
|
The push is attempted on both "main" and "master" branches.
|
||||||
"""
|
"""
|
||||||
parts = identifier.split("/")
|
parts = identifier.split("/")
|
||||||
if len(parts) != 3:
|
if len(parts) != 3:
|
||||||
print("Identifier must be in the format 'provider/account/repository'.")
|
print("Identifier must be in the format 'provider:port/account/repository' (port is optional).")
|
||||||
return
|
return
|
||||||
|
|
||||||
provider, account, repository = parts
|
provider_with_port, account, repository = parts
|
||||||
|
# Split provider and port if a colon is present.
|
||||||
|
if ":" in provider_with_port:
|
||||||
|
provider_name, port = provider_with_port.split(":", 1)
|
||||||
|
else:
|
||||||
|
provider_name = provider_with_port
|
||||||
|
port = None
|
||||||
|
|
||||||
# Check if the repository is already present in the merged config
|
# Check if the repository is already present in the merged config (including port)
|
||||||
exists = False
|
exists = False
|
||||||
for repo in config_merged.get("repositories", []):
|
for repo in config_merged.get("repositories", []):
|
||||||
if repo.get("provider") == provider and repo.get("account") == account and repo.get("repository") == repository:
|
if (repo.get("provider") == provider_name and
|
||||||
|
repo.get("account") == account and
|
||||||
|
repo.get("repository") == repository):
|
||||||
exists = True
|
exists = True
|
||||||
print(f"Repository {identifier} already exists in the configuration.")
|
print(f"Repository {identifier} already exists in the configuration.")
|
||||||
break
|
break
|
||||||
|
|
||||||
if not exists:
|
if not exists:
|
||||||
# Create a new entry with an automatically generated alias
|
# Create a new entry with an automatically generated alias.
|
||||||
new_entry = {
|
new_entry = {
|
||||||
"provider": provider,
|
"provider": provider_name,
|
||||||
|
"port": port,
|
||||||
"account": account,
|
"account": account,
|
||||||
"repository": repository,
|
"repository": repository,
|
||||||
"alias": generate_alias({"repository": repository, "provider": provider, "account": account}, bin_dir, existing_aliases=set()),
|
"alias": generate_alias({"repository": repository, "provider": provider_name, "account": account}, bin_dir, existing_aliases=set()),
|
||||||
"verified": {} # No initial verification info
|
"verified": {} # No initial verification info
|
||||||
}
|
}
|
||||||
# Load or initialize the user configuration
|
# Load or initialize the user configuration.
|
||||||
if os.path.exists(user_config_path):
|
if os.path.exists(user_config_path):
|
||||||
with open(user_config_path, "r") as f:
|
with open(user_config_path, "r") as f:
|
||||||
user_config = yaml.safe_load(f) or {}
|
user_config = yaml.safe_load(f) or {}
|
||||||
@@ -47,19 +60,19 @@ def create_repo(identifier, config_merged, user_config_path, bin_dir, remote=Fal
|
|||||||
user_config["repositories"].append(new_entry)
|
user_config["repositories"].append(new_entry)
|
||||||
save_user_config(user_config, user_config_path)
|
save_user_config(user_config, user_config_path)
|
||||||
print(f"Repository {identifier} added to the configuration.")
|
print(f"Repository {identifier} added to the configuration.")
|
||||||
# Also update the merged configuration object
|
# Also update the merged configuration object.
|
||||||
config_merged.setdefault("repositories", []).append(new_entry)
|
config_merged.setdefault("repositories", []).append(new_entry)
|
||||||
|
|
||||||
# Create the local repository directory based on the configured base directory
|
# Create the local repository directory based on the configured base directory.
|
||||||
base_dir = os.path.expanduser(config_merged["directories"]["repositories"])
|
base_dir = os.path.expanduser(config_merged["directories"]["repositories"])
|
||||||
repo_dir = os.path.join(base_dir, provider, account, repository)
|
repo_dir = os.path.join(base_dir, provider_name, account, repository)
|
||||||
if not os.path.exists(repo_dir):
|
if not os.path.exists(repo_dir):
|
||||||
os.makedirs(repo_dir, exist_ok=True)
|
os.makedirs(repo_dir, exist_ok=True)
|
||||||
print(f"Local repository directory created: {repo_dir}")
|
print(f"Local repository directory created: {repo_dir}")
|
||||||
else:
|
else:
|
||||||
print(f"Local repository directory already exists: {repo_dir}")
|
print(f"Local repository directory already exists: {repo_dir}")
|
||||||
|
|
||||||
# Initialize a Git repository if not already initialized
|
# Initialize a Git repository if not already initialized.
|
||||||
if not os.path.exists(os.path.join(repo_dir, ".git")):
|
if not os.path.exists(os.path.join(repo_dir, ".git")):
|
||||||
cmd_init = "git init"
|
cmd_init = "git init"
|
||||||
if preview:
|
if preview:
|
||||||
@@ -71,7 +84,7 @@ def create_repo(identifier, config_merged, user_config_path, bin_dir, remote=Fal
|
|||||||
print("Git repository is already initialized.")
|
print("Git repository is already initialized.")
|
||||||
|
|
||||||
if remote:
|
if remote:
|
||||||
# Create a README.md if it does not exist to have content for an initial commit
|
# Create a README.md if it does not exist to have content for an initial commit.
|
||||||
readme_path = os.path.join(repo_dir, "README.md")
|
readme_path = os.path.join(repo_dir, "README.md")
|
||||||
if not os.path.exists(readme_path):
|
if not os.path.exists(readme_path):
|
||||||
if preview:
|
if preview:
|
||||||
@@ -82,8 +95,47 @@ def create_repo(identifier, config_merged, user_config_path, bin_dir, remote=Fal
|
|||||||
subprocess.run("git add README.md", cwd=repo_dir, shell=True, check=True)
|
subprocess.run("git add README.md", cwd=repo_dir, shell=True, check=True)
|
||||||
subprocess.run('git commit -m "Initial commit"', cwd=repo_dir, shell=True, check=True)
|
subprocess.run('git commit -m "Initial commit"', cwd=repo_dir, shell=True, check=True)
|
||||||
print("README.md created and initial commit made.")
|
print("README.md created and initial commit made.")
|
||||||
# Add a remote named "origin"
|
|
||||||
remote_url = f"git@{provider}:{account}/{repository}.git"
|
# Build the remote URL.
|
||||||
|
if provider_name.lower() == "github.com":
|
||||||
|
remote_url = f"git@{provider_name}:{account}/{repository}.git"
|
||||||
|
else:
|
||||||
|
if port:
|
||||||
|
remote_url = f"ssh://git@{provider_name}:{port}/{account}/{repository}.git"
|
||||||
|
else:
|
||||||
|
remote_url = f"ssh://git@{provider_name}/{account}/{repository}.git"
|
||||||
|
|
||||||
|
# Check if the remote "origin" already exists.
|
||||||
|
cmd_list = "git remote"
|
||||||
|
if preview:
|
||||||
|
print(f"[Preview] Would check for existing remotes in {repo_dir}")
|
||||||
|
remote_exists = False # Assume no remote in preview mode.
|
||||||
|
else:
|
||||||
|
result = subprocess.run(cmd_list, cwd=repo_dir, shell=True, capture_output=True, text=True, check=True)
|
||||||
|
remote_list = result.stdout.strip().split()
|
||||||
|
remote_exists = "origin" in remote_list
|
||||||
|
|
||||||
|
if remote_exists:
|
||||||
|
# Remove the existing remote "origin".
|
||||||
|
cmd_remove = "git remote remove origin"
|
||||||
|
if preview:
|
||||||
|
print(f"[Preview] Would execute: '{cmd_remove}' in {repo_dir}")
|
||||||
|
else:
|
||||||
|
subprocess.run(cmd_remove, cwd=repo_dir, shell=True, check=True)
|
||||||
|
print("Existing remote 'origin' removed.")
|
||||||
|
|
||||||
|
# Now add the new remote.
|
||||||
|
cmd_remote = f"git remote add origin {remote_url}"
|
||||||
|
if preview:
|
||||||
|
print(f"[Preview] Would execute: '{cmd_remote}' in {repo_dir}")
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
subprocess.run(cmd_remote, cwd=repo_dir, shell=True, check=True)
|
||||||
|
print(f"Remote 'origin' added: {remote_url}")
|
||||||
|
except subprocess.CalledProcessError:
|
||||||
|
print(f"Failed to add remote using URL: {remote_url}. Exiting.")
|
||||||
|
sys.exit(2)
|
||||||
|
|
||||||
cmd_remote = f"git remote add origin {remote_url}"
|
cmd_remote = f"git remote add origin {remote_url}"
|
||||||
if preview:
|
if preview:
|
||||||
print(f"[Preview] Would execute: '{cmd_remote}' in {repo_dir}")
|
print(f"[Preview] Would execute: '{cmd_remote}' in {repo_dir}")
|
||||||
|
|||||||
Reference in New Issue
Block a user