diff --git a/errors/errors.go b/errors/errors.go index 4c2b277..51804ad 100644 --- a/errors/errors.go +++ b/errors/errors.go @@ -33,6 +33,7 @@ const ( CodeUnableToDeletePasswordFile Code = 30 CodeUnableToDetermineIsDirectoryEmpty Code = 31 CodeUnableToDeleteEmptyDirectory Code = 32 + CodeUnableToCommitPasswordFile Code = 33 ) // Field extra field in the error response params diff --git a/helpers/helpers.go b/helpers/helpers.go index 9877a1c..2acc4fb 100644 --- a/helpers/helpers.go +++ b/helpers/helpers.go @@ -112,3 +112,21 @@ func IsDirectoryEmpty(dirPath string) (bool, error) { return false, err } + +func GitAddAndCommit(storePath string, filePath string, message string) error { + cmd := exec.Command("git", "add", filePath) + cmd.Dir = storePath + _, err := cmd.Output() + if err != nil { + return err + } + + cmd = exec.Command("git", "commit", "-m", message, "-o", filePath) // Only commit the file we added + cmd.Dir = storePath + _, err = cmd.Output() + if err != nil { + return err + } + + return nil +} diff --git a/request/save.go b/request/save.go index 12f4b78..8e705a9 100644 --- a/request/save.go +++ b/request/save.go @@ -3,6 +3,7 @@ package request import ( "path/filepath" "strings" + "fmt" "github.com/browserpass/browserpass-native/errors" "github.com/browserpass/browserpass-native/helpers" @@ -149,5 +150,25 @@ func saveEncryptedContents(request *request) { ) } + err = helpers.GitAddAndCommit(store.Path, filePath, fmt.Sprintf("browserpass: save %s", request.File)) + if err != nil { + log.Errorf( + "Unable to commit the password file '%v' in the password store '%+v': %+v", + request.File, store, err, + ) + response.SendErrorAndExit( + errors.CodeUnableToCommitPasswordFile, + &map[errors.Field]string{ + errors.FieldMessage: "Unable to commit the password file", + errors.FieldAction: "save", + errors.FieldError: err.Error(), + errors.FieldFile: request.File, + errors.FieldStoreID: store.ID, + errors.FieldStoreName: store.Name, + errors.FieldStorePath: store.Path, + }, + ) + } + response.SendOk(responseData) }