2017-09-17 1 views
3

Ich habe eine Liste der Release-Archive:Wie konvertiert Release Archive git Repo

MyProject-0.9.zip 
MyProject-1.0.zip 
MyProject-1.3.tar.gz 
MyProject-2.0.tar.gz 

Jedes enthält einen Ordner mit dem gleichen Namen wie das Archiv (ohne Dateierweiterung), die Quellen und Makefiles enthält und so weiter, wie folgt:

MyProject-0.9.zip 
- MyProject-0.9/ 
- MyProject-0.9/src/ 
- MyProject-0.9/src/mySrc.c 
- MyProject-0.9/Makefile 
- MyProject-0.9/LICENSE 

Ich möchte sie in ein Git Repo, ein Commit pro Archiv konvertieren.

Antwort

1

Dies ist eine Lösung mit bash.

  • verwendet das Release Archive Änderungsdatum als Autor-Datum der Commits
  • ein Release/Version Git-Tag jedes
  • unterstützt Formate verschiedenen Archiv
  • ermöglicht
  • für einfache Reinigung des Archivs Inhalt verpflichten fügt vor begehen

das Skript:

#!/bin/bash 
# author:  hoijui <[email protected]> 
# copyright: Copyright (c) 2017 hoijui 
# license  GNU General Public License version 3 
# description: Creates a git repository from a list of release archives. 
# example:  Before using the script, we should have this directory structure: 
#    * conv_dir/createGitRepoFromReleaseArchives # this script 
#    * conv_dir/MyProject-1.0.0.zip    # 1. release archive 
#    * conv_dir/MyProject-1.0.1.zip    # 2. release archive 
#    * conv_dir/MyProject-2.0.0.tar.gz   # 3. release archive 
#    * conv_dir/MyProject-2.1.0.tar.gz   # 4. release archive 
#    while each of the archives should contain a dir with the same name 
#    as the archive (without the file extension), for example: 
#    MyProject-1.0.0.zip: 
#    * MyProject-1.0.0/LICENSE 
#    * MyProject-1.0.0/Makefile 
#    * MyProject-1.0.0/src/mySrc.c 
#    We then simply run the script: 
#    > createGitRepoFromReleaseArchives 
#    after which we should end up with a dir "repo" containing the git repository. 

ORIG_DIR=`pwd` 

ARCHIVE_SUFFIX_REGEX="\.\(tar\|tar\.gz\|tgz\|zip\|tar\.bz2\|tbz2\)" 
ZIP_SUFFIX_REGEX=".*\.zip$" 

ARCHIVES="${1:-}" 
REPO_DIR="${2:-repo}" 
EXTRACT_DIR="/tmp/$(basename ${0})_extract_${RANDOM}" 

if [ -z "${ARCHIVES}" ] 
then 
    # if ARCHIVES is not set 
    # gather common archive files from the current directory 
    ARCHIVES="$(find . -regex ".*${ARCHIVE_SUFFIX_REGEX}" | sort --version-sort)" 
fi 

# Possibly remove files fond in the release archives 
# that we do not want in the git repo. 
# This gets called for each archive, 
# with PWD set to the extracted archives main dir. 
function cleanRepoDir() { 
    echo "cleaning repo dir ..." 
    #rm version.txt 
    # ... more commands 
} 

PROJECT_NAME="$(basename "${ARCHIVES%% *}" | sed -e 's/-.*//')" 

mkdir "${EXTRACT_DIR}" 

echo "" 
echo "project name: '${PROJECT_NAME}'" 
echo "extract dir: '${EXTRACT_DIR}'" 
echo "repo dir: '${REPO_DIR}'" 
echo -e "version archives:\n${ARCHIVES}" 
echo "" 

read -p "Do you want to continue creating a git repo with the above info! [Y/n]" -n 1 -r 
echo 
if [[ ${REPLY} =~ ^[Nn]$ ]] 
then 
    # handle exits from shell(-script) or function 
    # but do not exit interactive shell 
    [[ "$0" = "${BASH_SOURCE}" ]] && exit 1 || return 1 
fi 

# setup the resulting repo 
if [ -e "${REPO_DIR}" ] 
then 
    >&2 echo "Error: Repo already exists!" 
    exit 1 
fi 
mkdir "${REPO_DIR}" 
cd "${REPO_DIR}" 
git init 
#git config --local user.name "My Name" 
#git config --local user.email [email protected] 
cd .. 

for archive in ${ARCHIVES} 
do 
    echo 

    # get the absolute path to the current archive 
    archive="$(realpath "${archive}")" 

    # remove extracted contents of the last archive 
    cd "${EXTRACT_DIR}" 
    rm -Rf ./* 

    # extract contents of the current archive 
    if [[ "${archive}" =~ ${ZIP_SUFFIX_REGEX} ]] 
    then 
     echo "Extracting (unzip): '${archive}'" 
     unzip -q "${archive}" 
    else 
     echo "Extracting (tar): '${archive}'" 
     tar -xf "${archive}" 
    fi 
    # and create the absolute path to the contained directory 
    # (which should be "${PROJECT_NAME}-${version}") 
    version_dir="${EXTRACT_DIR}/$(basename "${archive}" | sed -e "s/${ARCHIVE_SUFFIX_REGEX}$//")" 
    #version_dir="${EXTRACT_DIR}/$(basename "${archive}" | sed -r -e 's/\.zip$//')" 
    #version_dir="${EXTRACT_DIR}/$(basename "${archive}" | sed -r -e 's/\.(zip|zap)$//')" 
    # remove directory and project name from extracted archive path 
    # to get the bare version string 
    version="$(basename "${version_dir}" | sed -e 's/^[^-]*-//')" 

    # remove all content from the previous commit from git 
    # for the next comit 
    cd "${ORIG_DIR}" 
    cd "${REPO_DIR}" 
    git rm -rfq . 2> /dev/null 
    # also remove empty directories 
    rm -Rf ./* 

    # move current versions content to the git repo 
    mv "${version_dir}"/* ./ 
    cleanRepoDir 
    # and add all of it for the next commit 
    git add . 

    # extract last modification date of the release archive file, 
    # which is to be used as commit date 
    date="$(date -r "${archive}" +'%Y-%m-%dT%H:%M:%S')" 

    # commit the current archives content 
    git commit -m "${PROJECT_NAME} release version ${version}" --date "${date}" --quiet 
    # and tag it as a release 
    git tag -m "${PROJECT_NAME} release version ${version}" "v${version}" 

    cd .. 
done 

# Compress the git history 
cd "${REPO_DIR}" 
git gc --aggressive 
echo "Git history size: $(du -s --si .git)" 

cd "${ORIG_DIR}"