Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git checkout -p HEAD regex search

Tags:

git

regex

OK... doing a git checkout -p HEAD beings up the prompt ...

Discard this hunk from index and worktree [y,n,q,a,d,/,j,J,g,e,?]?

Where ? shows a help and / is...

/ - search for a hunk matching the given regex

So, I wanted to try it. I added a line in a file like let mySillyString = "Hello, world".

If I do a git diff I can see several files and hunks that are in the worktree. (Including the sillyString one).

How would I use the / option to find my sillyString hunk.

When I enter / it gives the prompt

search for regex?

I have tried several options but it still just offers up the hunk it is currently on.

What should I type in as my regex string to search for sillyString in a hunk?

The git diff that I currently have

I have anonymised it to remove stuff but it shows the important bits I want to try to achieve.

diff --git a/MyProject/AViewController.swift b/MyProject/AViewController.swift index 1b14a4522..3341eb355 100644
--- a/MyProject/AViewController.swift
+++ b/MyProject/AViewController.swift 

@@ -99,12 +99,6 @@ class AViewController: UIViewController, Str
         print("doing something here")
     }

-    func getImageURL() -> URL? {
-        print("doing something here")
-        print("doing something here")
-        return theImageURL
-    }
-
     func addPopGesture() {
         print("doing something here")

@@ -174,6 +168,12 @@ extension AViewController {

         stackView.addArrangedSubviews(views: someViews)
     }
+
+    private func getImageURL() -> URL? {
+        print("doing something here")
+        print("doing something here")
+        return theImageURL
+    }
 }

 extension AViewController { 

diff --git a/MyProject/AView.swift b/MyProject/AView.swift index b9e3349aa..962e255e2 100644
--- a/MyProject/AView.swift
+++ b/MyProject/AView.swift 

@@ -300,5 +300,7 @@ fileprivate extension UIImage {

         return someData
     }
+
+    let mySillyString = "Hello, world!"  
+
 }

From the diff you can see that I have moved some code in one file (there are additions and subtractions of the same code in that file). And then I have added my example hunk that I want to search for.

like image 230
Fogmeister Avatar asked Mar 21 '18 14:03

Fogmeister


1 Answers

Short version:

/silly should take you right to the hunk you want to add. It only searches for hunks inside the same file as the hunk you are currently located at.

Long version:

I don't have your file, so I used one of my own as an example. I added 3 comments so the diff looks like this:

diff --git a/Gemfile b/Gemfile
index 751838d..626c553 100644
--- a/Gemfile
+++ b/Gemfile
@@ -5,6 +5,8 @@ git_source(:github) do |repo_name|
   "https://github.com/#{repo_name}.git"
 end

+# This is change 1
+
 # Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
 gem 'rails', '~> 5.1.5'
 # Use sqlite3 as the database for Active Record
@@ -18,6 +20,8 @@ gem 'uglifier', '>= 1.3.0'
 # See https://github.com/rails/execjs#readme for more supported runtimes
 # gem 'therubyracer', platforms: :ruby

+# This is change 2
+
 gem 'coffee-rails', '~> 4.2'
 # Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
 gem 'turbolinks', '~> 5'
@@ -39,6 +43,8 @@ group :development, :test do
   gem 'selenium-webdriver'
 end

+# This is a silly change
+
 group :development do
   # Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
   gem 'web-console', '>= 3.3.0'

So now I checkout. It shows the first hunk, I search for "silly", it shows the 3rd hunk. I still have to answer if I want to add the hunk or not, this just makes it faster to move around a file with a lot of hunks.

$ git checkout -p HEAD
diff --git a/Gemfile b/Gemfile
index 751838d..626c553 100644
--- a/Gemfile
+++ b/Gemfile
@@ -5,6 +5,8 @@ git_source(:github) do |repo_name|
   "https://github.com/#{repo_name}.git"
 end

+# This is change 1
+
 # Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
 gem 'rails', '~> 5.1.5'
 # Use sqlite3 as the database for Active Record
Discard this hunk from index and worktree [y,n,q,a,d,/,j,J,g,e,?]? /silly
@@ -39,6 +43,8 @@ group :development, :test do
   gem 'selenium-webdriver'
 end

+# This is a silly change
+
 group :development do
   # Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
   gem 'web-console', '>= 3.3.0'
Discard this hunk from index and worktree [y,n,q,a,d,/,k,K,g,e,?]?

If you're getting the same hunk over and over again, it probably means your regex matches that hunk as well.

like image 51
Jacob Vanus Avatar answered Nov 20 '22 00:11

Jacob Vanus