Skip to content

Commit

Permalink
shellgit: Ensure the passed filepath to diff-index is interpreted as …
Browse files Browse the repository at this point in the history
…filepath

Simplified, the code in the past was like this
(data/roles/pe_primary.yaml is a random test file):

`git diff-index -p HEAD data/roles/pe_primary.yaml`

Which results in the following error if the file is tracked in git but
was deleted locally:

```
$ git status
HEAD detached at 3140475
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	deleted:    data/roles/pe_primary.yaml

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	.r10k-deploy.json
	.resource_types/
	modules/

no changes added to commit (use "git add" and/or "git commit -a")
$ git diff-index -p HEAD data/roles/pe_primary.yaml
fatal: ambiguous argument 'data/roles/pe_primary.yaml': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
$ echo $?
128
```

With adding `--` to the command, we ensure it's interpreted as path.
This changes the exit code to 0.

```
$ git status
HEAD detached at 3140475
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	deleted:    data/roles/pe_primary.yaml

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	.r10k-deploy.json
	.resource_types/
	modules/

no changes added to commit (use "git add" and/or "git commit -a")
$ git diff-index -p HEAD -- data/roles/pe_primary.yaml
diff --git a/data/roles/pe_primary.yaml b/data/roles/pe_primary.yaml
deleted file mode 100644
index b827047..0000000
--- a/data/roles/pe_primary.yaml
+++ /dev/null
@@ -1,48 +0,0 @@
----
-classes:
-  - profiles::nftables
$ echo $?
0
```

Why do we do this change? This ensures r10k can properly handle
checkouts that were modified.
  • Loading branch information
bastelfreak committed Nov 24, 2023
1 parent fc02338 commit baab3f5
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion lib/r10k/git/shellgit/working_repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def dirty?(exclude_spec=true)
logger.debug(_("Found local modifications in %{file_path}" % {file_path: File.join(@path, file)}))

# Do this in a block so that the extra subprocess only gets invoked when needed.
logger.debug1 { git(['diff-index', '-p', 'HEAD', file], :path => @path.to_s, :raise_on_fail => false).stdout }
logger.debug1 { git(['diff-index', '-p', 'HEAD', '--', file], :path => @path.to_s, :raise_on_fail => false).stdout }
end

return dirty_files.size > 0
Expand Down

0 comments on commit baab3f5

Please sign in to comment.