Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add undo last commit feature #624

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,12 @@ public void showMessageDialog(int title, String msg) {
.setPositiveButton(R.string.label_ok, new DummyDialogListener()).show();
}

public void showMessageDialog(int title, int msg) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(title).setMessage(getString(msg))
.setPositiveButton(R.string.label_ok, new DummyDialogListener()).show();
}

public void showOptionsDialog(int title,final int option_names,
final onOptionDialogClicked[] option_listeners) {
CharSequence[] options_values = getResources().getStringArray(option_names);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import me.sheimi.sgit.activities.delegate.actions.RemoveRemoteAction;
import me.sheimi.sgit.activities.delegate.actions.RepoAction;
import me.sheimi.sgit.activities.delegate.actions.ResetAction;
import me.sheimi.sgit.activities.delegate.actions.UndoAction;
import me.sheimi.sgit.database.models.Repo;
import me.sheimi.sgit.repo.tasks.SheimiAsyncTask.AsyncTaskPostCallback;
import me.sheimi.sgit.repo.tasks.repo.AddToStageTask;
Expand Down Expand Up @@ -55,6 +56,7 @@ private void initActions() {
mActions.add(new PushAction(mRepo, mActivity));
mActions.add(new AddAllAction(mRepo, mActivity));
mActions.add(new CommitAction(mRepo, mActivity));
mActions.add(new UndoAction(mRepo, mActivity));
mActions.add(new ResetAction(mRepo, mActivity));
mActions.add(new MergeAction(mRepo, mActivity));
mActions.add(new FetchAction(mRepo, mActivity));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package me.sheimi.sgit.activities.delegate.actions;

import android.content.DialogInterface;

import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.revwalk.RevCommit;

import java.util.Iterator;

import me.sheimi.sgit.R;
import me.sheimi.sgit.activities.RepoDetailActivity;
import me.sheimi.sgit.database.models.Repo;
import me.sheimi.sgit.exception.StopTaskException;
import me.sheimi.sgit.repo.tasks.SheimiAsyncTask.AsyncTaskPostCallback;
import me.sheimi.sgit.repo.tasks.repo.UndoCommitTask;

public class UndoAction extends RepoAction {

public UndoAction(Repo repo, RepoDetailActivity activity) {
super(repo, activity);
}

@Override
public void execute() {
boolean firstCommit = true;
boolean noCommit = true;
try {
Iterator<RevCommit> logIt = mRepo.getGit().log().call().iterator();
noCommit = !logIt.hasNext();
if (!noCommit) {
logIt.next();
firstCommit = !logIt.hasNext();
}
} catch (GitAPIException | StopTaskException e) {
e.printStackTrace();
}
if (noCommit) {
mActivity.showMessageDialog(R.string.dialog_undo_commit_title, R.string.dialog_undo_no_commit_msg);
} else if (firstCommit) {
mActivity.showMessageDialog(R.string.dialog_undo_commit_title, R.string.dialog_undo_first_commit_msg);
} else {
mActivity.showMessageDialog(R.string.dialog_undo_commit_title,
R.string.dialog_undo_commit_msg, R.string.action_undo,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
undo();
}
});
}
mActivity.closeOperationDrawer();
}

public void undo() {
UndoCommitTask undoTask = new UndoCommitTask(mRepo,
new AsyncTaskPostCallback() {
@Override
public void onPostExecute(Boolean isSuccess) {
mActivity.reset();
}
});
undoTask.executeTask();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package me.sheimi.sgit.repo.tasks.repo;

import org.eclipse.jgit.api.ResetCommand;

import me.sheimi.sgit.R;
import me.sheimi.sgit.database.models.Repo;
import me.sheimi.sgit.exception.StopTaskException;

public class UndoCommitTask extends RepoOpTask {

private AsyncTaskPostCallback mCallback;

public UndoCommitTask(Repo repo, AsyncTaskPostCallback callback) {
super(repo);
mCallback = callback;
setSuccessMsg(R.string.success_undo);
}

@Override
protected Boolean doInBackground(Void... params) {
return undo();
}

protected void onPostExecute(Boolean isSuccess) {
super.onPostExecute(isSuccess);
if (mCallback != null) {
mCallback.onPostExecute(isSuccess);
}
}

public boolean undo() {
try {
mRepo.getGit().getRepository().writeMergeCommitMsg(null);
mRepo.getGit().getRepository().writeMergeHeads(null);
mRepo.getGit().reset().setRef("HEAD~").setMode(ResetCommand.ResetType.SOFT).call();
} catch (StopTaskException e) {
return false;
} catch (Throwable e) {
setException(e);
return false;
}
return true;
}
}
1 change: 1 addition & 0 deletions app/src/main/res/values-de/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<item>Push</item>
<item>Alles zum Staging-Bereich hinzufügen</item>
<item>Commit</item>
<item>Undo Last Commit</item>
<item>Reset (HARD)</item>
<item>Merge</item>
<item>Fetch</item>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-es/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<item>Empujar</item>
<item>Anadir todo a stage</item>
<item>Commit</item>
<item>Undo Last Commit</item>
<item>Reset (DURO)</item>
<item>Merge</item>
<item>Fetch</item>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-fr/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
      <item>Pousser</item>
<item>Ajouter tout au stage</item>
<item>Commit</item>
<item>Undo Last Commit</item>
<item>Reset (HARD)</item>
<item>Merge</item>
<item>Fetch</item>
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/values-iw/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
<item>דחוף</item>
<item>הוסף את כולם לשלב</item>
<item>Commit</item>
<item>Reset (HARD)</item>
<item>Undo Last Commit</item>
<item>Reset (HARD)</item>
<item>מיזוג</item>
<item>Fetch</item>
<item>Rebase</item>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-ja/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<item>プッシュ</item>
<item>すべてステージに追加</item>
<item>コミット</item>
<item>Undo Last Commit</item>
<item>Reset (HARD)</item>
<item>マージ</item>
<item>Fetch</item>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-ko/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<item>Push</item>
<item>Add all to stage</item>
<item>Commit</item>
<item>Undo Last Commit</item>
<item>Reset (HARD)</item>
<item>Merge</item>
<item>Fetch</item>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-ru/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<item>Загрузить</item>
<item>Добавить все</item>
<item>Закоммитить</item>
<item>Undo Last Commit</item>
<item>СБРОС</item>
<item>Слитие</item>
<item>Обновить в новую ветку</item>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-zh-rCN/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<item>推送</item>
<item>暂存所有</item>
<item>提交</item>
<item>撤销上次提交</item>
<item>重置(强制)</item>
<item>合并</item>
<item>获取</item>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<string name="action_share_diff">分享 diff</string>
<string name="action_save_diff">保存 diff</string>
<string name="action_send_report">发送报告</string>
<string name="action_undo">撤销</string>

<!-- label msg -->
<string name="label_ok">确定</string>
Expand Down Expand Up @@ -192,4 +193,5 @@
<string name="error_file_not_found">File not found!</string>
<string name="dialog_access_all_files_title">所有文件的访问</string>
<string name="dialog_access_all_files_msg">请允许访问所有文件以从外部存储读取/写入存储库。</string>

</resources>
5 changes: 5 additions & 0 deletions app/src/main/res/values-zh-rCN/strings_dialog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,9 @@
<string name="dialog_fetch_positive_button">抓取</string>
<string name="dialog_fetch_all_button">抓取所有</string>

<!-- Undo -->
<string name="dialog_undo_commit_msg">您确定要撤销上次提交吗?</string>
<string name="dialog_undo_commit_title">撤销上次提交</string>
<string name="dialog_undo_no_commit_msg">没有历史提交!</string>
<string name="dialog_undo_first_commit_msg">首次提交无法撤销!</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-zh-rCN/strings_success_msg.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@
<string name="success_save">文件成功保存</string>
<string name="msg_now_you_can_edit">文件可以被编辑</string>
<string name="msg_commit_str_has_copied">已复制添加的 Hash 值</string>
<string name="success_undo">上次提交已被撤销</string>

</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<item>Push</item>
<item>Add all to stage</item>
<item>Commit</item>
<item>Undo Last Commit</item>
<item>Reset (HARD)</item>
<item>Merge</item>
<item>Fetch</item>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<string name="action_share_diff">Share diff</string>
<string name="action_save_diff">Save diff</string>
<string name="action_send_report">Send report</string>
<string name="action_undo">Undo</string>

<!-- label msg -->
<string name="label_ok">OK</string>
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/values/strings_dialog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,10 @@
<string name="dialog_fetch_positive_button">Fetch</string>
<string name="dialog_fetch_all_button">Fetch All</string>

<!-- Undo -->
<string name="dialog_undo_commit_title">Undo Last Commit</string>
<string name="dialog_undo_commit_msg">Are you sure you want to undo last commit?</string>
<string name="dialog_undo_no_commit_msg">No commit to undo!</string>
<string name="dialog_undo_first_commit_msg">Cannot undo the initial commit!</string>

</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings_success_msg.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@
<string name="success_save">File saved Successfully</string>
<string name="msg_now_you_can_edit">File can be edited</string>
<string name="msg_commit_str_has_copied">Commit hash has been copied</string>
<string name="success_undo">Last commit have been undone</string>

</resources>