Repository: getsops/sops (language: python)

Issue:
sops seems to duplicate yaml comments in certain cases
I have no idea how many cases this happens in, but I found at least one case where sops duplicates comments in yaml files.

I'm able to reproduce this with the following steps.

First create a yaml file with the following content:
```
a:
    b:
        c:
            - name: test_1
              test: a
            # I get duplicated
            - name: test_2
              test: b
```

Then encrypt it with sops.
```
$ cat test.yaml
a:
    b:
        c:
            - name: test_1
              test: a
            # I get duplicated
            - name: test_2
              test: b
$ sops --version
sops 3.7.1 (latest)
$ sops -e --verbose -i test.yaml
[GCPKMS]	 INFO[0000] Encryption succeeded                          resourceID=REMOVED
[CMD]	 INFO[0000] File written successfully
```

Now decrypt it with sops.
```
$ sops -d test.yaml
a:
    b:
        c:
            - name: test_1
              test: a
            # I get duplicated
            # I get duplicated
            # I get duplicated
            - name: test_2
              test: b
```


Discussion / hints:

Fix patch:
diff --git a/stores/yaml/store.go b/stores/yaml/store.go
index a3ae2fb46..d9e78b528 100644
--- a/stores/yaml/store.go
+++ b/stores/yaml/store.go
@@ -70,7 +70,7 @@ func (store Store) nodeToTreeValue(node *yaml.Node, commentsWereHandled bool) (i
 		return result, nil
 	case yaml.MappingNode:
 		branch := make(sops.TreeBranch, 0)
-		return store.appendYamlNodeToTreeBranch(node, branch, false)
+		return store.appendYamlNodeToTreeBranch(node, branch, commentsWereHandled)
 	case yaml.ScalarNode:
 		var result interface{}
 		node.Decode(&result)


Test patch:
diff --git a/stores/yaml/store_test.go b/stores/yaml/store_test.go
index be6b90eb2..05249f4a5 100644
--- a/stores/yaml/store_test.go
+++ b/stores/yaml/store_test.go
@@ -91,6 +91,26 @@ var COMMENT_5 = []byte(`# foo
 key: value
 `)
 
+// The following is a regression test for https://github.com/mozilla/sops/issues/865
+var COMMENT_6 = []byte(`a:
+    - a
+    # I no longer get duplicated
+    - {}
+`)
+
+var COMMENT_6_BRANCHES = sops.TreeBranches{
+	sops.TreeBranch{
+		sops.TreeItem{
+			Key:   "a",
+			Value: []interface{}{
+				"a",
+				sops.Comment{" I no longer get duplicated"},
+				sops.TreeBranch{},
+			},
+		},
+	},
+}
+
 func TestUnmarshalMetadataFromNonSOPSFile(t *testing.T) {
 	data := []byte(`hello: 2`)
 	_, err := (&Store{}).LoadEncryptedFile(data)
@@ -178,6 +198,16 @@ func TestEmpty2(t *testing.T) {
 }
 */
 
+func TestComment6(t *testing.T) {
+	branches, err := (&Store{}).LoadPlainFile(COMMENT_6)
+	assert.Nil(t, err)
+	assert.Equal(t, COMMENT_6_BRANCHES, branches)
+	bytes, err := (&Store{}).EmitPlainFile(branches)
+	assert.Nil(t, err)
+	assert.Equal(t, string(COMMENT_6), string(bytes))
+	assert.Equal(t, COMMENT_6, bytes)
+}
+
 func TestEmitValue(t *testing.T) {
 	// First iteration: load and store
 	bytes, err := (&Store{}).EmitValue(BRANCHES[0])