Skip to content

Commit 0d6cdb3

Browse files
committed
Initial commit
0 parents  commit 0d6cdb3

File tree

4 files changed

+140
-0
lines changed

4 files changed

+140
-0
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2018 Alan Płócieniak
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# git-sync
2+
Git-Sync helps you sync your repositories across multiple remotes.
3+
4+
The main goal of this script is to create enough redundancy so even if a single provider will fail you can still access your code using alternative hosting companies.
5+
6+
## How to use
7+
### 1. Generate SSH key
8+
```bash
9+
cd c:\Users\Alan\.ssh\
10+
ssh-keygen.exe
11+
notepad.exe id_rsa.pub
12+
```
13+
14+
### 2. Add SSH keys to your alternate hosting websites
15+
Go to you repository hosting website and add key generated in previous step.
16+
17+
Here are some examples:
18+
19+
- `https://[USER_NAME].visualstudio.com/_details/security/keys`
20+
- `https://bitbucket.org/account/user/[USER_NAME]/ssh-keys/`
21+
- `https://github.com/settings/keys`
22+
23+
### 3. Create configuration
24+
- Rename `config-example.json` into `config.json`
25+
- Add your configuration
26+
27+
### 4. Run script
28+
29+
```powershell
30+
.\sync.ps1
31+
```
32+
33+
## License
34+
[MIT](LICENSE)

config-example.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"projects": [{
3+
"name": "MyProject",
4+
"url": "git@github.com:[USER_NAME]/[REPO_NAME].git",
5+
"dst": [{
6+
"name": "backup",
7+
"url": "git@bitbucket.org:[USER_NAME]/[REPO_NAME].git"
8+
}]
9+
}]
10+
}

sync.ps1

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
function Get-CurrentLocation {
2+
[CmdletBinding()]
3+
param ()
4+
5+
begin {
6+
}
7+
8+
process {
9+
(Get-Item .).FullName
10+
}
11+
12+
end {
13+
}
14+
}
15+
16+
Clear-Host
17+
$configuration = Get-Content -Raw -Path .\config.json | ConvertFrom-Json
18+
$rootDirectoryPath = Get-CurrentLocation
19+
$rootDirectoryPath = Join-Path $rootDirectoryPath "repo"
20+
21+
$configuration.projects | % {
22+
Set-Location $rootDirectoryPath
23+
Write-Host ""
24+
Write-Host "Processing: '$($_.name)'" -ForegroundColor Red
25+
26+
$repoPath = Join-Path $rootDirectoryPath $_.name
27+
if (Test-Path $repoPath) {
28+
Write-Host "Repository folder exists" -ForegroundColor Green
29+
30+
Set-Location $rootDirectoryPath
31+
Set-Location $repoPath
32+
Write-Host "Current Location $(Get-CurrentLocation)" -ForegroundColor Gray
33+
34+
Write-Host "Reseting status. . ." -ForegroundColor Yellow
35+
git reset --hard | Out-Null
36+
git clean -f -d | Out-Null
37+
38+
Write-Host "Fetching . . ." -ForegroundColor Yellow
39+
git fetch --tags
40+
41+
Write-Host "Pulling . . ." -ForegroundColor Yellow
42+
git pull
43+
}
44+
else {
45+
Write-Host "Repository does not exists" -ForegroundColor Red
46+
Write-Host "Clonning . . ." -ForegroundColor Yellow
47+
git clone $_.url
48+
}
49+
50+
Set-Location $rootDirectoryPath
51+
Set-Location $repoPath
52+
Write-Host "Current Location $(Get-CurrentLocation)" -ForegroundColor Gray
53+
54+
Write-Host "Validating remotes" -ForegroundColor Cyan
55+
$_.dst | % {
56+
$remoteName = $_.name
57+
$remoteUrl = $_.url
58+
Write-Host "`t$remoteName" -ForegroundColor Cyan -NoNewline
59+
60+
$remotes = git remote
61+
$remote = $remotes | ? { $_ -eq $remoteName } | Select-Object -First 1
62+
if ($remote) {
63+
Write-Host "[OK]" -ForegroundColor Green
64+
}
65+
else {
66+
Write-Host "[MISSING]" -ForegroundColor Red
67+
Write-Host "Adding remote" -ForegroundColor Yellow
68+
git remote add $remoteName $remoteUrl
69+
}
70+
71+
Write-Host "Pushing to backup . . ." -ForegroundColor Green
72+
git push -u $remoteName --all --force
73+
}
74+
}
75+
Set-Location $rootDirectoryPath

0 commit comments

Comments
 (0)