BASH

Automate Git Clone and Initial Setup

Streamline your development workflow by automating the cloning of a Git repository and performing initial setup tasks like dependency installation.

#!/bin/bash

REPO_URL=$1
TARGET_DIR=$2

if [ -z "$REPO_URL" ] || [ -z "$TARGET_DIR" ]; then
  echo "Usage: $0 <repository_url> <target_directory>"
  exit 1
fi

if [ -d "$TARGET_DIR" ]; then
  echo "Error: Directory '$TARGET_DIR' already exists. Aborting."
  exit 1
fi

echo "Cloning $REPO_URL into $TARGET_DIR..."
git clone "$REPO_URL" "$TARGET_DIR"

if [ $? -eq 0 ]; then
  echo "Repository cloned successfully."
  cd "$TARGET_DIR" || exit

  echo "Performing initial setup (e.g., installing dependencies)..."
  # Example: Install Node.js dependencies if package.json exists
  if [ -f "package.json" ]; then
    echo "npm install..."
    npm install
  fi

  # Example: Install Python dependencies if requirements.txt exists
  if [ -f "requirements.txt" ]; then
    echo "pip install..."
    pip install -r requirements.txt
  fi

  echo "Setup complete for $TARGET_DIR."
else
  echo "Error: Failed to clone repository."
  exit 1
fi
How it works: This script automates the process of cloning a Git repository and performing common initial setup tasks. It takes the repository URL and a target directory as arguments. After successfully cloning, it navigates into the new directory and conditionally runs dependency installation commands (e.g., `npm install` for Node.js projects, `pip install` for Python projects) based on the presence of specific configuration files. This helps in quickly setting up new development environments.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs