This repository has been archived by the owner on Apr 12, 2019. It is now read-only.
forked from gogs/git-module
-
Notifications
You must be signed in to change notification settings - Fork 42
Add Storage interface for different implementation of git tags #92
Open
lunny
wants to merge
2
commits into
go-gitea:master
Choose a base branch
from
lunny:lunny/get_tag
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 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 |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Copyright 2017 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package git | ||
|
||
import ( | ||
"bytes" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
func readPackedRefs(repoPath string) ([]string, error) { | ||
path := repoPath + "/packed-refs" | ||
|
||
if _, err := os.Stat(path); os.IsNotExist(err) { | ||
return nil, nil | ||
} | ||
|
||
refData, err := ioutil.ReadFile(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
re := regexp.MustCompile("v\\d+\\.\\d+\\.\\d+$") | ||
names := []string{} | ||
|
||
for _, ref := range bytes.Split(refData, []byte("\n")) { | ||
if tag := re.Find(ref); tag != nil { | ||
names = append(names, string(tag)) | ||
} | ||
} | ||
|
||
return names, nil | ||
} | ||
|
||
func readRefDir(repoPath, prefix, relPath string) ([]string, error) { | ||
dirPath := filepath.Join(repoPath, prefix, relPath) | ||
f, err := os.Open(dirPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer f.Close() | ||
|
||
fis, err := f.Readdir(0) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
names := make([]string, 0, len(fis)) | ||
for _, fi := range fis { | ||
if strings.Contains(fi.Name(), ".DS_Store") { | ||
continue | ||
} | ||
|
||
relFileName := filepath.Join(relPath, fi.Name()) | ||
if fi.IsDir() { | ||
subnames, err := readRefDir(repoPath, prefix, relFileName) | ||
if err != nil { | ||
return nil, err | ||
} | ||
names = append(names, subnames...) | ||
continue | ||
} | ||
|
||
names = append(names, relFileName) | ||
} | ||
|
||
return names, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package git | ||
|
||
import "testing" | ||
|
||
func TestGetRefs(t *testing.T) { | ||
expected := []string{"v1.0.0", "v1.2.1", "v2.0.0", "v3.0.0", "v3.1.0", "v3.10.1"} | ||
|
||
for _, p := range []string{ | ||
"testdata/test_loose_refs.git", | ||
"testdata/test_packed_refs.git", | ||
"testdata/test_mixed_refs.git", | ||
} { | ||
r, err := OpenRepository(p) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
tags, err := r.GetTags() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if len(expected) != len(tags) { | ||
t.Fatalf("[%s] wrong number of tags returned - expected [%d] got [%d]", p, len(expected), len(tags)) | ||
} | ||
|
||
for i, v := range tags { | ||
if expected[i] != v { | ||
t.Fatalf("[%s] incorrect tag - expected [%s] got [%s]", p, expected[i], v) | ||
} | ||
} | ||
} | ||
|
||
} |
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,87 @@ | ||
// Copyright 2017 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package git | ||
|
||
import ( | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/Unknwon/com" | ||
version "github.com/mcuadros/go-version" | ||
) | ||
|
||
// Storage is an interface which describes how to fetch git objects from the git data storage placement. | ||
type Storage interface { | ||
GetTags(string) ([]string, error) | ||
} | ||
|
||
// shellStorage is an implementation of Storage which use git shell to retrieve git objects from file system. | ||
type shellStorage struct { | ||
} | ||
|
||
// GetTags return the tag names according repoPath | ||
func (shellStorage) GetTags(repoPath string) ([]string, error) { | ||
cmd := NewCommand("tag", "-l") | ||
if version.Compare(gitVersion, "2.0.0", ">=") { | ||
cmd.AddArguments("--sort=-v:refname") | ||
} | ||
|
||
stdout, err := cmd.RunInDir(repoPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
tags := strings.Split(stdout, "\n") | ||
tags = tags[:len(tags)-1] | ||
|
||
if version.Compare(gitVersion, "2.0.0", "<") { | ||
version.Sort(tags) | ||
|
||
// Reverse order | ||
for i := 0; i < len(tags)/2; i++ { | ||
j := len(tags) - i - 1 | ||
tags[i], tags[j] = tags[j], tags[i] | ||
} | ||
} | ||
|
||
return tags, nil | ||
} | ||
|
||
type localStorage struct { | ||
} | ||
|
||
// GetTags return the tag names according repoPath | ||
func (localStorage) GetTags(repoPath string) ([]string, error) { | ||
packed, err := readPackedRefs(repoPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if !com.IsExist(filepath.Join(repoPath, "refs/tags")) { | ||
return packed, nil | ||
} | ||
|
||
// Attempt loose files first as the /refs/tags folder should always | ||
// exist whether it has files or not. | ||
loose, err := readRefDir(repoPath, "refs/tags", "") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// If both loose refs and packed refs exist then it's highly | ||
// likely that the loose refs are more recent than packed (created | ||
// on top of packed older refs). Therefore we can append each | ||
// together taking the packed refs first. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should not return duplicate refs. Especially not stale refs :( |
||
return append(packed, loose...), nil | ||
} | ||
|
||
var ( | ||
// ShellStorage provides methods to retrieve git objects via git shell | ||
ShellStorage = new(shellStorage) | ||
// LocalStorage provides methods to retrieve git objects via pure go | ||
LocalStorage = new(localStorage) | ||
// DefaultStorage is the default implementation of git data storage | ||
DefaultStorage Storage = ShellStorage | ||
) |
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 @@ | ||
f821d04fc4f8ba72f88cf1880b69860c88a0a58b |
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 @@ | ||
f821d04fc4f8ba72f88cf1880b69860c88a0a58b |
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 @@ | ||
f821d04fc4f8ba72f88cf1880b69860c88a0a58b |
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 @@ | ||
f821d04fc4f8ba72f88cf1880b69860c88a0a58b |
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 @@ | ||
f821d04fc4f8ba72f88cf1880b69860c88a0a58b |
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 @@ | ||
f821d04fc4f8ba72f88cf1880b69860c88a0a58b |
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,6 @@ | ||
# pack-refs with: peeled fully-peeled | ||
f821d04fc4f8ba72f88cf1880b69860c88a0a58b refs/heads/master | ||
f821d04fc4f8ba72f88cf1880b69860c88a0a58b refs/remotes/origin/master | ||
f821d04fc4f8ba72f88cf1880b69860c88a0a58b refs/tags/v1.0.0 | ||
f821d04fc4f8ba72f88cf1880b69860c88a0a58b refs/tags/v1.2.1 | ||
f821d04fc4f8ba72f88cf1880b69860c88a0a58b refs/tags/v2.0.0 |
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 @@ | ||
f821d04fc4f8ba72f88cf1880b69860c88a0a58b |
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 @@ | ||
f821d04fc4f8ba72f88cf1880b69860c88a0a58b |
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 @@ | ||
f821d04fc4f8ba72f88cf1880b69860c88a0a58b |
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,9 @@ | ||
# pack-refs with: peeled fully-peeled | ||
f821d04fc4f8ba72f88cf1880b69860c88a0a58b refs/heads/master | ||
f821d04fc4f8ba72f88cf1880b69860c88a0a58b refs/remotes/origin/master | ||
f821d04fc4f8ba72f88cf1880b69860c88a0a58b refs/tags/v1.0.0 | ||
f821d04fc4f8ba72f88cf1880b69860c88a0a58b refs/tags/v1.2.1 | ||
f821d04fc4f8ba72f88cf1880b69860c88a0a58b refs/tags/v2.0.0 | ||
f821d04fc4f8ba72f88cf1880b69860c88a0a58b refs/tags/v3.0.0 | ||
f821d04fc4f8ba72f88cf1880b69860c88a0a58b refs/tags/v3.1.0 | ||
f821d04fc4f8ba72f88cf1880b69860c88a0a58b refs/tags/v3.10.1 |
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.
How would this get the tag
unreleased-beta-version
?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.
and the regexp given above doesn't find any tags at all since it's broken :)