diff --git a/Documentation/rest-api-changes.txt b/Documentation/rest-api-changes.txt index 6fd0ef20ac..1b05462a47 100644 --- a/Documentation/rest-api-changes.txt +++ b/Documentation/rest-api-changes.txt @@ -1317,14 +1317,15 @@ content isn't provided, it is wiped out for that file. As response ---- [[post-edit]] -=== Restore file content in Change Edit +=== Restore file content or rename files in Change Edit -- 'POST /changes/link:#change-id[\{change-id\}]/edit -- -Creates empty change edit or restores file content in change edit. The -request body needs to include a link:#change-edit-input[ChangeEditInput] -entity when a file within change edit should be restored. +Creates empty change edit, restores file content or renames files in change +edit. The request body needs to include a +link:#change-edit-input[ChangeEditInput] entity when a file within change +edit should be restored or old and new file names to rename a file. .Request ---- @@ -1336,9 +1337,23 @@ entity when a file within change edit should be restored. } ---- +or for rename: + +.Request +---- + POST /changes/myProject~master~I8473b95934b5732ac55d26311a706c9c2bde9940/edit HTTP/1.0 + Content-Type: application/json; charset=UTF-8 + + { + "old_path": "foo", + "new_path": "bar" + } +---- + When change edit doesn't exist for this change yet it is created. When path -and restore flag are provided in request body, this file is restored. As -response "`204 No Content`" is returned. +and restore flag are provided in request body, this file is restored. When +old and new file names are provided, the file is renamed. As response +"`204 No Content`" is returned. .Response ---- @@ -4066,6 +4081,8 @@ path within change edit. |=========================== |Field Name ||Description |`restore_path`|optional|Path to file to restore. +|`old_path` |optional|Old path to file to rename. +|`new_path` |optional|New path to file to rename. |=========================== [[change-edit-message-input]] diff --git a/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/edit/ChangeEditIT.java b/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/edit/ChangeEditIT.java index a1168a8fad..25fce0bb0f 100644 --- a/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/edit/ChangeEditIT.java +++ b/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/edit/ChangeEditIT.java @@ -434,6 +434,25 @@ public class ChangeEditIT extends AbstractDaemonTest { ObjectId.fromString(edit.get().getRevision().get()), FILE_NAME), CONTENT_OLD); } + @Test + public void renameFileRest() throws Exception { + assertThat(modifier.createEdit(change, ps)).isEqualTo(RefUpdate.Result.NEW); + Post.Input in = new Post.Input(); + in.oldPath = FILE_NAME; + in.newPath = FILE_NAME3; + assertThat(adminSession.post(urlEdit(), in).getStatusCode()).isEqualTo( + SC_NO_CONTENT); + Optional edit = editUtil.byChange(change); + assertByteArray(fileUtil.getContent(projectCache.get(edit.get().getChange().getProject()), + ObjectId.fromString(edit.get().getRevision().get()), FILE_NAME3), CONTENT_OLD); + try { + fileUtil.getContent(projectCache.get(edit.get().getChange().getProject()), + ObjectId.fromString(edit.get().getRevision().get()), FILE_NAME); + fail("ResourceNotFoundException expected"); + } catch (ResourceNotFoundException rnfe) { + } + } + @Test public void restoreDeletedFileInPatchSetRest() throws Exception { Post.Input in = new Post.Input(); diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/change/ChangeEdits.java b/gerrit-server/src/main/java/com/google/gerrit/server/change/ChangeEdits.java index 4f1104429f..0759370578 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/server/change/ChangeEdits.java +++ b/gerrit-server/src/main/java/com/google/gerrit/server/change/ChangeEdits.java @@ -311,6 +311,8 @@ public class ChangeEdits implements RestModifyView { public static class Input { public String restorePath; + public String oldPath; + public String newPath; } private final Provider db; @@ -335,8 +337,13 @@ public class ChangeEdits implements edit = createEdit(resource.getChange()); } - if (input != null && !Strings.isNullOrEmpty(input.restorePath)) { - editModifier.restoreFile(edit.get(), input.restorePath); + if (input != null) { + if (!Strings.isNullOrEmpty(input.restorePath)) { + editModifier.restoreFile(edit.get(), input.restorePath); + } else if (!Strings.isNullOrEmpty(input.oldPath) + && !Strings.isNullOrEmpty(input.newPath)) { + editModifier.renameFile(edit.get(), input.oldPath, input.newPath); + } } return Response.none(); }