@@ -5,88 +5,91 @@ package attribute
55
66import (
77 "bytes"
8+ "context"
89 "errors"
910 "fmt"
1011 "os"
1112
1213 "code.gitea.io/gitea/modules/git"
1314)
1415
15- // CheckAttributeOpts represents the possible options to CheckAttribute
16- type CheckAttributeOpts struct {
17- CachedOnly bool
18- AllAttributes bool
19- Attributes []string
20- Filenames []string
21- IndexFile string
22- WorkTree string
23- }
24-
25- // CheckAttribute return the Blame object of file
26- func CheckAttribute (repo * git.Repository , opts CheckAttributeOpts ) (map [string ]Attributes , error ) {
27- env := []string {}
28-
29- if len (opts .IndexFile ) > 0 {
30- env = append (env , "GIT_INDEX_FILE=" + opts .IndexFile )
31- }
32- if len (opts .WorkTree ) > 0 {
33- env = append (env , "GIT_WORK_TREE=" + opts .WorkTree )
16+ func checkAttrCommand (gitRepo * git.Repository , treeish string , filenames , attributes []string ) (* git.Command , []string , func (), error ) {
17+ cmd := git .NewCommand ("check-attr" , "-z" )
18+ if len (attributes ) == 0 {
19+ cmd .AddArguments ("--all" )
3420 }
35-
36- if len (env ) > 0 {
37- env = append (os .Environ (), env ... )
21+ cmd .AddDashesAndList (filenames ... )
22+ if git .DefaultFeatures ().SupportCheckAttrOnBare && treeish != "" {
23+ cmd .AddArguments ("--source" )
24+ cmd .AddDynamicArguments (treeish )
25+ cmd .AddDynamicArguments (attributes ... )
26+ return cmd , []string {"GIT_FLUSH=1" }, nil , nil
3827 }
3928
40- stdOut := new (bytes.Buffer )
41- stdErr := new (bytes.Buffer )
42-
43- cmd := git .NewCommand ("check-attr" , "-z" )
29+ var cancel func ()
30+ var envs []string
31+ if treeish != "" { // if it's empty, then we assume it's a worktree repository
32+ indexFilename , worktree , deleteTemporaryFile , err := gitRepo .ReadTreeToTemporaryIndex (treeish )
33+ if err != nil {
34+ return nil , nil , nil , err
35+ }
4436
45- if opts .AllAttributes {
46- cmd .AddArguments ("-a" )
47- } else {
48- for _ , attribute := range opts .Attributes {
49- if attribute != "" {
50- cmd .AddDynamicArguments (attribute )
51- }
37+ envs = []string {
38+ "GIT_INDEX_FILE=" + indexFilename ,
39+ "GIT_WORK_TREE=" + worktree ,
40+ "GIT_FLUSH=1" ,
5241 }
42+ cancel = deleteTemporaryFile
5343 }
44+ cmd .AddArguments ("--cached" )
45+ cmd .AddDynamicArguments (attributes ... )
46+ return cmd , envs , cancel , nil
47+ }
48+
49+ type CheckAttributeOpts struct {
50+ Filenames []string
51+ Attributes []string
52+ }
5453
55- if opts .CachedOnly {
56- cmd .AddArguments ("--cached" )
54+ // CheckAttribute return the Blame object of file
55+ func CheckAttribute (ctx context.Context , gitRepo * git.Repository , treeish string , opts CheckAttributeOpts ) (map [string ]Attributes , error ) {
56+ cmd , envs , cancel , err := checkAttrCommand (gitRepo , treeish , opts .Filenames , opts .Attributes )
57+ if err != nil {
58+ return nil , err
5759 }
60+ defer cancel ()
5861
59- cmd .AddDashesAndList (opts .Filenames ... )
62+ stdOut := new (bytes.Buffer )
63+ stdErr := new (bytes.Buffer )
6064
61- if err := cmd .Run (repo . Ctx , & git.RunOpts {
62- Env : env ,
63- Dir : repo .Path ,
65+ if err := cmd .Run (ctx , & git.RunOpts {
66+ Env : append ( os . Environ (), envs ... ) ,
67+ Dir : gitRepo .Path ,
6468 Stdout : stdOut ,
6569 Stderr : stdErr ,
6670 }); err != nil {
6771 return nil , fmt .Errorf ("failed to run check-attr: %w\n %s\n %s" , err , stdOut .String (), stdErr .String ())
6872 }
6973
70- // FIXME: This is incorrect on versions < 1.8.5
7174 fields := bytes .Split (stdOut .Bytes (), []byte {'\000' })
7275
7376 if len (fields )% 3 != 1 {
7477 return nil , errors .New ("wrong number of fields in return from check-attr" )
7578 }
7679
77- name2attribute2info := make (map [string ]Attributes )
80+ attributesMap := make (map [string ]Attributes )
7881
7982 for i := 0 ; i < (len (fields ) / 3 ); i ++ {
8083 filename := string (fields [3 * i ])
8184 attribute := string (fields [3 * i + 1 ])
8285 info := string (fields [3 * i + 2 ])
83- attribute2info := name2attribute2info [filename ]
86+ attribute2info := attributesMap [filename ]
8487 if attribute2info == nil {
8588 attribute2info = make (Attributes )
8689 }
8790 attribute2info [attribute ] = Attribute (info )
88- name2attribute2info [filename ] = attribute2info
91+ attributesMap [filename ] = attribute2info
8992 }
9093
91- return name2attribute2info , nil
94+ return attributesMap , nil
9295}
0 commit comments