patch interview questions
Top patch frequently asked interview questions
I've got a file and a patch for it. I'd like to visually apply the patch, t.i. see how the changes proposed by the patch look in context, make some corrections, and save the resulting file.
What tool can do that?
Neither of the visual diff tools (i.e. meld, diffuse, diffmerge) do what I want: they don't work with patches, they merely merge whole files.
Source: (StackOverflow)
What's the best way to go about making a patch for a binary file? I want it to be simple for users to apply(a simple patch
application would be nice). Running diff on the file just gives Binary files [...] differ
Source: (StackOverflow)
I know there are several threads on this already, but no one has fully explained exactly how to perform the initial diff to create the patch file, then how to apply that patch to the initial directory to update it.
In my case, there is a directory of files that anyone can download from the web. I have taken that directory and made changes to it, and want to create a patch file such that others can apply it to the downloaded directory to reproduce exactly what I have in my modified directory.
Help? What do I need to tell the other person with respect to how to apply my patch?
Source: (StackOverflow)
I've got eight commits on a branch that I'd like to email to some people who aren't git enlightened, yet. So far, everything I do either gives me 8 patch files, or starts giving me patch files for every commit in the branch's history, since the beginning of time. I used git rebase --interactive to squash the commits, but now everything I try gives me zillions of patches from the beginning of time. What am I doing wrong?
git format-patch master HEAD # yields zillions of patches, even though there's
# only one commit since master
Source: (StackOverflow)
How can my client apply patch created by git diff
without git installed?
I have tried to use patch
command but it always asks file name to patch.
Source: (StackOverflow)
The situation:
- master is at X;
- quickfix1 is at X + 2 commits
Then I started working on quickfix2, but by accident took quickfix1 as the source branch to copy, not the master. Now quickfix2 is at X + 2 commits + 2 relevant commits.
Now I want to have a branch with quickfix2, but without the 2 commits that belong to quickfix1.
I tried to create a patch from a certain revision in quickfix2, but the patch doesn't preserve the commit history. Is there a way to save my commit history, but have a branch without changes in quickfix1?
Source: (StackOverflow)
I've recently discovered git patch option to the add command, and I must say it really is a fantastic feature.
I also discovered that a large hunk could be split into smaller hunk by hitting the s key, which adds to the precision of the commit.
But what if I want even more precision, if the split hunk is not small enough?
For example, consider this already split hunk:
@@ -34,12 +34,7 @@
width: 440px;
}
-/*#field_teacher_id {
- display: block;
-} */
-
-form.table-form #field_teacher + label,
-form.table-form #field_producer_distributor + label {
+#user-register form.table-form .field-type-checkbox label {
width: 300px;
}
How can I add the CSS comment removal only to the next commit ? The 's' option is not available anymore!
Source: (StackOverflow)
Say I get a patch created with git format-patch
. The file is basically a unified diff with some metadata. If I open the file in Vim, I can see which lines have been modified, but I cannot see which characters in the changed lines differ. Does anyone know a way (in Vim, or some other free software that runs on Ubuntu) to visualize per-character differences?
A counter example where per-character diff is visualized is when executing vimdiff a b
.
update Fri Nov 12 22:36:23 UTC 2010
diffpatch is helpful for the scenario where you're working with a single file.
update Thu Jun 16 17:56:10 UTC 2016
Check out diff-highlight in git 2.9. This script does exactly what I was originally seeking.
Source: (StackOverflow)
I'm trying to use git add --interactive
to selectively add some changes to my index, but I continually receive the "Your edited hunk does not apply. Edit again..." message. I get this message even if I choose the e option, and immediately save/close my editor. In other words, without editing the hunk at all, the patch doesn't apply.
Here's the exact example I'm using (I'm trying to put together a small demo):
Original file:
first change
second change off branch
third change off branch
second change
third change
fourth change
New file:
Change supporting feature 1
first change
second change off branch
third change off branch
second change
third change
fourth change
bug fix 1
change supporting feature 1
I'm trying to show how to use git add --interactive
to only add the "bug fix 1" line to the index. Running interactive add on the file, I choose the patch mode. It presents me with
diff --git a/newfile b/newfile
index 6d501a3..8b81ae9 100644
--- a/newfile
+++ b/newfile
@@ -1,6 +1,9 @@
+Change supporting feature 1
first change
second change off branch
third change off branch
second change
third change
fourth change
+bug fix 1
+change supporting feature 1
I respond with split, followed by "no" to apply the first hunk. The second hunk, I try to edit. I originally tried deleting the bottom line - that didn't work. Leaving the hunk alone completely doesn't work either, and I can't figure out why.
Source: (StackOverflow)
I have two repositories, one is the main repo for a library, and the other is a project using that library.
If I make a fix to the in the subservient project, I'd like an easy way to apply that patch back upstream.
The file's location is different in each repository.
- Main repo:
www.playdar.org/static/playdar.js
- Project:
playlick.com/lib/playdar.js
I tried using git format-patch -- lib/playdar.js
on the playlick project, and then git am
on the main playdar repo, but the differing file locations in the patch file raised an error.
Is there an easy way to apply the patch from a given commit on a given file to another arbitrary file elsewhere?
For bonus points, what if the file you want to apply the patch to isn't in a git repository?
Source: (StackOverflow)
According to my knowledge:
PUT
- update object with its whole representation (replace)
PATCH
- update object with given fields only (update)
I'm using Spring to implement a pretty simple HTTP server. When a user wants to update his data he needs to make a HTTP PATCH
to some endpoint (let's say: api/user
). His request body is mapped to a DTO via @RequestBody
, which looks like this:
class PatchUserRequest {
@Email
@Length(min = 5, max = 50)
var email: String? = null
@Length(max = 100)
var name: String? = null
...
}
Then I use an object of this class to update (patch) the user object:
fun patchWithRequest(userRequest: PatchUserRequest) {
if (!userRequest.email.isNullOrEmpty()) {
email = userRequest.email!!
}
if (!userRequest.name.isNullOrEmpty()) {
name = userRequest.name
}
...
}
My doubt is: what if a client (web app for example) would like to clear a property? I would ignore such a change.
How can I know, if a user wanted to clear a property (he sent me null intentionally) or he just doesn't want to change it? It will be null in my object in both cases.
I can see two options here:
- Agree with the client that if he wants to remove a property he should send me an empty string (but what about dates and other non-string types?)
- Stop using DTO mapping and use a simple map, which will let me check if a field was given empty or not given at all. What about request body validation then? I use
@Valid
right now.
How should such cases should be properly handled, in harmony with REST and all good practices?
EDIT:
One could say that PATCH
shouldn't be used in such an example and I should use PUT
to update my User. But then what about API updates (adding a new property for example)? I would have to version my API (or version user endpoint alone); after every User change, api/v1/user
, which accepts PUT
with an old request body, api/v2/user
which accepts PUT
with a new request body, etc. I guess it's not the solution and PATCH
exists for a reason.
Source: (StackOverflow)
Sometimes, for whatever reason, I have to produce patch-files (under Linux) that are in the wrong direction. I know that I can deal with this by using the -R
switch when applying it via patch
, but it would be nice if there were a way of permanently reversing the patch-file. Is there a utility that can do this, or e.g. a regex that would be guaranteed to work?
UPDATE
Lie Ryan has suggested a neat way of doing this. However, it requires access to the original source file(s). So I suppose I should update my question to state that I'm more after a way of achieving this given only the patch-file itself.
Source: (StackOverflow)
I have a patch file (unified diff), like the output from svn diff
, git diff
, or diff -u ...
. I want to review it, but the unified diff format - especially with many files & changes - is hard on my eyes.
How can I get a nicely-formatted diff view from the patch file? I don't have the files themselves, only the diff, so I can't use all the regular diff tools.
So far my 2 best tactics are:
- Load the diff in gvim and get some syntax highlighting
- Paste the diff into a Trac wiki in a
#!diff
-formatted section and click "preview" - this creates an awesome diff view:
{{{
#!diff
<unified diff here>
}}}
Are there desktop tools that can do this? Is there a way to persuade kdiff3 / diffmerge / p4merge / etc. to visualize the patch file? Something that replicates Trac's visualization would be great as well.
EDIT: bonus points for Windows support as well, preferably with an installer or a pain-free installation.
Source: (StackOverflow)
Both git am
and git apply
can be used to apply patches. I fail to see the difference.
UPDATE
I see a difference now: git am
automatically commits whereas git apply
only touches the files but doesn't create a commit. Is that the only difference?
Source: (StackOverflow)
I have my projects in 2 repositories. One under SVN and one under Git.
Whenever I change something in SVN I want to do the same thing to the Git repository.
Say I make a change to SVN repository, producing revision 125. How would I apply these same changes to my Git repository (assuming my Git repository is up to date with revision 124).
Thank you.
Source: (StackOverflow)