-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor models.NewRepoContext to extract git related codes to modules/git #6941
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -13,7 +13,6 @@ import ( | |
"io/ioutil" | ||
"net/url" | ||
"os" | ||
"os/exec" | ||
"path" | ||
"path/filepath" | ||
"regexp" | ||
|
@@ -32,11 +31,9 @@ import ( | |
"code.gitea.io/gitea/modules/sync" | ||
"code.gitea.io/gitea/modules/util" | ||
|
||
"github.com/Unknwon/cae/zip" | ||
"github.com/Unknwon/com" | ||
"github.com/go-xorm/builder" | ||
"github.com/go-xorm/xorm" | ||
version "github.com/mcuadros/go-version" | ||
ini "gopkg.in/ini.v1" | ||
) | ||
|
||
|
@@ -67,8 +64,8 @@ var ( | |
ItemsPerPage = 40 | ||
) | ||
|
||
// LoadRepoConfig loads the repository config | ||
func LoadRepoConfig() { | ||
// loadRepoConfig loads the repository config | ||
func loadRepoConfig() { | ||
// Load .gitignore and license files and readme templates. | ||
types := []string{"gitignore", "license", "readme", "label"} | ||
typeFiles := make([][]string, 4) | ||
|
@@ -119,45 +116,7 @@ func LoadRepoConfig() { | |
|
||
// NewRepoContext creates a new repository context | ||
func NewRepoContext() { | ||
zip.Verbose = false | ||
|
||
// Check Git installation. | ||
if _, err := exec.LookPath("git"); err != nil { | ||
log.Fatal("Failed to test 'git' command: %v (forgotten install?)", err) | ||
} | ||
|
||
// Check Git version. | ||
var err error | ||
setting.Git.Version, err = git.BinVersion() | ||
if err != nil { | ||
log.Fatal("Failed to get Git version: %v", err) | ||
} | ||
|
||
log.Info("Git Version: %s", setting.Git.Version) | ||
if version.Compare("1.7.1", setting.Git.Version, ">") { | ||
log.Fatal("Gitea requires Git version greater or equal to 1.7.1") | ||
} | ||
|
||
// Git requires setting user.name and user.email in order to commit changes. | ||
for configKey, defaultValue := range map[string]string{"user.name": "Gitea", "user.email": "[email protected]"} { | ||
if stdout, stderr, err := process.GetManager().Exec("NewRepoContext(get setting)", "git", "config", "--get", configKey); err != nil || strings.TrimSpace(stdout) == "" { | ||
// ExitError indicates this config is not set | ||
if _, ok := err.(*exec.ExitError); ok || strings.TrimSpace(stdout) == "" { | ||
if _, stderr, gerr := process.GetManager().Exec("NewRepoContext(set "+configKey+")", "git", "config", "--global", configKey, defaultValue); gerr != nil { | ||
log.Fatal("Failed to set git %s(%s): %s", configKey, gerr, stderr) | ||
} | ||
log.Info("Git config %s set to %s", configKey, defaultValue) | ||
} else { | ||
log.Fatal("Failed to get git %s(%s): %s", configKey, err, stderr) | ||
} | ||
} | ||
} | ||
|
||
// Set git some configurations. | ||
if _, stderr, err := process.GetManager().Exec("NewRepoContext(git config --global core.quotepath false)", | ||
"git", "config", "--global", "core.quotepath", "false"); err != nil { | ||
log.Fatal("Failed to execute 'git config --global core.quotepath false': %s", stderr) | ||
} | ||
loadRepoConfig() | ||
|
||
RemoveAllWithNotice("Clean up repository temporary data", filepath.Join(setting.AppDataPath, "tmp")) | ||
} | ||
|
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 |
---|---|---|
|
@@ -11,6 +11,8 @@ import ( | |
"strings" | ||
"time" | ||
|
||
"code.gitea.io/gitea/modules/process" | ||
|
||
"github.com/mcuadros/go-version" | ||
) | ||
|
||
|
@@ -31,6 +33,8 @@ var ( | |
// GitExecutable is the command name of git | ||
// Could be updated to an absolute path while initialization | ||
GitExecutable = "git" | ||
|
||
gitVersion string | ||
) | ||
|
||
func log(format string, args ...interface{}) { | ||
|
@@ -46,8 +50,6 @@ func log(format string, args ...interface{}) { | |
} | ||
} | ||
|
||
var gitVersion string | ||
|
||
// BinVersion returns current Git version from shell. | ||
func BinVersion() (string, error) { | ||
if len(gitVersion) > 0 { | ||
|
@@ -89,6 +91,26 @@ func init() { | |
if version.Compare(gitVersion, GitVersionRequired, "<") { | ||
panic(fmt.Sprintf("Git version not supported. Requires version > %v", GitVersionRequired)) | ||
} | ||
|
||
// Git requires setting user.name and user.email in order to commit changes. | ||
for configKey, defaultValue := range map[string]string{"user.name": "Gitea", "user.email": "[email protected]"} { | ||
if stdout, stderr, err := process.GetManager().Exec("git.Init(get setting)", GitExecutable, "config", "--get", configKey); err != nil || strings.TrimSpace(stdout) == "" { | ||
// ExitError indicates this config is not set | ||
if _, ok := err.(*exec.ExitError); ok || strings.TrimSpace(stdout) == "" { | ||
if _, stderr, gerr := process.GetManager().Exec("git.Init(set "+configKey+")", "git", "config", "--global", configKey, defaultValue); gerr != nil { | ||
panic(fmt.Sprintf("Failed to set git %s(%s): %s", configKey, gerr, stderr)) | ||
} | ||
} else { | ||
panic(fmt.Sprintf("Failed to get git %s(%s): %s", configKey, err, stderr)) | ||
} | ||
} | ||
} | ||
|
||
// Set git some configurations. | ||
if _, stderr, err := process.GetManager().Exec("git.Init(git config --global core.quotepath false)", | ||
GitExecutable, "config", "--global", "core.quotepath", "false"); err != nil { | ||
panic(fmt.Sprintf("Failed to execute 'git config --global core.quotepath false': %s", stderr)) | ||
} | ||
} | ||
|
||
// Fsck verifies the connectivity and validity of the objects in the database | ||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we still need this? I'm not sure we use cae/zip elsewhere.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't know. Since this is a refactor PR, I just keep it.