Skip to content

Instantly share code, notes, and snippets.

@hithismani
Last active March 30, 2024 08:49
Show Gist options
  • Save hithismani/e9abfcc5a995cb2fd01b9708643a21d1 to your computer and use it in GitHub Desktop.
Save hithismani/e9abfcc5a995cb2fd01b9708643a21d1 to your computer and use it in GitHub Desktop.
Folder structure -> CSV generator, for helping you setup projects in a particular folder hierarchy and migrate the setup across devices.

Folder Hierarchy Sync-er

Description

This project consists of a pair of Bash scripts designed to help maintain consistent folder structures across different devices when moving projects. This is particularly useful for developers and professionals who frequently transition between working on portable devices and desktop environments, ensuring that project directories remain organized and identical across all devices.

Problem Statement

Maintaining identical folder structures for projects across multiple devices can be a challenging task, especially when transitioning work between portable devices and desktops. This becomes a problem specifically when your device from scratch again. This script aims to streamline this process by automating the synchronization of git repository-based project folders.

How It Works

The solution is divided into two main scripts:

  1. Export Script: This script searches for .git directories within a given folder hierarchy, extracts the URLs of their remote origins, and saves them along with their paths to a CSV file. This effectively creates a snapshot of your project directories and their associated git repositories.

  2. Import Script: This script reads the previously generated CSV file and reconstructs the directory structure on a different device by cloning the repositories from their saved URLs into their corresponding paths.

Installation

  • No installation is required, but you must have git and bash installed on your device. These scripts should work on any Unix-like operating system, including Linux and macOS.
  • Assumptions: You've already set up SSH keys and have access to the repo you want to pull.

Usage

Exporting Folder Structure

  1. Navigate to the root directory from which you want to start the export.
  2. Run the export script:
    bash indexGenerator.bash
    This generates a file named git_repos.csv in the current directory, containing the paths and git URLs of your projects.

Importing Folder Structure

  1. Copy the git_repos.csv file to the root directory on the new device where you want to import the folder structure.
  2. Run the import script:
    bash indexPuller.bash
    The script will recreate the directory structure and clone the git repositories accordingly.

Features

  • Easy synchronization of project folder structures across devices.
  • Automation of tedious manual folder and repository setup.
  • Ensures a seamless transition between working environments.
  • You can back up the bash files and the generated csv document by indexGenerator.bash in any cloud storage of your choice.
  • Edit either bash file yourself, incase you'd like to extend the features yourself.

Contributing

Contributions are welcome! If you have suggestions for improvements or bug fixes feel free to make changes.

Disclaimer

This software is provided "as is," without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose, and non-infringement. In no event shall the authors or copyright holders be liable for any claim, damages, or other liability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with the software or the use or other dealings in the software.

Use of this script is at your own risk. While it has been designed with care and with the intention of being useful, the responsibility for ensuring its proper function and for safeguarding data lies with the user. It's recommended to always have backups of important data and to review scripts before execution to ensure they meet your needs and security standards.

#!/bin/bash
# Initialize an empty array
repos=()
# Use find to recursively search for .git directories
while IFS= read -r -d '' dir; do
# Get the parent directory of the .git directory
parentdir=$(dirname "$dir")
# Change to the parent directory
cd "$parentdir"
# Get the URL of the remote repository
url=$(git config --get remote.origin.url)
# Change back to the original directory
cd - > /dev/null
# Add the directory and URL to the array
repos+=("$parentdir,$url")
done < <(find . -name ".git" -type d -print0)
# Write the array to a file in the current directory
printf "%s\n" "${repos[@]}" > ./git_repos.csv
#!/bin/bash
# Read the CSV file line by line
while IFS=, read -r path url; do
# Create the directory if it doesn't exist
mkdir -p "$path"
# Change to the directory
cd "$path"
# Check if the directory is a git repository
if ! git rev-parse --is-inside-work-tree > /dev/null 2>&1; then
# Clone the repository
git clone "$url" .
fi
# Change back to the original directory
cd - > /dev/null
done < git_repos.csv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment