-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add sync command * Add release workflow
- Loading branch information
1 parent
2c30719
commit d102633
Showing
11 changed files
with
259 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# .github/workflows/release.yaml | ||
|
||
on: | ||
release: | ||
types: [created] | ||
|
||
permissions: | ||
contents: write | ||
packages: write | ||
|
||
jobs: | ||
releases-matrix: | ||
name: Release Go Binary | ||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
# build and publish in parallel: linux/386, linux/amd64, linux/arm64, windows/386, windows/amd64, darwin/amd64, darwin/arm64 | ||
goos: [linux, windows, darwin] | ||
goarch: ["386", amd64, arm64] | ||
exclude: | ||
- goarch: "386" | ||
goos: darwin | ||
- goarch: arm64 | ||
goos: windows | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: wangyoucao577/go-release-action@v1 | ||
with: | ||
github_token: ${{ secrets.GITHUB_TOKEN }} | ||
goos: ${{ matrix.goos }} | ||
goarch: ${{ matrix.goarch }} | ||
binary_name: "syncommit" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"syncommit/structs" | ||
"syncommit/utils" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
func init() { | ||
rootCmd.AddCommand(syncCommand) | ||
} | ||
|
||
var syncCommand = &cobra.Command{ | ||
Use: "sync", | ||
Short: "sync all commits to private repo", | ||
Long: `commits and pushes all your commits in the current project to github.`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
repo := structs.GetRepoAtPath(".") | ||
allCommits := repo.GetRepoCommitsForCurrentAuthor() | ||
syncRepo := structs.GetRepoAtPath(structs.RepoPath) | ||
syncedCommits := syncRepo.GetRepoCommitsForCurrentAuthor() | ||
commitsToSync := utils.FilterSyncedCommits(allCommits, syncedCommits) | ||
for _, commit := range commitsToSync { | ||
err := commit.Commit(branchName, repo.Name) | ||
if err != nil { | ||
log.Fatal("failed to sync repo.\nError: ", err.Error()) | ||
} | ||
} | ||
err := syncRepo.Push() | ||
if err != nil { | ||
log.Fatal("failed to push. ", err.Error()) | ||
} | ||
fmt.Println("sync commits pushed successfully.") | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package structs | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
"os/exec" | ||
"strings" | ||
"time" | ||
) | ||
|
||
type Commit struct { | ||
Hash string | ||
Message string | ||
Time time.Time | ||
} | ||
|
||
func (c *Commit) generateCommitMessage(repoName, branchName string) string { | ||
return fmt.Sprintf("hash: %s %s on branch: %s on repo: %s", c.Hash, c.Message, strings.TrimSpace(repoName), strings.TrimSpace(branchName)) | ||
} | ||
|
||
func (c *Commit) Commit(repoName, branchName string) error { | ||
err := os.Chdir(RepoPath) | ||
if err != nil { | ||
log.Fatal("failed to cd into repo directory. ", err.Error()) | ||
} | ||
return exec.Command("git", "commit", "-m", fmt.Sprintf("%q", c.generateCommitMessage(repoName, branchName)), "--allow-empty", fmt.Sprintf("--date='%s'", c.Time.Format(time.RFC1123Z))).Run() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package structs | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"strconv" | ||
"strings" | ||
"time" | ||
) | ||
|
||
const RepoFileName = ".repo" | ||
const RepoLocation = "repo" | ||
|
||
var HomePath = os.Getenv("HOME") | ||
|
||
var ConfigFolderPath = filepath.Join(HomePath, "/.syncommit") | ||
var RepoPath = filepath.Join(ConfigFolderPath, RepoLocation) | ||
|
||
const hashPrefix = "hash: " | ||
|
||
type Repo struct { | ||
Name string | ||
Path string | ||
BranchName string | ||
CurrentAuthorName string | ||
CurrentAuthorEmail string | ||
} | ||
|
||
func (r *Repo) GetRepoCommitsForCurrentAuthor() (commits []Commit) { | ||
cmd := exec.Command("git", "--no-pager", "log", fmt.Sprintf("--author=%s", r.CurrentAuthorEmail), "--pretty=format:%h$//%s$//%ad", "--date=unix", "--no-merges") | ||
commitsBytes, err := cmd.Output() | ||
if err != nil && err.Error() == "exit status 128" { | ||
// User has no commits | ||
return | ||
} | ||
if err != nil { | ||
log.Fatal("Failed to get commits for current author.\nError: ", err.Error()) | ||
} | ||
commitsString := strings.Split(string(commitsBytes), "\n") | ||
for _, commitString := range commitsString { | ||
if len(commitString) < 1 { | ||
continue | ||
} | ||
|
||
var commit Commit | ||
if r.Path != RepoPath { | ||
commit = parseGeneralRepoCommitString(commitString) | ||
} else { | ||
if !strings.Contains(commitString, hashPrefix) { | ||
continue | ||
} | ||
commit = parseSyncRepoCommitString(commitString) | ||
} | ||
commits = append(commits, commit) | ||
} | ||
return | ||
} | ||
|
||
func parseSyncRepoCommitString(commitString string) Commit { | ||
hashWithMessageAndTime := strings.Split(commitString, "$//") | ||
commitTimeStr, _ := strconv.Atoi(hashWithMessageAndTime[2]) | ||
originalCommitHash := strings.Replace(hashWithMessageAndTime[0], hashPrefix, "", 1)[0:7] | ||
return Commit{Hash: originalCommitHash, Message: hashWithMessageAndTime[1], Time: time.Unix(int64(commitTimeStr), 0)} | ||
} | ||
|
||
func parseGeneralRepoCommitString(commitString string) Commit { | ||
hashWithMessageAndTime := strings.Split(commitString, "$//") | ||
commitTimeStr, _ := strconv.Atoi(hashWithMessageAndTime[2]) | ||
return Commit{Hash: hashWithMessageAndTime[0], Message: hashWithMessageAndTime[1], Time: time.Unix(int64(commitTimeStr), 0)} | ||
} | ||
|
||
func (r *Repo) Push() error { | ||
cmd := exec.Command("git", "push", "-fu") | ||
cmd.Dir = r.Path | ||
return cmd.Run() | ||
} | ||
|
||
func GetRepoAtPath(path string) Repo { | ||
err := os.Chdir(path) | ||
if err != nil { | ||
log.Fatal("failed to change directory,\nError: ", err.Error()) | ||
} | ||
branchNameBytes, err := exec.Command("git", "branch", "--show-current").Output() | ||
if err != nil { | ||
log.Fatal("failed to get repo information.\nError: ", err.Error()) | ||
} | ||
branchName := strings.ReplaceAll(string(branchNameBytes), "\n", "") | ||
repoPathBytes, err := exec.Command("git", "rev-parse", "--show-toplevel").Output() | ||
if err != nil { | ||
log.Fatal("failed to get repo information.\nError: ", err.Error()) | ||
} | ||
repoPath := strings.ReplaceAll(string(repoPathBytes), "\n", "") | ||
repoNameBytes, err := exec.Command("basename", string(repoPath)).Output() | ||
if err != nil { | ||
log.Fatal("failed to get repo information.\nError: ", err.Error()) | ||
} | ||
repoName := strings.ReplaceAll(string(repoNameBytes), "\n", "") | ||
authorNameBytes, err := exec.Command("git", "config", "user.name").Output() | ||
if err != nil { | ||
log.Fatal("failed to get repo information.\nError: ", err.Error()) | ||
} | ||
authorName := strings.TrimSpace(string(authorNameBytes)) | ||
authorEmailBytes, err := exec.Command("git", "config", "user.email").Output() | ||
if err != nil { | ||
log.Fatal("failed to get repo information.\nError:", err.Error()) | ||
} | ||
authorEmail := strings.TrimSpace(string(string(authorEmailBytes))) | ||
return Repo{ | ||
Name: repoName, | ||
Path: repoPath, | ||
BranchName: branchName, | ||
CurrentAuthorName: authorName, | ||
CurrentAuthorEmail: authorEmail, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.