]> git.ipfire.org Git - thirdparty/git.git/commitdiff
checkout, restore: support the --pathspec-from-file option
authorAlexandr Miloslavskiy <alexandr.miloslavskiy@syntevo.com>
Tue, 3 Dec 2019 14:02:18 +0000 (14:02 +0000)
committerJunio C Hamano <gitster@pobox.com>
Wed, 4 Dec 2019 18:10:37 +0000 (10:10 -0800)
Decisions taken for simplicity:
1) For now, `--pathspec-from-file` is declared incompatible with
   `--patch`, even when <file> is not `stdin`. Such use case it not
   really expected.
2) It is not allowed to pass pathspec in both args and file.

`you must specify path(s) to restore` block was moved down to be able to
test for `pathspec.nr` instead, because testing for `argc` is no longer
correct.

`git switch` does not support the new options because it doesn't expect
`<pathspec>` arguments.

Signed-off-by: Alexandr Miloslavskiy <alexandr.miloslavskiy@syntevo.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Documentation/git-checkout.txt
Documentation/git-restore.txt
builtin/checkout.c
t/t2026-checkout-pathspec-file.sh [new file with mode: 0755]
t/t2072-restore-pathspec-file.sh [new file with mode: 0755]
t/t9902-completion.sh

index a2d420ec62e02c0fd7a5287a398de105723a4e89..c8fb995fa74ee9262ddd71514aeb250376d30a1b 100644 (file)
@@ -13,6 +13,7 @@ SYNOPSIS
 'git checkout' [-q] [-f] [-m] [--detach] <commit>
 'git checkout' [-q] [-f] [-m] [[-b|-B|--orphan] <new_branch>] [<start_point>]
 'git checkout' [-f|--ours|--theirs|-m|--conflict=<style>] [<tree-ish>] [--] <pathspec>...
+'git checkout' [-f|--ours|--theirs|-m|--conflict=<style>] [<tree-ish>] --pathspec-from-file=<file> [--pathspec-file-nul]
 'git checkout' (-p|--patch) [<tree-ish>] [--] [<pathspec>...]
 
 DESCRIPTION
@@ -79,6 +80,7 @@ be used to detach `HEAD` at the tip of the branch (`git checkout
 Omitting `<branch>` detaches `HEAD` at the tip of the current branch.
 
 'git checkout' [-f|--ours|--theirs|-m|--conflict=<style>] [<tree-ish>] [--] <pathspec>...::
+'git checkout' [-f|--ours|--theirs|-m|--conflict=<style>] [<tree-ish>] --pathspec-from-file=<file> [--pathspec-file-nul]::
 
        Overwrite the contents of the files that match the pathspec.
        When the `<tree-ish>` (most often a commit) is not given,
@@ -306,6 +308,19 @@ Note that this option uses the no overlay mode by default (see also
        working tree, but not in `<tree-ish>` are removed, to make them
        match `<tree-ish>` exactly.
 
+--pathspec-from-file=<file>::
+       Pathspec is passed in `<file>` instead of commandline args. If
+       `<file>` is exactly `-` then standard input is used. Pathspec
+       elements are separated by LF or CR/LF. Pathspec elements can be
+       quoted as explained for the configuration variable `core.quotePath`
+       (see linkgit:git-config[1]). See also `--pathspec-file-nul` and
+       global `--literal-pathspecs`.
+
+--pathspec-file-nul::
+       Only meaningful with `--pathspec-from-file`. Pathspec elements are
+       separated with NUL character and all other characters are taken
+       literally (including newlines and quotes).
+
 <branch>::
        Branch to checkout; if it refers to a branch (i.e., a name that,
        when prepended with "refs/heads/", is a valid ref), then that
index d7bf016bba3cf4d13797f1ac8b7c39008cc1e712..5bf60d49434109918fedbc6dd9051a2f65d24316 100644 (file)
@@ -9,6 +9,7 @@ SYNOPSIS
 --------
 [verse]
 'git restore' [<options>] [--source=<tree>] [--staged] [--worktree] [--] <pathspec>...
+'git restore' [<options>] [--source=<tree>] [--staged] [--worktree] --pathspec-from-file=<file> [--pathspec-file-nul]
 'git restore' (-p|--patch) [<options>] [--source=<tree>] [--staged] [--worktree] [--] [<pathspec>...]
 
 DESCRIPTION
@@ -113,6 +114,19 @@ in linkgit:git-checkout[1] for details.
        appear in the `--source` tree are removed, to make them match
        `<tree>` exactly. The default is no-overlay mode.
 
+--pathspec-from-file=<file>::
+       Pathspec is passed in `<file>` instead of commandline args. If
+       `<file>` is exactly `-` then standard input is used. Pathspec
+       elements are separated by LF or CR/LF. Pathspec elements can be
+       quoted as explained for the configuration variable `core.quotePath`
+       (see linkgit:git-config[1]). See also `--pathspec-file-nul` and
+       global `--literal-pathspecs`.
+
+--pathspec-file-nul::
+       Only meaningful with `--pathspec-from-file`. Pathspec elements are
+       separated with NUL character and all other characters are taken
+       literally (including newlines and quotes).
+
 \--::
        Do not interpret any more arguments as options.
 
index 3634a3dac15ae9deca30325ef5b19d5b3e250eb0..b52c490c8f5404e24c05db289fc22539faf276e3 100644 (file)
@@ -70,6 +70,8 @@ struct checkout_opts {
        int checkout_worktree;
        const char *ignore_unmerged_opt;
        int ignore_unmerged;
+       int pathspec_file_nul;
+       const char *pathspec_from_file;
 
        const char *new_branch;
        const char *new_branch_force;
@@ -1480,6 +1482,8 @@ static struct option *add_checkout_path_options(struct checkout_opts *opts,
                OPT_BOOL('p', "patch", &opts->patch_mode, N_("select hunks interactively")),
                OPT_BOOL(0, "ignore-skip-worktree-bits", &opts->ignore_skipworktree,
                         N_("do not limit pathspecs to sparse entries only")),
+               OPT_PATHSPEC_FROM_FILE(&opts->pathspec_from_file),
+               OPT_PATHSPEC_FILE_NUL(&opts->pathspec_file_nul),
                OPT_END()
        };
        struct option *newopts = parse_options_concat(prevopts, options);
@@ -1618,10 +1622,6 @@ static int checkout_main(int argc, const char **argv, const char *prefix,
                        die(_("reference is not a tree: %s"), opts->from_treeish);
        }
 
-       if (opts->accept_pathspec && !opts->empty_pathspec_ok && !argc &&
-           !opts->patch_mode)  /* patch mode is special */
-               die(_("you must specify path(s) to restore"));
-
        if (argc) {
                parse_pathspec(&opts->pathspec, 0,
                               opts->patch_mode ? PATHSPEC_PREFIX_ORIGIN : 0,
@@ -1641,10 +1641,33 @@ static int checkout_main(int argc, const char **argv, const char *prefix,
                if (opts->force_detach)
                        die(_("git checkout: --detach does not take a path argument '%s'"),
                            argv[0]);
+       }
+
+       if (opts->pathspec_from_file) {
+               if (opts->pathspec.nr)
+                       die(_("--pathspec-from-file is incompatible with pathspec arguments"));
+
+               if (opts->force_detach)
+                       die(_("--pathspec-from-file is incompatible with --detach"));
 
+               if (opts->patch_mode)
+                       die(_("--pathspec-from-file is incompatible with --patch"));
+
+               parse_pathspec_file(&opts->pathspec, 0,
+                                   0,
+                                   prefix, opts->pathspec_from_file, opts->pathspec_file_nul);
+       } else if (opts->pathspec_file_nul) {
+               die(_("--pathspec-file-nul requires --pathspec-from-file"));
+       }
+
+       if (opts->pathspec.nr) {
                if (1 < !!opts->writeout_stage + !!opts->force + !!opts->merge)
                        die(_("git checkout: --ours/--theirs, --force and --merge are incompatible when\n"
                              "checking out of the index."));
+       } else {
+               if (opts->accept_pathspec && !opts->empty_pathspec_ok &&
+                   !opts->patch_mode)  /* patch mode is special */
+                       die(_("you must specify path(s) to restore"));
        }
 
        if (opts->new_branch) {
diff --git a/t/t2026-checkout-pathspec-file.sh b/t/t2026-checkout-pathspec-file.sh
new file mode 100755 (executable)
index 0000000..f62fd27
--- /dev/null
@@ -0,0 +1,139 @@
+#!/bin/sh
+
+test_description='checkout --pathspec-from-file'
+
+. ./test-lib.sh
+
+test_tick
+
+test_expect_success setup '
+       test_commit file0 &&
+
+       echo 1 >fileA.t &&
+       echo 1 >fileB.t &&
+       echo 1 >fileC.t &&
+       echo 1 >fileD.t &&
+       git add fileA.t fileB.t fileC.t fileD.t &&
+       git commit -m "files 1" &&
+
+       echo 2 >fileA.t &&
+       echo 2 >fileB.t &&
+       echo 2 >fileC.t &&
+       echo 2 >fileD.t &&
+       git add fileA.t fileB.t fileC.t fileD.t &&
+       git commit -m "files 2" &&
+
+       git tag checkpoint
+'
+
+restore_checkpoint () {
+       git reset --hard checkpoint
+}
+
+verify_expect () {
+       git status --porcelain --untracked-files=no -- fileA.t fileB.t fileC.t fileD.t >actual &&
+       test_cmp expect actual
+}
+
+test_expect_success '--pathspec-from-file from stdin' '
+       restore_checkpoint &&
+
+       echo fileA.t | git checkout --pathspec-from-file=- HEAD^1 &&
+
+       cat >expect <<-\EOF &&
+       M  fileA.t
+       EOF
+       verify_expect
+'
+
+test_expect_success '--pathspec-from-file from file' '
+       restore_checkpoint &&
+
+       echo fileA.t >list &&
+       git checkout --pathspec-from-file=list HEAD^1 &&
+
+       cat >expect <<-\EOF &&
+       M  fileA.t
+       EOF
+       verify_expect
+'
+
+test_expect_success 'NUL delimiters' '
+       restore_checkpoint &&
+
+       printf "fileA.t\0fileB.t\0" | git checkout --pathspec-from-file=- --pathspec-file-nul HEAD^1 &&
+
+       cat >expect <<-\EOF &&
+       M  fileA.t
+       M  fileB.t
+       EOF
+       verify_expect
+'
+
+test_expect_success 'LF delimiters' '
+       restore_checkpoint &&
+
+       printf "fileA.t\nfileB.t\n" | git checkout --pathspec-from-file=- HEAD^1 &&
+
+       cat >expect <<-\EOF &&
+       M  fileA.t
+       M  fileB.t
+       EOF
+       verify_expect
+'
+
+test_expect_success 'no trailing delimiter' '
+       restore_checkpoint &&
+
+       printf "fileA.t\nfileB.t" | git checkout --pathspec-from-file=- HEAD^1 &&
+
+       cat >expect <<-\EOF &&
+       M  fileA.t
+       M  fileB.t
+       EOF
+       verify_expect
+'
+
+test_expect_success 'CRLF delimiters' '
+       restore_checkpoint &&
+
+       printf "fileA.t\r\nfileB.t\r\n" | git checkout --pathspec-from-file=- HEAD^1 &&
+
+       cat >expect <<-\EOF &&
+       M  fileA.t
+       M  fileB.t
+       EOF
+       verify_expect
+'
+
+test_expect_success 'quotes' '
+       restore_checkpoint &&
+
+       printf "\"file\\101.t\"" | git checkout --pathspec-from-file=- HEAD^1 &&
+
+       cat >expect <<-\EOF &&
+       M  fileA.t
+       EOF
+       verify_expect
+'
+
+test_expect_success 'quotes not compatible with --pathspec-file-nul' '
+       restore_checkpoint &&
+
+       printf "\"file\\101.t\"" >list &&
+       test_must_fail git checkout --pathspec-from-file=list --pathspec-file-nul HEAD^1
+'
+
+test_expect_success 'only touches what was listed' '
+       restore_checkpoint &&
+
+       printf "fileB.t\nfileC.t\n" | git checkout --pathspec-from-file=- HEAD^1 &&
+
+       cat >expect <<-\EOF &&
+       M  fileB.t
+       M  fileC.t
+       EOF
+       verify_expect
+'
+
+test_done
diff --git a/t/t2072-restore-pathspec-file.sh b/t/t2072-restore-pathspec-file.sh
new file mode 100755 (executable)
index 0000000..db58e83
--- /dev/null
@@ -0,0 +1,139 @@
+#!/bin/sh
+
+test_description='restore --pathspec-from-file'
+
+. ./test-lib.sh
+
+test_tick
+
+test_expect_success setup '
+       test_commit file0 &&
+
+       echo 1 >fileA.t &&
+       echo 1 >fileB.t &&
+       echo 1 >fileC.t &&
+       echo 1 >fileD.t &&
+       git add fileA.t fileB.t fileC.t fileD.t &&
+       git commit -m "files 1" &&
+
+       echo 2 >fileA.t &&
+       echo 2 >fileB.t &&
+       echo 2 >fileC.t &&
+       echo 2 >fileD.t &&
+       git add fileA.t fileB.t fileC.t fileD.t &&
+       git commit -m "files 2" &&
+
+       git tag checkpoint
+'
+
+restore_checkpoint () {
+       git reset --hard checkpoint
+}
+
+verify_expect () {
+       git status --porcelain --untracked-files=no -- fileA.t fileB.t fileC.t fileD.t >actual &&
+       test_cmp expect actual
+}
+
+test_expect_success '--pathspec-from-file from stdin' '
+       restore_checkpoint &&
+
+       echo fileA.t | git restore --pathspec-from-file=- --source=HEAD^1 &&
+
+       cat >expect <<-\EOF &&
+        M fileA.t
+       EOF
+       verify_expect
+'
+
+test_expect_success '--pathspec-from-file from file' '
+       restore_checkpoint &&
+
+       echo fileA.t >list &&
+       git restore --pathspec-from-file=list --source=HEAD^1 &&
+
+       cat >expect <<-\EOF &&
+        M fileA.t
+       EOF
+       verify_expect
+'
+
+test_expect_success 'NUL delimiters' '
+       restore_checkpoint &&
+
+       printf "fileA.t\0fileB.t\0" | git restore --pathspec-from-file=- --pathspec-file-nul --source=HEAD^1 &&
+
+       cat >expect <<-\EOF &&
+        M fileA.t
+        M fileB.t
+       EOF
+       verify_expect
+'
+
+test_expect_success 'LF delimiters' '
+       restore_checkpoint &&
+
+       printf "fileA.t\nfileB.t\n" | git restore --pathspec-from-file=- --source=HEAD^1 &&
+
+       cat >expect <<-\EOF &&
+        M fileA.t
+        M fileB.t
+       EOF
+       verify_expect
+'
+
+test_expect_success 'no trailing delimiter' '
+       restore_checkpoint &&
+
+       printf "fileA.t\nfileB.t" | git restore --pathspec-from-file=- --source=HEAD^1 &&
+
+       cat >expect <<-\EOF &&
+        M fileA.t
+        M fileB.t
+       EOF
+       verify_expect
+'
+
+test_expect_success 'CRLF delimiters' '
+       restore_checkpoint &&
+
+       printf "fileA.t\r\nfileB.t\r\n" | git restore --pathspec-from-file=- --source=HEAD^1 &&
+
+       cat >expect <<-\EOF &&
+        M fileA.t
+        M fileB.t
+       EOF
+       verify_expect
+'
+
+test_expect_success 'quotes' '
+       restore_checkpoint &&
+
+       printf "\"file\\101.t\"" | git restore --pathspec-from-file=- --source=HEAD^1 &&
+
+       cat >expect <<-\EOF &&
+        M fileA.t
+       EOF
+       verify_expect
+'
+
+test_expect_success 'quotes not compatible with --pathspec-file-nul' '
+       restore_checkpoint &&
+
+       printf "\"file\\101.t\"" >list &&
+       test_must_fail git restore --pathspec-from-file=list --pathspec-file-nul --source=HEAD^1
+'
+
+test_expect_success 'only touches what was listed' '
+       restore_checkpoint &&
+
+       printf "fileB.t\nfileC.t\n" | git restore --pathspec-from-file=- --source=HEAD^1 &&
+
+       cat >expect <<-\EOF &&
+        M fileB.t
+        M fileC.t
+       EOF
+       verify_expect
+'
+
+test_done
index 54f8ce18cb9e510edaf7d21f70ddb075a55304e3..0ed57006cec96dde87aa834adfb370eabfb33b1d 100755 (executable)
@@ -1438,6 +1438,8 @@ test_expect_success 'double dash "git checkout"' '
        --no-guess Z
        --no-... Z
        --overlay Z
+       --pathspec-file-nul Z
+       --pathspec-from-file=Z
        EOF
 '