I have written some powershell code, since I wanted to achive two things. The first one was to organize my gists and the second one to learn ps1 script syntax. Until now I guess I've managed some impressive things on one hand, on the other I got stuck...
I read lots of interesting and sophisticated articles on git and earlier on I also used .gitignore
files quite successfully.
Now this particular script does EVERYTIME Ignore the .gitignore
seamingly not dependend on what im doing.
May somebody of you help me busting this mystery why this script always ignores my gitignore - it sucks that everytime on the first hand it uploads itself everywhere (which is the reason why it writes itself to the .gitignore
in line: 40 ++
) and even more it gets wierd if everytime the node_modules
where uploaded too
(testing this script on a freshly generated npx create-react-app testapp
in the apps root folder)
By the way, to make things easier I also cleaned the cache with git rm -r --cached .
in line 88 before the git add .
this also does not work
Filename : git-create-repo.ps1
Sourcecode:
$workdir = Convert-Path $pwd
$gituser = "l00tz1ffer"
$gitserver = "lootziffers-welt.de"
$defaultRemoteName = "origin"
$targetBranchName = "master"
$gitHubExists = 0
$workDirName = $workdir.Substring(($workdir.LastIndexOf("\") + 1), ($workdir.Length - 1 ) - $workdir.LastIndexOf("\"))
$git_dir_string = $workDirName + ".git"
echo $workDirName
git remote -v | Out-File -FilePath remotes.temp -Encoding utf8 -Append
$File = 'remotes.temp'
foreach ($line in Get-Content $File) {
$remoteListingLine = $line
$remoteHostName = $remoteListingLine.Substring(($remoteListingLine.IndexOf("@") + 1), ($remoteListingLine.LastIndexOf(":") - 1 ) - $remoteListingLine.IndexOf("@"))
echo $remoteHostName
if ($remoteHostName -contains "github.com") {
echo "GitHub Repo found"
$remoteListingLine = $remoteListingLine -replace $defaultRemoteName, "github"
echo "Renaming properly ..."
echo $remoteListingLine
git remote rename $defaultRemoteName "github"
$gitHubExists = 1
}
}
Remove-Item 'remotes.temp'
if (-not (Test-Path -Path .gitignore)) {
New-Item -Path '.gitignore' -ItemType File
}
If ( $workDirName -ne "git-create-repo" -and $workDirName -ne "git-create-repo.git") {
$File = '.gitignore'
foreach ($line in Get-Content $File) {
if (-not (Test-Path -Path new.gitignore)) {
New-Item -Path 'new.gitignore' -ItemType File
}
echo ".gitignore enthält folgenden Wert: $line"
if ($line -contains "git-create-repo.ps1") {
echo "Duplicate entry found, Removing it"
}
elseif ($line.Length -eq 0) {
echo "Empty Line Found in .gitignore -> Removing it"
}
elseif ($line -contains $null) {
echo "Empty Line Found in .gitignore -> Removing it"
}
else {
line | Out-File -FilePath new.gitignore -Encoding utf8 -Append
}
}
Remove-Item '.gitignore'
Rename-Item 'new.gitignore' '.gitignore'
"git-create-repo.ps1" >> | Out-File -FilePath .gitignore -Encoding utf8 -Append
}
Start-Sleep -Seconds 3
git remote rm $defaultRemoteName
git branch -mv main $targetBranchName
ssh git@$gitserver "cd $gituser && mkdir $git_dir_string && cd $git_dir_string && git init --bare"
#if (Test-Path -Path '.git' -PathType Container) {
git init
echo "Lokales Repo wurde Initialisiert"
git rm -r --cached .
git add .\.gitignore
$timestamp = (get-date).ToString('G')
git commit -m "Autogenerated Commit from ${[System.Environment]::UserName} -> Zeit: $timestamp"
echo "Autogenerated Commit -> Zeit: $timestamp"
git rm -r --cached .
git add .
echo "Dateien wurden zum Lokalen Repository hinzugefuegt"
#}
$timestamp = (get-date).ToString('G')
git commit -m "Autogenerated Commit from ${[System.Environment]::UserName} -> Zeit: $timestamp"
echo "Autogenerated Commit -> Zeit: $timestamp"
$git_repo_string = "git@lootziffers-welt.de:" + $gituser + "/" + $workDirName + ".git"
echo "Der Verwendete Remote Git Repo string lautet: $git_repo_string"
git remote add $defaultRemoteName $git_repo_string
git push $defaultRemoteName $targetBranchName
if ($gitHubExists -eq 1) {
git push github $targetBranchName
}
scp git@${gitserver}:${gituser}/repos.txt repos.txt
if (-not (Test-Path -Path .gitignore)) {
New-Item -Path 'repos.txt' -ItemType File
}
$File = "repos.txt"
foreach ($line in Get-Content $File) {
echo "repos.txt enthält folgenden Wert: $line"
if ($line -contains $git_repo_string) {
echo "Duplicate entry found, Removing it"
}
elseif ($line.Length -eq 0) {
echo "Empty Line Found in .gitignore -> Removing it"
}
elseif ($line -contains $null) {
echo "Empty Line Found in .gitignore -> Removing it"
}
else {
line | Out-File -FilePath new.repos.txt -Encoding utf8 -Append
}
}
Remove-Item 'repos.txt'
Rename-Item 'new.repos.txt' 'repos.txt'
${git_repo_string}.ToString() | Out-File -FilePath repos.txt -Encoding utf8 -Append
scp repos.txt git@${gitserver}:${gituser}/repos.txt
Remove-Item 'repos.txt'
Start-Sleep -Seconds 5
Here i will give you a basic idea of how my .gitignore
file looks like
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
# dependencies
/node_modules/*
node_modules/
/node_modules
/.pnp
.pnp.js
# testing
/coverage
# production
/build
# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local
npm-debug.log*
yarn-debug.log*
yarn-error.log*
git-create-repo.ps1
Thank you my dear friends out there for your quick support and taking time to overview this huge bulk of code.
Sincierly
PowerShell 5, by default, appends text in ASCII and git expects your .gitignore
to be in UTF8.
You can solve this in 2 ways.
pwsh
instead of from powershell
. pwsh
will launch PowerShell core. PowerShell Core defaults to UTF-8."text" | Out-File -append -Encoding UTF8 -filepath .gitignore
instead of "text" >> .gitignore
.