-
-
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.
- Loading branch information
Showing
5 changed files
with
217 additions
and
7 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,45 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os/exec" | ||
"path/filepath" | ||
"strings" | ||
|
||
"moul.io/u" | ||
) | ||
|
||
func gitFindRootDir(path string) string { | ||
for { | ||
if u.DirExists(filepath.Join(path, ".git")) { | ||
return path | ||
} | ||
parent := filepath.Dir(path) | ||
if parent == path { | ||
break | ||
} | ||
path = parent | ||
} | ||
return "" | ||
} | ||
|
||
func gitGetMainBranch(path string) (string, error) { | ||
// equivalent of: git symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@' | ||
|
||
cmd := exec.Command("git", "symbolic-ref", "refs/remotes/origin/HEAD") | ||
stdout, _, err := u.ExecStandaloneOutputs(cmd) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to execute command: %q: %w", cmd, err) | ||
} | ||
|
||
if len(stdout) == 0 { | ||
return "", fmt.Errorf("invalid command output for %q", cmd) | ||
} | ||
|
||
branch := strings.TrimSpace(strings.TrimPrefix(string(stdout), "refs/remotes/origin/")) | ||
if branch == "" || strings.ContainsRune(branch, '/') { | ||
return "", fmt.Errorf("invalid branch: %q", branch) | ||
} | ||
|
||
return branch, nil | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,48 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"path/filepath" | ||
|
||
"go.uber.org/zap" | ||
"moul.io/u" | ||
) | ||
|
||
type project struct { | ||
Path string | ||
Git struct { | ||
Root string | ||
MainBranch string | ||
CurrentBranch string | ||
OriginRemote string | ||
} | ||
} | ||
|
||
func (p *project) checkoutMainBranch() error { | ||
return nil | ||
} | ||
|
||
func projectFromPath(path string) (*project, error) { | ||
abs, err := filepath.Abs(path) | ||
if err != nil { | ||
return nil, fmt.Errorf("incorrect path: %q: %w", path, err) | ||
} | ||
path = abs | ||
|
||
if !u.DirExists(path) { | ||
return nil, fmt.Errorf("path is not a directory: %q", path) | ||
} | ||
|
||
project := &project{Path: path} | ||
project.Git.Root = gitFindRootDir(path) | ||
if project.Git.Root != "" { | ||
project.Git.MainBranch, err = gitGetMainBranch(project.Git.Root) | ||
if err != nil { | ||
return nil, fmt.Errorf("cannot guess main branch: %w", err) | ||
} | ||
} else { | ||
logger.Warn("project not withing a git directory", zap.String("path", path)) | ||
} | ||
|
||
return project, nil | ||
} |