Skip to content

Instantly share code, notes, and snippets.

@QuentinN42
Created August 15, 2023 22:56
Show Gist options
  • Save QuentinN42/320b56c149f7b30eefc94832c7395d41 to your computer and use it in GitHub Desktop.
Save QuentinN42/320b56c149f7b30eefc94832c7395d41 to your computer and use it in GitHub Desktop.
Sync your gitlab commits history with gihub.
#!/bin/sh
# Create a git repo on github
# Put this script inside
# Update USERNAME
# Run this script
# Push
PRE_COMMIT_MSG='auto: '
USERNAME='QuentinN42'
function modif(){
cat changes | grep -q "true" && echo "false" > changes || echo "true" > changes
}
todo="$(curl -s "https://gitlab.com/users/${USERNAME}/calendar.json" | jq -rc '. | to_entries | .[] | "\(.key)-\(range(.value)+1)"')"
already_done="$(git log --pretty='format:%s' | grep -E "^${PRE_COMMIT_MSG}" | sed "s/^${PRE_COMMIT_MSG}//")"
export IFS=$'\n'
for x in $todo;
do
echo "Processing ${x}"
if [[ ! $already_done =~ $x ]]; then
modif
git add changes
git commit -s -m "${PRE_COMMIT_MSG}${x}" --date="$(date -d "$(echo "${x}" | cut -d- -f-3)")"
fi
done
@toby-bro
Copy link

toby-bro commented Aug 2, 2024

Seemingly not working with sh as the function keyword is not POSIX compliant (cf bashism )

@toby-bro
Copy link

toby-bro commented Aug 2, 2024

And you might also want to update the git author date with GIT_COMMITER_DATE

export IFS=$'\n'
for x in $todo;
do
    echo "Processing ${x}"
    if [[ ! $already_done =~ $x ]]; then
        modif
        git add changes
        commit_date="$(echo "${x}" | cut -d- -f-3)"
        commit_message="${PRE_COMMIT_MSG}${x}"
        GIT_COMMITTER_DATE="$(date -d "${commit_date}" '+%Y-%m-%d %H:%M:%S')" git commit -s -m "${commit_message}" --date "$(date -d "${commit_date}" '+%Y-%m-%d %H:%M:%S')"
    fi
done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment