Skip to content

Commit c4b82ea

Browse files
authored
Merge branch 'master' into lunny/fix_approvals_counting
2 parents 47af47d + 026696b commit c4b82ea

File tree

16 files changed

+64
-117
lines changed

16 files changed

+64
-117
lines changed
Binary file not shown.

models/migrations/migrations.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,11 +327,25 @@ func dropTableColumns(sess *xorm.Session, tableName string, columnNames ...strin
327327
return err
328328
}
329329
tableSQL := string(res[0]["sql"])
330+
331+
// Separate out the column definitions
330332
tableSQL = tableSQL[strings.Index(tableSQL, "("):]
333+
334+
// Remove the required columnNames
331335
for _, name := range columnNames {
332-
tableSQL = regexp.MustCompile(regexp.QuoteMeta("`"+name+"`")+"[^`,)]*[,)]").ReplaceAllString(tableSQL, "")
336+
tableSQL = regexp.MustCompile(regexp.QuoteMeta("`"+name+"`")+"[^`,)]*?[,)]").ReplaceAllString(tableSQL, "")
337+
}
338+
339+
// Ensure the query is ended properly
340+
tableSQL = strings.TrimSpace(tableSQL)
341+
if tableSQL[len(tableSQL)-1] != ')' {
342+
if tableSQL[len(tableSQL)-1] == ',' {
343+
tableSQL = tableSQL[:len(tableSQL)-1]
344+
}
345+
tableSQL += ")"
333346
}
334347

348+
// Find all the columns in the table
335349
columns := regexp.MustCompile("`([^`]*)`").FindAllString(tableSQL, -1)
336350

337351
tableSQL = fmt.Sprintf("CREATE TABLE `new_%s_new` ", tableName) + tableSQL

models/migrations/v78.go

Lines changed: 7 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,7 @@
55
package migrations
66

77
import (
8-
"fmt"
9-
10-
"code.gitea.io/gitea/models"
11-
"code.gitea.io/gitea/modules/log"
12-
138
"github.com/go-xorm/xorm"
14-
"xorm.io/core"
159
)
1610

1711
func renameRepoIsBareToIsEmpty(x *xorm.Engine) error {
@@ -21,73 +15,28 @@ func renameRepoIsBareToIsEmpty(x *xorm.Engine) error {
2115
IsEmpty bool `xorm:"INDEX"`
2216
}
2317

24-
// First remove the index
2518
sess := x.NewSession()
2619
defer sess.Close()
2720
if err := sess.Begin(); err != nil {
2821
return err
2922
}
3023

31-
var err error
32-
if models.DbCfg.Type == core.POSTGRES || models.DbCfg.Type == core.SQLITE {
33-
_, err = sess.Exec("DROP INDEX IF EXISTS IDX_repository_is_bare")
34-
} else if models.DbCfg.Type == core.MSSQL {
35-
_, err = sess.Exec(`DECLARE @ConstraintName VARCHAR(256)
36-
DECLARE @SQL NVARCHAR(256)
37-
SELECT @ConstraintName = obj.name FROM sys.columns col LEFT OUTER JOIN sys.objects obj ON obj.object_id = col.default_object_id AND obj.type = 'D' WHERE col.object_id = OBJECT_ID('repository') AND obj.name IS NOT NULL AND col.name = 'is_bare'
38-
SET @SQL = N'ALTER TABLE [repository] DROP CONSTRAINT [' + @ConstraintName + N']'
39-
EXEC sp_executesql @SQL`)
40-
if err != nil {
41-
return err
42-
}
43-
} else if models.DbCfg.Type == core.MYSQL {
44-
indexes, err := sess.QueryString(`SHOW INDEX FROM repository WHERE KEY_NAME = 'IDX_repository_is_bare'`)
45-
if err != nil {
46-
return err
47-
}
48-
49-
if len(indexes) >= 1 {
50-
_, err = sess.Exec("DROP INDEX IDX_repository_is_bare ON repository")
51-
if err != nil {
52-
return fmt.Errorf("Drop index failed: %v", err)
53-
}
54-
}
55-
} else {
56-
_, err = sess.Exec("DROP INDEX IDX_repository_is_bare ON repository")
57-
}
58-
59-
if err != nil {
60-
return fmt.Errorf("Drop index failed: %v", err)
61-
}
62-
63-
if err = sess.Commit(); err != nil {
64-
return err
65-
}
66-
67-
if err := sess.Begin(); err != nil {
68-
return err
69-
}
70-
7124
if err := sess.Sync2(new(Repository)); err != nil {
7225
return err
7326
}
7427
if _, err := sess.Exec("UPDATE repository SET is_empty = is_bare;"); err != nil {
7528
return err
7629
}
77-
78-
if models.DbCfg.Type != core.SQLITE {
79-
_, err = sess.Exec("ALTER TABLE repository DROP COLUMN is_bare")
80-
if err != nil {
81-
return fmt.Errorf("Drop column failed: %v", err)
82-
}
30+
if err := sess.Commit(); err != nil {
31+
return err
8332
}
8433

85-
if err = sess.Commit(); err != nil {
34+
if err := sess.Begin(); err != nil {
8635
return err
8736
}
88-
89-
if models.DbCfg.Type == core.SQLITE {
90-
log.Warn("TABLE repository's COLUMN is_bare should be DROP but sqlite is not supported, you could manually do that.")
37+
if err := dropTableColumns(sess, "repository", "is_bare"); err != nil {
38+
return err
9139
}
92-
return nil
40+
41+
return sess.Commit()
9342
}

models/migrations/v85.go

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ import (
88
"fmt"
99

1010
"github.com/go-xorm/xorm"
11-
"xorm.io/core"
1211

13-
"code.gitea.io/gitea/models"
1412
"code.gitea.io/gitea/modules/generate"
1513
"code.gitea.io/gitea/modules/log"
1614
"code.gitea.io/gitea/modules/util"
@@ -37,41 +35,6 @@ func hashAppToken(x *xorm.Engine) error {
3735
// First remove the index
3836
sess := x.NewSession()
3937
defer sess.Close()
40-
if err := sess.Begin(); err != nil {
41-
return err
42-
}
43-
44-
var err error
45-
if models.DbCfg.Type == core.POSTGRES || models.DbCfg.Type == core.SQLITE {
46-
_, err = sess.Exec("DROP INDEX IF EXISTS UQE_access_token_sha1")
47-
} else if models.DbCfg.Type == core.MSSQL {
48-
_, err = sess.Exec(`DECLARE @ConstraintName VARCHAR(256)
49-
DECLARE @SQL NVARCHAR(256)
50-
SELECT @ConstraintName = obj.name FROM sys.columns col LEFT OUTER JOIN sys.objects obj ON obj.object_id = col.default_object_id AND obj.type = 'D' WHERE col.object_id = OBJECT_ID('access_token') AND obj.name IS NOT NULL AND col.name = 'sha1'
51-
SET @SQL = N'ALTER TABLE [access_token] DROP CONSTRAINT [' + @ConstraintName + N']'
52-
EXEC sp_executesql @SQL`)
53-
} else if models.DbCfg.Type == core.MYSQL {
54-
indexes, err := sess.QueryString(`SHOW INDEX FROM access_token WHERE KEY_NAME = 'UQE_access_token_sha1'`)
55-
if err != nil {
56-
return err
57-
}
58-
59-
if len(indexes) >= 1 {
60-
_, err = sess.Exec("DROP INDEX UQE_access_token_sha1 ON access_token")
61-
if err != nil {
62-
return err
63-
}
64-
}
65-
} else {
66-
_, err = sess.Exec("DROP INDEX UQE_access_token_sha1 ON access_token")
67-
}
68-
if err != nil {
69-
return fmt.Errorf("Drop index failed: %v", err)
70-
}
71-
72-
if err = sess.Commit(); err != nil {
73-
return err
74-
}
7538

7639
if err := sess.Begin(); err != nil {
7740
return err
@@ -81,7 +44,7 @@ func hashAppToken(x *xorm.Engine) error {
8144
return fmt.Errorf("Sync2: %v", err)
8245
}
8346

84-
if err = sess.Commit(); err != nil {
47+
if err := sess.Commit(); err != nil {
8548
return err
8649
}
8750

modules/git/commit.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ func AddChanges(repoPath string, all bool, files ...string) error {
169169
if all {
170170
cmd.AddArguments("--all")
171171
}
172+
cmd.AddArguments("--")
172173
_, err := cmd.AddArguments(files...).RunInDir(repoPath)
173174
return err
174175
}
@@ -304,6 +305,7 @@ func (c *Commit) GetFilesChangedSinceCommit(pastCommit string) ([]string, error)
304305
}
305306

306307
// FileChangedSinceCommit Returns true if the file given has changed since the the past commit
308+
// YOU MUST ENSURE THAT pastCommit is a valid commit ID.
307309
func (c *Commit) FileChangedSinceCommit(filename, pastCommit string) (bool, error) {
308310
return c.repo.FileChangedBetweenCommits(filename, pastCommit, c.ID.String())
309311
}

modules/git/repo.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,7 @@ func Pull(repoPath string, opts PullRemoteOptions) error {
187187
if opts.All {
188188
cmd.AddArguments("--all")
189189
} else {
190-
cmd.AddArguments(opts.Remote)
191-
cmd.AddArguments(opts.Branch)
190+
cmd.AddArguments("--", opts.Remote, opts.Branch)
192191
}
193192

194193
if opts.Timeout <= 0 {
@@ -213,7 +212,7 @@ func Push(repoPath string, opts PushOptions) error {
213212
if opts.Force {
214213
cmd.AddArguments("-f")
215214
}
216-
cmd.AddArguments(opts.Remote, opts.Branch)
215+
cmd.AddArguments("--", opts.Remote, opts.Branch)
217216
_, err := cmd.RunInDirWithEnv(repoPath, opts.Env)
218217
return err
219218
}

modules/git/repo_branch.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func (repo *Repository) DeleteBranch(name string, opts DeleteBranchOptions) erro
135135
cmd.AddArguments("-d")
136136
}
137137

138-
cmd.AddArguments(name)
138+
cmd.AddArguments("--", name)
139139
_, err := cmd.RunInDir(repo.Path)
140140

141141
return err

modules/git/repo_commit.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,20 +117,26 @@ func (repo *Repository) getCommit(id SHA1) (*Commit, error) {
117117
return commit, nil
118118
}
119119

120-
// GetCommit returns commit object of by ID string.
121-
func (repo *Repository) GetCommit(commitID string) (*Commit, error) {
120+
// ConvertToSHA1 returns a Hash object from a potential ID string
121+
func (repo *Repository) ConvertToSHA1(commitID string) (SHA1, error) {
122122
if len(commitID) != 40 {
123123
var err error
124-
actualCommitID, err := NewCommand("rev-parse", commitID).RunInDir(repo.Path)
124+
actualCommitID, err := NewCommand("rev-parse", "--verify", commitID).RunInDir(repo.Path)
125125
if err != nil {
126-
if strings.Contains(err.Error(), "unknown revision or path") {
127-
return nil, ErrNotExist{commitID, ""}
126+
if strings.Contains(err.Error(), "unknown revision or path") ||
127+
strings.Contains(err.Error(), "fatal: Needed a single revision") {
128+
return SHA1{}, ErrNotExist{commitID, ""}
128129
}
129-
return nil, err
130+
return SHA1{}, err
130131
}
131132
commitID = actualCommitID
132133
}
133-
id, err := NewIDFromString(commitID)
134+
return NewIDFromString(commitID)
135+
}
136+
137+
// GetCommit returns commit object of by ID string.
138+
func (repo *Repository) GetCommit(commitID string) (*Commit, error) {
139+
id, err := repo.ConvertToSHA1(commitID)
134140
if err != nil {
135141
return nil, err
136142
}
@@ -243,6 +249,7 @@ func (repo *Repository) getFilesChanged(id1, id2 string) ([]string, error) {
243249
}
244250

245251
// FileChangedBetweenCommits Returns true if the file changed between commit IDs id1 and id2
252+
// You must ensure that id1 and id2 are valid commit ids.
246253
func (repo *Repository) FileChangedBetweenCommits(filename, id1, id2 string) (bool, error) {
247254
stdout, err := NewCommand("diff", "--name-only", "-z", id1, id2, "--", filename).RunInDirBytes(repo.Path)
248255
if err != nil {

modules/git/repo_commit_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,5 @@ func TestGetCommitWithBadCommitID(t *testing.T) {
5858
commit, err := bareRepo1.GetCommit("bad_branch")
5959
assert.Nil(t, commit)
6060
assert.Error(t, err)
61-
assert.EqualError(t, err, "object does not exist [id: bad_branch, rel_path: ]")
61+
assert.True(t, IsErrNotExist(err))
6262
}

modules/git/repo_compare.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (repo *Repository) GetMergeBase(tmpRemote string, base, head string) (strin
3939
}
4040
}
4141

42-
stdout, err := NewCommand("merge-base", base, head).RunInDir(repo.Path)
42+
stdout, err := NewCommand("merge-base", "--", base, head).RunInDir(repo.Path)
4343
return strings.TrimSpace(stdout), base, err
4444
}
4545

0 commit comments

Comments
 (0)