-
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.
Collect information about git repository of SQLMesh project
- Loading branch information
1 parent
dceaa51
commit 996b1cb
Showing
6 changed files
with
115 additions
and
10 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
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,62 @@ | ||
package git | ||
|
||
import ( | ||
ingestsqlmeshv1 "buf.build/gen/go/getsynq/api/protocolbuffers/go/synq/ingest/sqlmesh/v1" | ||
"context" | ||
"os/exec" | ||
"strings" | ||
"time" | ||
) | ||
|
||
func CollectGitContext(ctx context.Context, dir string) *ingestsqlmeshv1.GitContext { | ||
if !commandExists("git") { | ||
return nil | ||
} | ||
|
||
return &ingestsqlmeshv1.GitContext{ | ||
CloneUrl: getCloneUrl(ctx, dir), | ||
Branch: getCurrentBranch(ctx, dir), | ||
CommitSha: getCommitSHA(ctx, dir), | ||
} | ||
} | ||
|
||
func getCurrentBranch(ctx context.Context, dir string) string { | ||
ctx, cancel := context.WithTimeout(ctx, 10*time.Second) | ||
defer cancel() | ||
cmd := exec.CommandContext(ctx, "git", "rev-parse", "--abbrev-ref", "HEAD") | ||
cmd.Dir = dir | ||
out, err := cmd.Output() | ||
if err != nil { | ||
return "" | ||
} | ||
return strings.TrimSpace(string(out)) | ||
} | ||
|
||
func getCommitSHA(ctx context.Context, dir string) string { | ||
ctx, cancel := context.WithTimeout(ctx, 10*time.Second) | ||
defer cancel() | ||
cmd := exec.CommandContext(ctx, "git", "rev-parse", "HEAD") | ||
cmd.Dir = dir | ||
out, err := cmd.Output() | ||
if err != nil { | ||
return "" | ||
} | ||
return strings.TrimSpace(string(out)) | ||
} | ||
|
||
func getCloneUrl(ctx context.Context, dir string) string { | ||
ctx, cancel := context.WithTimeout(ctx, 10*time.Second) | ||
defer cancel() | ||
cmd := exec.CommandContext(ctx, "git", "remote", "get-url", "origin") | ||
cmd.Dir = dir | ||
out, err := cmd.Output() | ||
if err != nil { | ||
return "" | ||
} | ||
return strings.TrimSpace(string(out)) | ||
} | ||
|
||
func commandExists(cmd string) bool { | ||
_, err := exec.LookPath(cmd) | ||
return err == nil | ||
} |
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