commit b353a198c5844a2e9b078ac654b6a61de00b5a13 from: Stefan Sperling date: Wed Aug 07 15:40:15 2019 UTC add 'q' response to 'got stage -p' commit - a61a44146db63a72f7bf98b08e5d16b18ab209da commit + b353a198c5844a2e9b078ac654b6a61de00b5a13 blob - 601a65eadb4dcaff86ccb43efd94fb6ff9183663 blob + 9697e4dbb758aa0d21c5cd4e2a6cd17e2e20360c --- got/got.1 +++ got/got.1 @@ -921,9 +921,11 @@ among the specified paths. Otherwise, show all staged Instead of staging the entire content of a changed file, interactively select or reject changes for staging based on .Dq y -and +(select), .Dq n -responses. +(reject), and +.Dq q +(quit) responses. If a file is in modified status, individual patches derived from the modified file content can be staged. Files in added or deleted status may be staged or rejected in their entirety. blob - 71b71a09c2f121a9962882e618c136f256b2105a blob + fefcde6d313e10e1fab631ab23cad7d8ef7aab19 --- got/got.c +++ got/got.c @@ -5200,10 +5200,10 @@ show_change(unsigned char status, const char *path, FI switch (status) { case GOT_STATUS_ADD: - printf("A %s\nstage this addition? [y/n] ", path); + printf("A %s\nstage this addition? [y/n/q] ", path); break; case GOT_STATUS_DELETE: - printf("D %s\nstage deletion? [y/n] ", path); + printf("D %s\nstage deletion? [y/n/q] ", path); break; case GOT_STATUS_MODIFY: if (fseek(patch_file, 0L, SEEK_SET) == -1) @@ -5214,7 +5214,7 @@ show_change(unsigned char status, const char *path, FI if (ferror(patch_file)) return got_error_from_errno("getline"); printf(GOT_COMMIT_SEP_STR); - printf("M %s\nstage this change? [y/n] ", path); + printf("M %s\nstage this change? [y/n/q] ", path); break; default: return got_error_path(path, GOT_ERR_FILE_STATUS); @@ -5257,19 +5257,23 @@ choose_patch(int *choice, void *arg, unsigned char sta if (strcmp(line, "n") == 0) { *choice = GOT_PATCH_CHOICE_NO; printf("n\n"); + } + if (strcmp(line, "q") == 0) { + *choice = GOT_PATCH_CHOICE_QUIT; + printf("q\n"); } free(line); return NULL; } - while (resp != 'y' && resp != 'n') { + while (resp != 'y' && resp != 'n' && resp != 'q') { err = show_change(status, path, patch_file); if (err) return err; resp = getchar(); if (resp == '\n') resp = getchar(); - if (resp != 'y' && resp != 'n') + if (resp != 'y' && resp != 'n' && resp != 'q') printf("invalid response '%c'\n", resp); } @@ -5277,6 +5281,8 @@ choose_patch(int *choice, void *arg, unsigned char sta *choice = GOT_PATCH_CHOICE_YES; else if (resp == 'n') *choice = GOT_PATCH_CHOICE_NO; + else if (resp == 'q') + *choice = GOT_PATCH_CHOICE_QUIT; return NULL; } blob - 0edf139bd3d1ced5c9179e07ffed8843f424b0ea blob + c4b942501561a17043e8895b1f6f2779fd3ebb10 --- include/got_worktree.h +++ include/got_worktree.h @@ -381,6 +381,7 @@ typedef const struct got_error *(*got_worktree_patch_c #define GOT_PATCH_CHOICE_NONE 0 #define GOT_PATCH_CHOICE_YES 1 #define GOT_PATCH_CHOICE_NO 2 +#define GOT_PATCH_CHOICE_QUIT 3 /* * Stage the specified paths for commit. blob - 3c1ffb4a2a5de47f3d61e5f8f8fded152ed6b987 blob + 3bfbe160afb3d00eb393f76db0f5af2ee423f030 --- lib/worktree.c +++ lib/worktree.c @@ -5274,6 +5274,15 @@ apply_or_reject_change(int *choice, struct got_diff_ch (*line_cur2)++; } break; + case GOT_PATCH_CHOICE_QUIT: + /* Copy old file's lines until EOF. */ + while (!feof(f1)) { + err = copy_one_line(f1, outfile); + if (err) + goto done; + (*line_cur1)++; + } + break; default: err = got_error(GOT_ERR_PATCH_CHOICE); break; @@ -5355,6 +5364,8 @@ create_staged_content(char **path_outfile, struct got_ goto done; if (choice == GOT_PATCH_CHOICE_YES) have_content = 1; + else if (choice == GOT_PATCH_CHOICE_QUIT) + break; } done: free(id_str); blob - 4bc1f4252276a19fd35ea26cf76d444bfaabe503 blob + 7b8a5cf105fc8e4033ecdf70fa582d32a8962e93 --- regress/cmdline/stage.sh +++ regress/cmdline/stage.sh @@ -1181,7 +1181,7 @@ function test_stage_patch { 5 ----------------------------------------------- M numbers -stage this change? [y/n] n +stage this change? [y/n/q] n ----------------------------------------------- @@ -4,7 +4,7 @@ 4 @@ -1194,7 +1194,7 @@ stage this change? [y/n] n 10 ----------------------------------------------- M numbers -stage this change? [y/n] n +stage this change? [y/n/q] n ----------------------------------------------- @@ -13,4 +13,4 @@ 13 @@ -1204,7 +1204,7 @@ stage this change? [y/n] n +c ----------------------------------------------- M numbers -stage this change? [y/n] n +stage this change? [y/n/q] n EOF cmp -s $testroot/stdout.expected $testroot/stdout ret="$?" @@ -1240,7 +1240,7 @@ EOF 5 ----------------------------------------------- M numbers -stage this change? [y/n] n +stage this change? [y/n/q] n ----------------------------------------------- @@ -4,7 +4,7 @@ 4 @@ -1253,7 +1253,7 @@ stage this change? [y/n] n 10 ----------------------------------------------- M numbers -stage this change? [y/n] y +stage this change? [y/n/q] y ----------------------------------------------- @@ -13,4 +13,4 @@ 13 @@ -1263,7 +1263,7 @@ stage this change? [y/n] y +c ----------------------------------------------- M numbers -stage this change? [y/n] n +stage this change? [y/n/q] n EOF cmp -s $testroot/stdout.expected $testroot/stdout ret="$?" @@ -1346,7 +1346,7 @@ EOF 5 ----------------------------------------------- M numbers -stage this change? [y/n] n +stage this change? [y/n/q] n ----------------------------------------------- @@ -4,7 +4,7 @@ 4 @@ -1359,7 +1359,7 @@ stage this change? [y/n] n 10 ----------------------------------------------- M numbers -stage this change? [y/n] n +stage this change? [y/n/q] n ----------------------------------------------- @@ -13,4 +13,4 @@ 13 @@ -1369,7 +1369,7 @@ stage this change? [y/n] n +c ----------------------------------------------- M numbers -stage this change? [y/n] y +stage this change? [y/n/q] y EOF cmp -s $testroot/stdout.expected $testroot/stdout ret="$?" @@ -1435,7 +1435,7 @@ function test_stage_patch_added { epsilon/new > $testroot/stdout) echo "A epsilon/new" > $testroot/stdout.expected - echo "stage this addition? [y/n] y" >> $testroot/stdout.expected + echo "stage this addition? [y/n/q] y" >> $testroot/stdout.expected cmp -s $testroot/stdout.expected $testroot/stdout ret="$?" if [ "$ret" != "0" ]; then @@ -1494,7 +1494,7 @@ function test_stage_patch_removed { echo -n > $testroot/stdout.expected echo "D beta" > $testroot/stdout.expected - echo "stage deletion? [y/n] y" >> $testroot/stdout.expected + echo "stage deletion? [y/n/q] y" >> $testroot/stdout.expected cmp -s $testroot/stdout.expected $testroot/stdout ret="$?" if [ "$ret" != "0" ]; then @@ -1532,7 +1532,109 @@ function test_stage_patch_removed { fi test_done "$testroot" "$ret" } + +function test_stage_patch_quit { + local testroot=`test_init stage_patch_quit` + + jot 16 > $testroot/repo/numbers + (cd $testroot/repo && git add numbers) + git_commit $testroot/repo -m "added numbers file" + local commit_id=`git_show_head $testroot/repo` + + got checkout $testroot/repo $testroot/wt > /dev/null + ret="$?" + if [ "$ret" != "0" ]; then + test_done "$testroot" "$ret" + return 1 + fi + sed -i -e 's/^2$/a/' $testroot/wt/numbers + sed -i -e 's/^7$/b/' $testroot/wt/numbers + sed -i -e 's/^16$/c/' $testroot/wt/numbers + + # stage first hunk and quit + printf "y\nq\n" > $testroot/patchscript + (cd $testroot/wt && got stage -F $testroot/patchscript -p \ + numbers > $testroot/stdout) + ret="$?" + if [ "$ret" != "0" ]; then + echo "got stage command failed unexpectedly" >&2 + test_done "$testroot" "1" + return 1 + fi + cat > $testroot/stdout.expected < $testroot/stdout) + echo "MM numbers" > $testroot/stdout.expected + cmp -s $testroot/stdout.expected $testroot/stdout + ret="$?" + if [ "$ret" != "0" ]; then + diff -u $testroot/stdout.expected $testroot/stdout + test_done "$testroot" "$ret" + return 1 + fi + + (cd $testroot/wt && got diff -s > $testroot/stdout) + + echo "diff $commit_id $testroot/wt (staged changes)" \ + > $testroot/stdout.expected + echo -n 'blob - ' >> $testroot/stdout.expected + got tree -r $testroot/repo -i -c $commit_id \ + | grep 'numbers$' | cut -d' ' -f 1 \ + >> $testroot/stdout.expected + echo -n 'blob + ' >> $testroot/stdout.expected + (cd $testroot/wt && got stage -l numbers) | cut -d' ' -f 1 \ + >> $testroot/stdout.expected + echo "--- numbers" >> $testroot/stdout.expected + echo "+++ numbers" >> $testroot/stdout.expected + echo "@@ -1,5 +1,5 @@" >> $testroot/stdout.expected + echo " 1" >> $testroot/stdout.expected + echo "-2" >> $testroot/stdout.expected + echo "+a" >> $testroot/stdout.expected + echo " 3" >> $testroot/stdout.expected + echo " 4" >> $testroot/stdout.expected + echo " 5" >> $testroot/stdout.expected + cmp -s $testroot/stdout.expected $testroot/stdout + ret="$?" + if [ "$ret" != "0" ]; then + diff -u $testroot/stdout.expected $testroot/stdout + fi + test_done "$testroot" "$ret" + +} + run_test test_stage_basic run_test test_stage_no_changes run_test test_stage_list @@ -1552,3 +1654,4 @@ run_test test_stage_commit run_test test_stage_patch run_test test_stage_patch_added run_test test_stage_patch_removed +run_test test_stage_patch_quit