From 8caf4e7e4f6b4961f6396ed72e6c18ab47215a85 Mon Sep 17 00:00:00 2001 From: Nilesh Date: Thu, 29 Aug 2019 23:23:16 +0530 Subject: [PATCH 1/4] Maintain permissions for attribute file while applying attribute --- .../service/repository/apply_gitattributes.go | 7 +++++++ .../repository/apply_gitattributes_test.go | 15 +++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/internal/service/repository/apply_gitattributes.go b/internal/service/repository/apply_gitattributes.go index 4f52a06be8..e2b6dd87e2 100644 --- a/internal/service/repository/apply_gitattributes.go +++ b/internal/service/repository/apply_gitattributes.go @@ -68,6 +68,13 @@ func applyGitattributes(c *catfile.Batch, repoPath string, revision []byte) erro return err } + // Change the permission of tempFile as the permission of file attributesPath + if info, err := os.Stat(attributesPath); err == nil { + if err := os.Chmod(tempFile.Name(), info.Mode()); err != nil { + return err + } + } + // Rename temp file and return the result return os.Rename(tempFile.Name(), attributesPath) } diff --git a/internal/service/repository/apply_gitattributes_test.go b/internal/service/repository/apply_gitattributes_test.go index e82968530d..31faf77156 100644 --- a/internal/service/repository/apply_gitattributes_test.go +++ b/internal/service/repository/apply_gitattributes_test.go @@ -133,6 +133,12 @@ func assertGitattributesApplied(t *testing.T, client gitalypb.RepositoryServiceC ctx, cancel := testhelper.Context() defer cancel() + var expectedFileMode os.FileMode + shouldCheckFileMode := false + if info, err := os.Stat(attributesPath); err == nil { + expectedFileMode = info.Mode() + shouldCheckFileMode = true + } req := &gitalypb.ApplyGitattributesRequest{Repository: testRepo, Revision: revision} c, err := client.ApplyGitattributes(ctx, req) @@ -149,6 +155,15 @@ func assertGitattributesApplied(t *testing.T, client gitalypb.RepositoryServiceC t.Error(err) } + if shouldCheckFileMode { + if info, err := os.Stat(attributesPath); err == nil { + actualFileMode := info.Mode() + if actualFileMode != expectedFileMode { + t.Errorf("Expected Permission of attributes file %s, found %s", expectedFileMode.String(), actualFileMode.String()) + } + } + } + assert.Equal(t, expectedContents, contents) } } -- GitLab From 1004320296f9da0333d4e11075707b8e1ecf589e Mon Sep 17 00:00:00 2001 From: Nilesh Date: Mon, 2 Sep 2019 13:41:52 +0530 Subject: [PATCH 2/4] Added changelog --- changelogs/unreleased/attributes-perm.yml | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 changelogs/unreleased/attributes-perm.yml diff --git a/changelogs/unreleased/attributes-perm.yml b/changelogs/unreleased/attributes-perm.yml new file mode 100644 index 0000000000..1628516936 --- /dev/null +++ b/changelogs/unreleased/attributes-perm.yml @@ -0,0 +1,5 @@ +--- +title: Maintain permissions for attributes file +merge_request: 1466 +author: @njkevlani +type: added -- GitLab From 3fb9d24d9d4e842c94a2d57fa231ebaf13a89411 Mon Sep 17 00:00:00 2001 From: Nilesh Date: Tue, 3 Sep 2019 23:32:25 +0530 Subject: [PATCH 3/4] Set permission of attributes file to `0644` --- changelogs/unreleased/attributes-perm.yml | 2 +- .../service/repository/apply_gitattributes.go | 8 ++++---- .../repository/apply_gitattributes_test.go | 16 ++++------------ 3 files changed, 9 insertions(+), 17 deletions(-) diff --git a/changelogs/unreleased/attributes-perm.yml b/changelogs/unreleased/attributes-perm.yml index 1628516936..03bf4e4c00 100644 --- a/changelogs/unreleased/attributes-perm.yml +++ b/changelogs/unreleased/attributes-perm.yml @@ -1,5 +1,5 @@ --- -title: Maintain permissions for attributes file +title: Set permission of attributes file to `0644` merge_request: 1466 author: @njkevlani type: added diff --git a/internal/service/repository/apply_gitattributes.go b/internal/service/repository/apply_gitattributes.go index e2b6dd87e2..c302ce170c 100644 --- a/internal/service/repository/apply_gitattributes.go +++ b/internal/service/repository/apply_gitattributes.go @@ -16,6 +16,8 @@ import ( "google.golang.org/grpc/status" ) +const attributesFileMode os.FileMode = 0644 + func applyGitattributes(c *catfile.Batch, repoPath string, revision []byte) error { infoPath := path.Join(repoPath, "info") attributesPath := path.Join(infoPath, "attributes") @@ -69,10 +71,8 @@ func applyGitattributes(c *catfile.Batch, repoPath string, revision []byte) erro } // Change the permission of tempFile as the permission of file attributesPath - if info, err := os.Stat(attributesPath); err == nil { - if err := os.Chmod(tempFile.Name(), info.Mode()); err != nil { - return err - } + if err := os.Chmod(tempFile.Name(), attributesFileMode); err != nil { + return err } // Rename temp file and return the result diff --git a/internal/service/repository/apply_gitattributes_test.go b/internal/service/repository/apply_gitattributes_test.go index 31faf77156..3b9099dca9 100644 --- a/internal/service/repository/apply_gitattributes_test.go +++ b/internal/service/repository/apply_gitattributes_test.go @@ -133,12 +133,6 @@ func assertGitattributesApplied(t *testing.T, client gitalypb.RepositoryServiceC ctx, cancel := testhelper.Context() defer cancel() - var expectedFileMode os.FileMode - shouldCheckFileMode := false - if info, err := os.Stat(attributesPath); err == nil { - expectedFileMode = info.Mode() - shouldCheckFileMode = true - } req := &gitalypb.ApplyGitattributesRequest{Repository: testRepo, Revision: revision} c, err := client.ApplyGitattributes(ctx, req) @@ -155,12 +149,10 @@ func assertGitattributesApplied(t *testing.T, client gitalypb.RepositoryServiceC t.Error(err) } - if shouldCheckFileMode { - if info, err := os.Stat(attributesPath); err == nil { - actualFileMode := info.Mode() - if actualFileMode != expectedFileMode { - t.Errorf("Expected Permission of attributes file %s, found %s", expectedFileMode.String(), actualFileMode.String()) - } + if info, err := os.Stat(attributesPath); err == nil { + actualFileMode := info.Mode() + if actualFileMode != attributesFileMode { + t.Errorf("Expected Permission of attributes file %s, found %s", attributesFileMode.String(), actualFileMode.String()) } } -- GitLab From bd2a125b1013d25a9d633fd90f95a4b83ef005e7 Mon Sep 17 00:00:00 2001 From: Nilesh Kevlani Date: Thu, 5 Sep 2019 17:01:08 +0000 Subject: [PATCH 4/4] Apply suggestion to internal/service/repository/apply_gitattributes_test.go --- internal/service/repository/apply_gitattributes_test.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/internal/service/repository/apply_gitattributes_test.go b/internal/service/repository/apply_gitattributes_test.go index 3b9099dca9..0dd242b92f 100644 --- a/internal/service/repository/apply_gitattributes_test.go +++ b/internal/service/repository/apply_gitattributes_test.go @@ -151,9 +151,7 @@ func assertGitattributesApplied(t *testing.T, client gitalypb.RepositoryServiceC if info, err := os.Stat(attributesPath); err == nil { actualFileMode := info.Mode() - if actualFileMode != attributesFileMode { - t.Errorf("Expected Permission of attributes file %s, found %s", attributesFileMode.String(), actualFileMode.String()) - } + assert.Equal(t, attributesFileMode, actualFileMode) } assert.Equal(t, expectedContents, contents) -- GitLab