Skip to content

Commit

Permalink
feat(save): File hint, from --file or --body flags, informs commit me…
Browse files Browse the repository at this point in the history
…ssage
  • Loading branch information
dustmop committed Apr 20, 2020
1 parent 3e77d0a commit 10403f5
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 72 deletions.
30 changes: 21 additions & 9 deletions base/dsfs/dataset.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"io"
"io/ioutil"
"os"
"path/filepath"
"sync"
"time"

Expand Down Expand Up @@ -223,19 +224,21 @@ func DerefDatasetCommit(ctx context.Context, store cafs.Filestore, ds *dataset.D
// SaveSwitches represents options for saving a dataset
type SaveSwitches struct {
// Replace is whether the save is a full replacement or a set of patches to previous
Replace bool
Replace bool
// DryRun is whether the save should not be written to the refstore for real
DryRun bool
DryRun bool
// Pin is whether the dataset should be pinned
Pin bool
Pin bool
// ConvertFormatToPrev is whether the body should be converted to match the previous format
ConvertFormatToPrev bool
// ForceIfNoChanges is whether the save should be forced even if no changes are detected
ForceIfNoChanges bool
ForceIfNoChanges bool
// ShouldRender is deprecated, controls whether viz should be rendered
ShouldRender bool
ShouldRender bool
// NewName is whether a new dataset should be created, guaranteeing there's no previous version
NewName bool
NewName bool
// FileHint is a hint for what file is used for creating this dataset
FileHint string
}

// CreateDataset places a dataset into the store.
Expand Down Expand Up @@ -390,7 +393,7 @@ func prepareDataset(store cafs.Filestore, ds, dsPrev *dataset.Dataset, privKey c
bodyAct = BodyTooBig
}

if err = generateCommit(store, dsPrev, ds, privKey, bodyAct, sw.ForceIfNoChanges); err != nil {
if err = generateCommit(store, dsPrev, ds, privKey, bodyAct, sw.FileHint, sw.ForceIfNoChanges); err != nil {
return err
}

Expand All @@ -410,13 +413,20 @@ func prepareDataset(store cafs.Filestore, ds, dsPrev *dataset.Dataset, privKey c
}

// generateCommit creates the commit title, message, timestamp, etc
func generateCommit(store cafs.Filestore, prev, ds *dataset.Dataset, privKey crypto.PrivKey, bodyAct BodyAction, forceIfNoChanges bool) error {
func generateCommit(store cafs.Filestore, prev, ds *dataset.Dataset, privKey crypto.PrivKey, bodyAct BodyAction, fileHint string, forceIfNoChanges bool) error {
shortTitle, longMessage, err := generateCommitDescriptions(store, prev, ds, bodyAct, forceIfNoChanges)
if err != nil {
log.Debug(fmt.Errorf("error saving: %s", err))
return fmt.Errorf("error saving: %s", err)
}

if shortTitle == defaultCreatedDescription && fileHint != "" {
shortTitle = shortTitle + " from " + filepath.Base(fileHint)
}
if longMessage == defaultCreatedDescription && fileHint != "" {
longMessage = longMessage + " from " + filepath.Base(fileHint)
}

if ds.Commit.Title == "" {
ds.Commit.Title = shortTitle
}
Expand Down Expand Up @@ -549,10 +559,12 @@ func setChecksumAndLength(ds *dataset.Dataset, data qfs.File, buf *bytes.Buffer,
done <- nil
}

const defaultCreatedDescription = "created dataset"

// returns a commit message based on the diff of the two datasets
func generateCommitDescriptions(store cafs.Filestore, prev, ds *dataset.Dataset, bodyAct BodyAction, forceIfNoChanges bool) (short, long string, err error) {
if prev == nil || prev.IsEmpty() {
return "created dataset", "created dataset", nil
return defaultCreatedDescription, defaultCreatedDescription, nil
}

ctx := context.TODO()
Expand Down
4 changes: 2 additions & 2 deletions cmd/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestAddAndListRefs(t *testing.T) {
expect := `0 Peername: test_peer
ProfileID: QmeL2mdVka1eahKENjehK6tBxkkpk5dNQ1qMcgWi7Hrb4B
Name: my_dataset
Path: /ipfs/QmQ8xUVwptw46kxxNBAw974LZsY58kCWorCbF7Bf2n2D5n
Path: /ipfs/QmXte9h1Ztm1nyd4G1CUjWnkL82T2eY7qomMfY4LUXsn3Z
FSIPath:
Published: false
Expand All @@ -56,7 +56,7 @@ func TestAddAndListRefs(t *testing.T) {
1 Peername: test_peer
ProfileID: QmeL2mdVka1eahKENjehK6tBxkkpk5dNQ1qMcgWi7Hrb4B
Name: my_dataset
Path: /ipfs/QmQ8xUVwptw46kxxNBAw974LZsY58kCWorCbF7Bf2n2D5n
Path: /ipfs/QmXte9h1Ztm1nyd4G1CUjWnkL82T2eY7qomMfY4LUXsn3Z
FSIPath:
Published: false
Expand Down
30 changes: 15 additions & 15 deletions cmd/cmd_test.go

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions cmd/fetch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestFetchCommand(t *testing.T) {

// Get the log, should have two versions.
actual := a.MustExec(t, "qri log peer_a/test_movies")
expect := `1 Commit: /ipfs/QmX3MtEVFdJMfAFaHvUsf6BVMS2m4Qw1huXUf1Gi3DPyUc
expect := `1 Commit: /ipfs/Qmde2e5etUdVtBzHdvnsPXU4mFmy3yepw72rWz3CQ8JQ3v
Date: Sun Dec 31 20:05:01 EST 2000
Storage: local
Size: 720 B
Expand All @@ -39,12 +39,12 @@ func TestFetchCommand(t *testing.T) {
body:
changed by 70%
2 Commit: /ipfs/QmQ8xUVwptw46kxxNBAw974LZsY58kCWorCbF7Bf2n2D5n
2 Commit: /ipfs/QmXte9h1Ztm1nyd4G1CUjWnkL82T2eY7qomMfY4LUXsn3Z
Date: Sun Dec 31 20:02:01 EST 2000
Storage: local
Size: 224 B
created dataset
created dataset from body_ten.csv
`
if diff := cmp.Diff(expect, actual); diff != "" {
Expand Down Expand Up @@ -108,19 +108,19 @@ func TestFetchCommand(t *testing.T) {

// Have peer B fetch from peer A, output correlates to the log from peer A earlier
actual = b.MustExec(t, "qri fetch peer_a/test_movies --remote a_node")
expect = `1 Commit: /ipfs/QmX3MtEVFdJMfAFaHvUsf6BVMS2m4Qw1huXUf1Gi3DPyUc
expect = `1 Commit: /ipfs/Qmde2e5etUdVtBzHdvnsPXU4mFmy3yepw72rWz3CQ8JQ3v
Date: Sun Dec 31 20:05:01 EST 2000
Storage: remote
Size: 720 B
body changed by 70%
2 Commit: /ipfs/QmQ8xUVwptw46kxxNBAw974LZsY58kCWorCbF7Bf2n2D5n
2 Commit: /ipfs/QmXte9h1Ztm1nyd4G1CUjWnkL82T2eY7qomMfY4LUXsn3Z
Date: Sun Dec 31 20:02:01 EST 2000
Storage: remote
Size: 224 B
created dataset
created dataset from body_ten.csv
`
if diff := cmp.Diff(expect, actual); diff != "" {
Expand Down Expand Up @@ -164,7 +164,7 @@ func TestFetchCommand(t *testing.T) {
t.Errorf("expected status code 200, got %d", actualStatusCode)
}
actualBody = string(fixTs.ReplaceAll([]byte(actualBody), []byte(`"commitTime":"timeStampHere"`)))
expectBody := `{"data":[{"username":"peer_a","profileID":"QmeL2mdVka1eahKENjehK6tBxkkpk5dNQ1qMcgWi7Hrb4B","name":"test_movies","path":"/ipfs/QmX3MtEVFdJMfAFaHvUsf6BVMS2m4Qw1huXUf1Gi3DPyUc","bodySize":720,"commitTime":"timeStampHere","commitTitle":"body changed by 70%","commitMessage":"body:\n\tchanged by 70%"},{"username":"peer_a","profileID":"QmeL2mdVka1eahKENjehK6tBxkkpk5dNQ1qMcgWi7Hrb4B","name":"test_movies","path":"/ipfs/QmQ8xUVwptw46kxxNBAw974LZsY58kCWorCbF7Bf2n2D5n","bodySize":224,"commitTime":"timeStampHere","commitTitle":"created dataset","commitMessage":"created dataset"}],"meta":{"code":200},"pagination":{"nextUrl":"/history/peer_a/test_movies?page=2"}}`
expectBody := `{"data":[{"username":"peer_a","profileID":"QmeL2mdVka1eahKENjehK6tBxkkpk5dNQ1qMcgWi7Hrb4B","name":"test_movies","path":"/ipfs/Qmde2e5etUdVtBzHdvnsPXU4mFmy3yepw72rWz3CQ8JQ3v","bodySize":720,"commitTime":"timeStampHere","commitTitle":"body changed by 70%","commitMessage":"body:\n\tchanged by 70%"},{"username":"peer_a","profileID":"QmeL2mdVka1eahKENjehK6tBxkkpk5dNQ1qMcgWi7Hrb4B","name":"test_movies","path":"/ipfs/QmXte9h1Ztm1nyd4G1CUjWnkL82T2eY7qomMfY4LUXsn3Z","bodySize":224,"commitTime":"timeStampHere","commitTitle":"created dataset from body_ten.csv","commitMessage":"created dataset from body_ten.csv"}],"meta":{"code":200},"pagination":{"nextUrl":"/history/peer_a/test_movies?page=2"}}`
if diff := cmp.Diff(expectBody, actualBody); diff != "" {
t.Errorf("body mismatch (-want +got):%s\n", diff)
}
Expand All @@ -183,7 +183,7 @@ func TestFetchCommand(t *testing.T) {
t.Errorf("expected status code 200, got %d", actualStatusCode)
}
actualBody = string(fixTs.ReplaceAll([]byte(actualBody), []byte(`"commitTime":"timeStampHere"`)))
expectBody = `{"data":[{"username":"peer_a","name":"test_movies","path":"/ipfs/QmX3MtEVFdJMfAFaHvUsf6BVMS2m4Qw1huXUf1Gi3DPyUc","foreign":true,"bodySize":720,"commitTime":"timeStampHere","commitTitle":"body changed by 70%"},{"username":"peer_a","name":"test_movies","path":"/ipfs/QmQ8xUVwptw46kxxNBAw974LZsY58kCWorCbF7Bf2n2D5n","foreign":true,"bodySize":224,"commitTime":"timeStampHere","commitTitle":"created dataset"}],"meta":{"code":200}}`
expectBody = `{"data":[{"username":"peer_a","name":"test_movies","path":"/ipfs/Qmde2e5etUdVtBzHdvnsPXU4mFmy3yepw72rWz3CQ8JQ3v","foreign":true,"bodySize":720,"commitTime":"timeStampHere","commitTitle":"body changed by 70%"},{"username":"peer_a","name":"test_movies","path":"/ipfs/QmXte9h1Ztm1nyd4G1CUjWnkL82T2eY7qomMfY4LUXsn3Z","foreign":true,"bodySize":224,"commitTime":"timeStampHere","commitTitle":"created dataset from body_ten.csv"}],"meta":{"code":200}}`
if diff := cmp.Diff(expectBody, actualBody); diff != "" {
t.Errorf("body mismatch (-want +got):%s\n", diff)
}
Expand Down
12 changes: 6 additions & 6 deletions cmd/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ commit:
timestamp: "2001-01-01T01:05:01.000000001Z"
title: body changed by 54%
name: my_ds
path: /ipfs/QmPnfCL3X8z2Jqv9ZJUqvWpy2JFJpQJkapLfjXhXzoWiFR
path: /ipfs/QmUJXnzYfYjXLxxLLvaK6BDrSenQGUsaxHF7Skw7WDLZGT
peername: test_peer
previousPath: /ipfs/QmQ8xUVwptw46kxxNBAw974LZsY58kCWorCbF7Bf2n2D5n
previousPath: /ipfs/QmXte9h1Ztm1nyd4G1CUjWnkL82T2eY7qomMfY4LUXsn3Z
qri: ds:0
structure:
checksum: QmSa4i985cF3dxNHxD5mSN7c6q1eYa83uNo1pLRmPZgTsa
Expand Down Expand Up @@ -111,14 +111,14 @@ structure:
commit:
author:
id: QmeL2mdVka1eahKENjehK6tBxkkpk5dNQ1qMcgWi7Hrb4B
message: created dataset
path: /ipfs/QmNodcAnA9NXfU32NxRWRY1yUXeyNem4R9FMcchtgBtob1
message: created dataset from body_ten.csv
path: /ipfs/QmTPieEVLviZbkiAKiPm5gzeEcovWRJ9mK2f6FHQ8sH82a
qri: cm:0
signature: njCFxpGqq0xJSrjgxC289KncjflqA0e00txweEqIyUTvEKSUBKHcfQmx4OQIJzJqQJdcjIEzFrwP9cdquozRgsnrpsSfKb+wBWdtbnrg8zfat0X/Dqjro6JD7afJf0gU9s5SDi/s8g/qZOLwWh1nuoH4UAeUX+l3DH0ocFjeD6r/YkMJ0KXaWaFloKP8UPasfqoei9PxxmYQuAnFMqpXFisB7mKFAbgbpF3eL80UcbQPTih7WF11SBym/AzJhGNvOivOjmRxKGEuqEH9g3NPTEQr+LnP415X4qiaZA6MVmOO66vC0diUN4vJUMvhTsWnVEBtgqjTRYlSaYwabHv/gA==
timestamp: "2001-01-01T01:02:01.000000001Z"
title: created dataset
title: created dataset from body_ten.csv
name: my_ds
path: /ipfs/QmQ8xUVwptw46kxxNBAw974LZsY58kCWorCbF7Bf2n2D5n
path: /ipfs/QmXte9h1Ztm1nyd4G1CUjWnkL82T2eY7qomMfY4LUXsn3Z
peername: test_peer
qri: ds:0
structure:
Expand Down
18 changes: 9 additions & 9 deletions cmd/log_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ func TestLogAndDeletes(t *testing.T) {
t.Fatal(err)
}
output := run.GetCommandOutput()
expect := `1 Commit: /ipfs/QmRyXnBUvGGFcdCebrwby4NWak7RvLrqciC9tstdVFZb1Z
expect := `1 Commit: /ipfs/QmQUBYGKBJGp1R5tCURnMJL6Bb7v1v3N32gpkqci6VcM98
Date: Sun Dec 31 20:02:01 EST 2000
Storage: local
Size: 79 B
created dataset
created dataset from body_two.json
`
if diff := cmpTextLines(expect, output); diff != "" {
Expand All @@ -45,7 +45,7 @@ func TestLogAndDeletes(t *testing.T) {
t.Fatal(err)
}
output = run.GetCommandOutput()
expect = `1 Commit: /ipfs/QmduBA2otXgFpfiySh6fboBb3R6sfqGCAJN1snbsYu67ZM
expect = `1 Commit: /ipfs/QmQJX35zUadkoXjTW3uBksyWgiNKvziVpUsVmxi5nJjDqk
Date: Sun Dec 31 20:05:01 EST 2000
Storage: local
Size: 137 B
Expand All @@ -55,12 +55,12 @@ func TestLogAndDeletes(t *testing.T) {
added row 2
added row 3
2 Commit: /ipfs/QmRyXnBUvGGFcdCebrwby4NWak7RvLrqciC9tstdVFZb1Z
2 Commit: /ipfs/QmQUBYGKBJGp1R5tCURnMJL6Bb7v1v3N32gpkqci6VcM98
Date: Sun Dec 31 20:02:01 EST 2000
Storage: local
Size: 79 B
created dataset
created dataset from body_two.json
`
if diff := cmpTextLines(expect, output); diff != "" {
Expand All @@ -79,12 +79,12 @@ func TestLogAndDeletes(t *testing.T) {
t.Fatal(err)
}
output = run.GetCommandOutput()
expect = `1 Commit: /ipfs/QmRyXnBUvGGFcdCebrwby4NWak7RvLrqciC9tstdVFZb1Z
expect = `1 Commit: /ipfs/QmQUBYGKBJGp1R5tCURnMJL6Bb7v1v3N32gpkqci6VcM98
Date: Sun Dec 31 20:02:01 EST 2000
Storage: local
Size: 79 B
created dataset
created dataset from body_two.json
`
if diff := cmpTextLines(expect, output); diff != "" {
Expand Down Expand Up @@ -113,12 +113,12 @@ func TestLogAndDeletes(t *testing.T) {
t.Fatal(err)
}
output = run.GetCommandOutput()
expect = `1 Commit: /ipfs/Qmf5DDCr8yPv4f149suQcdrC5ePJdzddbDi1hCLrfMnme7
expect = `1 Commit: /ipfs/QmZKQ8WF6RbwQ1K3kvjT3BpxRPfNsphGE9165bm56crzxF
Date: Sun Dec 31 20:07:01 EST 2000
Storage: local
Size: 224 B
created dataset
created dataset from body_ten.csv
`
if diff := cmpTextLines(expect, output); diff != "" {
Expand Down
Loading

0 comments on commit 10403f5

Please sign in to comment.