<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://devlicio.us/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Christopher Bennage : windows</title><link>http://devlicio.us/blogs/christopher_bennage/archive/tags/windows/default.aspx</link><description>Tags: windows</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP1 (Build: 31106.3070)</generator><item><title>Finding Out When Something Happened in Your Git Repo</title><link>http://devlicio.us/blogs/christopher_bennage/archive/2012/02/08/finding-out-when-something-happened-in-your-git-repo.aspx</link><pubDate>Wed, 08 Feb 2012 15:14:00 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:69477</guid><dc:creator>Christopher Bennage</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;&lt;em&gt;Acknowledgment: This is meant to be the Windows equivalent of &lt;a href="http://blog.jayway.com/author/andersjanmyr"&gt;Anders Janmyr&lt;/a&gt;&amp;#8217;s &lt;a href="http://blog.jayway.com/2012/01/25/finding-with-git/"&gt;excellent post&lt;/a&gt; on the subject of finding stuff with Git. Essentially, I&amp;#8217;m translating some of Anders&amp;#8217; examples to Powershell and providing explanations for things that many Windows devs might not be familiar with.&lt;/em&gt;&lt;/p&gt;



&lt;p&gt;This is the third in a series of posts providing a set of recipes for locating sundry and diverse &lt;em&gt;thingies&lt;/em&gt; in a Git repository.&lt;/p&gt;



&lt;h2&gt;Determining when a file was added, deleted, modified, or renamed&lt;/h2&gt;



&lt;p&gt;You can include the &lt;code&gt;--diff-filter&lt;/code&gt; argument with &lt;code&gt;git log&lt;/code&gt; to find commits that include specific operations. For example:&lt;/p&gt;



&lt;pre&gt;&lt;code&gt;git log --diff-filter=D # delete

git log --diff-filter=A # add

git log --diff-filter=M # modified

git log --diff-filter=R # rename

&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;There are additional flags as well. Check the &lt;a href="http://schacon.github.com/git/git-log.html"&gt;documentation&lt;/a&gt;. By default, &lt;code&gt;git log&lt;/code&gt; just returns the commit id, author, date, and message. When using these filters I like to include &lt;code&gt;--summary&lt;/code&gt; so that the list of operations in the commit are included as well.&lt;/p&gt;



&lt;p&gt;&lt;em&gt;N.B. If you run a &lt;code&gt;git log&lt;/code&gt; command and your prompt turns into a &lt;code&gt;:&lt;/code&gt; simply press &lt;code&gt;q&lt;/code&gt; to exit.&lt;/em&gt;&lt;/p&gt;



&lt;p&gt;I don&amp;#8217;t think that you would ever want to return &lt;em&gt;all&lt;/em&gt; of the operations of a specific type in the log however. Instead, you will probably want to find out when a specific file was operated on.&lt;/p&gt;



&lt;p&gt;Let&amp;#8217;s say that something was deleted and you need to find out when and by whom. You can pass a path to &lt;code&gt;git log&lt;/code&gt;, though you&amp;#8217;ll need to preced it with &lt;code&gt;--&lt;/code&gt; and a space to disambiguate it from other arguments. Armed with this and following Ander&amp;#8217;s post you would expect to be able to do this:&lt;/p&gt;



&lt;pre&gt;&lt;code&gt;git log --diff-filter=D --summary -- /path/to/deleted/file

&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;And if you aren&amp;#8217;t using Powershell this works as expected. I tested it with Git Bash (included with msysgit) and good ol&amp;#8217; cmd as well. Both work as expected.&lt;/p&gt;



&lt;p&gt;However, when you attempt this in Powershell, git complains that the path is an &lt;em&gt;ambiguous arugment&lt;/em&gt;. I was able to, um, &amp;#8220;work around&amp;#8221; it by creating an empty placeholder file at the location.  Fortunately, &lt;a href="https://twitter.com/#!/Jittery"&gt;Jay Hill&lt;/a&gt; heard my anguish on Twitter and dug up &lt;a href="http://blogs.popart.com/2011/11/command-line-git-and-windows-gotchas/"&gt;this post&lt;/a&gt; from &lt;a href="http://blogs.popart.com/author/ethanbrown/"&gt;Ethan Brown&lt;/a&gt;. In a nutshell, Powershell strips out the &lt;code&gt;--&lt;/code&gt;. You can force it to be recognized by wrapping the argument in double qoutes:&lt;/p&gt;



&lt;pre&gt;&lt;code&gt;git log --diff-filter=D --summary &amp;quot;--&amp;quot; /path/to/deleted/file

&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;That works!&lt;/p&gt;



&lt;p&gt;I&amp;#8217;m guessing that Powershell considers &lt;code&gt;--&lt;/code&gt; to be an empty arugment and therefore something to be ignored. I also assume that when the file actually exists at the path that git is smart enough to recognize the argument as a path. (Indeed, the official documentations says that &amp;#8220;paths &lt;em&gt;may&lt;/em&gt; need to be prefixed&amp;#8221;).&lt;/p&gt;



&lt;p&gt;While we&amp;#8217;re here, I also want to point out that you can use wild cards in the path. Perhaps you don&amp;#8217;t know the exact path to the file, but you know that it was named &lt;code&gt;monkey.js&lt;/code&gt;:&lt;/p&gt;



&lt;pre&gt;&lt;code&gt;git log --diff-filter=D --summary -- **/monkey.js

&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;Happy hunting!&lt;/p&gt;


&lt;em&gt;Comment on this post at &lt;a href="http://dev.bennage.com/blog/2012/02/08/finding-stuff-in-your-git-repo-3/"&gt;dev.bennage.com&lt;/a&gt;&lt;/em&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=69477" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/christopher_bennage/archive/tags/source+control/default.aspx">source control</category><category domain="http://devlicio.us/blogs/christopher_bennage/archive/tags/PowerShell/default.aspx">PowerShell</category><category domain="http://devlicio.us/blogs/christopher_bennage/archive/tags/git/default.aspx">git</category><category domain="http://devlicio.us/blogs/christopher_bennage/archive/tags/windows/default.aspx">windows</category></item><item><title>Finding Content in Files With Git</title><link>http://devlicio.us/blogs/christopher_bennage/archive/2012/02/03/finding-content-in-files-with-git.aspx</link><pubDate>Fri, 03 Feb 2012 16:35:00 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:69445</guid><dc:creator>Christopher Bennage</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;&lt;em&gt;Acknowledgment: This is meant to be the Windows equivalent of &lt;a href="http://blog.jayway.com/author/andersjanmyr"&gt;Anders Janmyr&lt;/a&gt;&amp;#8217;s &lt;a href="http://blog.jayway.com/2012/01/25/finding-with-git/"&gt;excellent post&lt;/a&gt; on the subject of finding stuff with Git. Essentially, I&amp;#8217;m translating some of Anders&amp;#8217; examples to Powershell and providing explanations for things that many Windows devs might not be familiar with.&lt;/em&gt;&lt;/p&gt;



&lt;p&gt;This is the second in a series of posts providing a set of recipes for locating sundry and diverse &lt;em&gt;thingies&lt;/em&gt; in a Git repository.&lt;/p&gt;



&lt;h2&gt;Finding content in files&lt;/h2&gt;



&lt;p&gt;Let&amp;#8217;s say that there are hidden monkeys inside your files and you need to find. You can search &lt;em&gt;the content&lt;/em&gt; of files in a Git repositor by using &lt;code&gt;git grep&lt;/code&gt;. (For all you Windows devs, &lt;a href="http://en.wikipedia.org/wiki/Grep"&gt;&lt;code&gt;grep&lt;/code&gt;&lt;/a&gt; is a kind of magical pony from Unixland whose special talent is finding things.)&lt;/p&gt;



&lt;pre&gt;&lt;code&gt;# find all files whose content contains the string &amp;#39;monkey&amp;#39;

PS:\&amp;gt; git grep monkey

&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;There several arguments you can pass to grep to modify the behavior. These special arguments make the pony do different tricks.&lt;/p&gt;



&lt;pre&gt;&lt;code&gt;# return the line number where the match was found

PS:\&amp;gt; git grep -n monkey



# return just the file names

PS:\&amp;gt; git grep -l monkey



# count the number of matches in each file

PS:\&amp;gt; git grep -c monkey

&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;You can pass an arbitrary number of &lt;em&gt;references&lt;/em&gt; after the pattern you&amp;#8217;re trying to match. By &lt;em&gt;reference&lt;/em&gt; I mean something that&amp;#8217;s &lt;em&gt;commit-ish&lt;/em&gt;. That is, it can be the id (or SHA) of a commit, the name of a branch, a tag, or one of the special identifier like HEAD.&lt;/p&gt;



&lt;pre&gt;&lt;code&gt;# search the master branch, and two commits by id, 

# and also the commit two before the HEAD

PS:\&amp;gt; git grep monkey master d0fb0d 032086 HEAD~2

&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;The SHA is the 40-digit id of a commit. We only need enough of the SHA for Git to uniquely identify the commit. Six or eight characters is generally enough.&lt;/p&gt;



&lt;p&gt;Here&amp;#8217;s an example using the &lt;a href="https://github.com/ravendb/ravendb"&gt;RavenDB repo&lt;/a&gt;.&lt;/p&gt;



&lt;pre&gt;&lt;code&gt;PS:\&amp;gt; git grep -n monkey master f45c08bb8 HEAD~2



master:Raven.Tests/Storage/CreateIndexes.cs:83:         db.PutIndex(&amp;quot;monkey&amp;quot;, new IndexDefinition { Map = unimportantIndexMap });

master:Raven.Tests/Storage/CreateIndexes.cs:90:         Assert.Equal(&amp;quot;monkey&amp;quot;, indexNames[1]);

f45c08bb8:Raven.Tests/Storage/CreateIndexes.cs:82:          db.PutIndex(&amp;quot;monkey&amp;quot;, new IndexDefinition { Map = unimportantIndexMap });

f45c08bb8:Raven.Tests/Storage/CreateIndexes.cs:89:          Assert.Equal(&amp;quot;monkey&amp;quot;, indexNames[1]);

HEAD~2:Raven.Tests/Storage/CreateIndexes.cs:83:         db.PutIndex(&amp;quot;monkey&amp;quot;, new IndexDefinition { Map = unimportantIndexMap });

HEAD~2:Raven.Tests/Storage/CreateIndexes.cs:90:         Assert.Equal(&amp;quot;monkey&amp;quot;, indexNames[1]);

&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;Notice that each line begins with the name of the commit where the match was found. In the example above where we asked for the line numbers, the results were in the pattern:&lt;/p&gt;



&lt;pre&gt;&lt;code&gt;[commit ref]:[file path]:[line no]:[matching content]

&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;&lt;em&gt;N.B. I had one repository that did not work with &lt;code&gt;git grep&lt;/code&gt;. It was because my &amp;#8216;text&amp;#8217; files were encoded UTF-16 and git interpretted them as binary. I converted them to UTF-8 and the world became a happy place. Thanks to &lt;a href="https://twitter.com/#!/dahlbyk"&gt;Keith Dahlby&lt;/a&gt; and &lt;a href="https://twitter.com/#!/adymitruk"&gt;Adam Dymitruk&lt;/a&gt; for helping me to figure out the problem.&lt;/em&gt;&lt;/p&gt;



&lt;h2&gt;References&lt;/h2&gt;



&lt;ul&gt;

&lt;li&gt;&lt;a href="http://en.wikipedia.org/wiki/Grep"&gt;grep&lt;/a&gt;&lt;/li&gt;

&lt;li&gt;&lt;a href="http://book.git-scm.com/1_the_git_object_model.html"&gt;SHA&lt;/a&gt; (just the first paragraph)&lt;/li&gt;

&lt;li&gt;&lt;a href="http://book.git-scm.com/4_git_treeishes.html"&gt;ways of referencing commits&lt;/a&gt;&lt;/li&gt;

&lt;/ul&gt;


&lt;em&gt;Comment on this post at &lt;a href="http://dev.bennage.com/blog/2012/02/01/finding-stuff-in-your-git-repo-2/"&gt;dev.bennage.com&lt;/a&gt;&lt;/em&gt;

&lt;p&gt;&lt;a href="http://dev.bennage.com/blog/2012/02/08/finding-stuff-in-your-git-repo-3/"&gt;Next, searching for specific operations (add, delete, modify, and rename).&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=69445" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/christopher_bennage/archive/tags/source+control/default.aspx">source control</category><category domain="http://devlicio.us/blogs/christopher_bennage/archive/tags/PowerShell/default.aspx">PowerShell</category><category domain="http://devlicio.us/blogs/christopher_bennage/archive/tags/git/default.aspx">git</category><category domain="http://devlicio.us/blogs/christopher_bennage/archive/tags/windows/default.aspx">windows</category></item><item><title>Finding Files by Name With Git</title><link>http://devlicio.us/blogs/christopher_bennage/archive/2012/01/30/finding-files-by-name-with-git.aspx</link><pubDate>Mon, 30 Jan 2012 17:01:00 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:69420</guid><dc:creator>Christopher Bennage</dc:creator><slash:comments>0</slash:comments><description>&lt;div class="entry-content"&gt;&lt;p&gt;&lt;em&gt;Acknowledgment: This is meant to be the Windows equivalent of &lt;a href="http://blog.jayway.com/author/andersjanmyr"&gt;Anders Janmyr&lt;/a&gt;&amp;#8217;s &lt;a href="http://blog.jayway.com/2012/01/25/finding-with-git/"&gt;excellent post&lt;/a&gt; on the subject of finding stuff with Git. Essentially, I&amp;#8217;m translating some of Anders&amp;#8217; examples to Powershell and providing explanations for things that many Windows devs might not be familiar with.&lt;/em&gt;&lt;/p&gt;



&lt;p&gt;This is the first in a series of posts providing a set of recipes for locating sundry and diverse &lt;em&gt;thingies&lt;/em&gt; in a Git repository.&lt;/p&gt;



&lt;h2&gt;Finding files by name&lt;/h2&gt;



&lt;p&gt;Let&amp;#8217;s say that you want locate all the files in a git repository that contain &amp;#8216;monkey&amp;#8217; in the file name. (Finding monkeys is a very common task.)&lt;/p&gt;



&lt;pre&gt;&lt;code&gt;# find all files whose name matches &amp;#39;monkey&amp;#39;

PS:\&amp;gt; git ls-files | Select-String monkey

&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;This &lt;em&gt;pipes&lt;/em&gt; the output of &lt;code&gt;git ls-files&lt;/code&gt; into the Powershell cmdlet &lt;code&gt;Select-String&lt;/code&gt; which filters the output line-by-line. To better understand what this means, run just &lt;code&gt;git ls-files&lt;/code&gt;.&lt;/p&gt;



&lt;p&gt;Of course, you can also pass a regular expression to&lt;code&gt;Select-String&lt;/code&gt; (that is, if you hate yourself.)&lt;/p&gt;



&lt;h2&gt;References&lt;/h2&gt;



&lt;ul&gt;

&lt;li&gt;&lt;a href="http://schacon.github.com/git/git-ls-files.html"&gt;git ls-files&lt;/a&gt;&lt;/li&gt;

&lt;li&gt;&lt;a href="http://technet.microsoft.com/en-us/library/dd315403.aspx"&gt;Select-String&lt;/a&gt;&lt;/li&gt;

&lt;li&gt;&lt;a href="http://technet.microsoft.com/en-us/library/ee176927.aspx"&gt;the pipe operator &lt;code&gt;|&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;

&lt;li&gt;&lt;a href="http://haacked.com/archive/2011/12/13/better-git-with-powershell.aspx"&gt;Better Git with Powershell by Phil Haack&lt;/a&gt;&lt;/li&gt;

&lt;/ul&gt;

&lt;em&gt;Comment on this post at &lt;a href="http://dev.bennage.com/blog/2012/01/30/finding-stuff-in-your-git-repo-1/"&gt;dev.bennage.com&lt;/a&gt;&lt;/em&gt;

&lt;p&gt;&lt;a href="http://dev.bennage.com/blog/2012/02/01/finding-stuff-in-your-git-repo-2/"&gt;Next, searching for files with specific content.&lt;/a&gt;&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=69420" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/christopher_bennage/archive/tags/source+control/default.aspx">source control</category><category domain="http://devlicio.us/blogs/christopher_bennage/archive/tags/PowerShell/default.aspx">PowerShell</category><category domain="http://devlicio.us/blogs/christopher_bennage/archive/tags/git/default.aspx">git</category><category domain="http://devlicio.us/blogs/christopher_bennage/archive/tags/windows/default.aspx">windows</category></item></channel></rss>