[PATCH v2] nmbug: Add 'clone' and replace FETCH_HEAD with @{upstream}
authorW. Trevor King <wking@tremily.us>
Mon, 10 Mar 2014 00:28:49 +0000 (17:28 +1700)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 18:00:33 +0000 (10:00 -0800)
91/2137eb85ad7f2d36ade42cc051fe47317bc757 [new file with mode: 0644]

diff --git a/91/2137eb85ad7f2d36ade42cc051fe47317bc757 b/91/2137eb85ad7f2d36ade42cc051fe47317bc757
new file mode 100644 (file)
index 0000000..1414311
--- /dev/null
@@ -0,0 +1,297 @@
+Return-Path: <wking@tremily.us>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+       by olra.theworths.org (Postfix) with ESMTP id 27D1F431FBD\r
+       for <notmuch@notmuchmail.org>; Sun,  9 Mar 2014 17:31:06 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: 0\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=0 tagged_above=-999 required=5\r
+       tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001]\r
+       autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+       by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+       with ESMTP id lpwj+ECND6K5 for <notmuch@notmuchmail.org>;\r
+       Sun,  9 Mar 2014 17:30:58 -0700 (PDT)\r
+Received: from qmta01.westchester.pa.mail.comcast.net\r
+       (qmta01.westchester.pa.mail.comcast.net [76.96.62.16])\r
+       by olra.theworths.org (Postfix) with ESMTP id 6EEB3431FBC\r
+       for <notmuch@notmuchmail.org>; Sun,  9 Mar 2014 17:30:58 -0700 (PDT)\r
+Received: from omta06.westchester.pa.mail.comcast.net ([76.96.62.51])\r
+       by qmta01.westchester.pa.mail.comcast.net with comcast\r
+       id bc6b1n00616LCl051cWvBd; Mon, 10 Mar 2014 00:30:55 +0000\r
+Received: from odin.tremily.us ([24.18.63.50])\r
+       by omta06.westchester.pa.mail.comcast.net with comcast\r
+       id bcWt1n00R152l3L3ScWuha; Mon, 10 Mar 2014 00:30:55 +0000\r
+Received: from mjolnir.tremily.us (unknown [192.168.0.140])\r
+       by odin.tremily.us (Postfix) with ESMTPS id 4C0A8106CDE1;\r
+       Sun,  9 Mar 2014 17:30:52 -0700 (PDT)\r
+Received: (nullmailer pid 30098 invoked by uid 1000);\r
+       Mon, 10 Mar 2014 00:28:53 -0000\r
+From: "W. Trevor King" <wking@tremily.us>\r
+To: notmuch <notmuch@notmuchmail.org>\r
+Subject: [PATCH v2] nmbug: Add 'clone' and replace FETCH_HEAD with @{upstream}\r
+Date: Sun,  9 Mar 2014 17:28:49 -0700\r
+Message-Id:\r
+ <de7dd9d4021cd17350df4423bc13f3da35f2b324.1394410765.git.wking@tremily.us>\r
+X-Mailer: git-send-email 1.8.5.2.8.g0f6c0d1\r
+MIME-Version: 1.0\r
+Content-Type: text/plain; charset=UTF-8\r
+Content-Transfer-Encoding: 8bit\r
+DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=comcast.net;\r
+       s=q20121106; t=1394411455;\r
+       bh=6N4BgLALsLWrkOxDEQpUbmEr/YbO/Csw8jtiXJ2YuM8=;\r
+       h=Received:Received:Received:Received:From:To:Subject:Date:\r
+       Message-Id:MIME-Version:Content-Type;\r
+       b=dCfnl14qjuwtUC8AWw/LEFEr9sZV1SHJ1O6Pt0LZTw42yU74cMfnoikeXwDwxwNxe\r
+       26pWR2Rfsjge8Fo8zhGH7yRqHj/Z9gqKMl3eBGc8weTZsk/FJevwXj7TOUgErYUHEV\r
+       MCyxPlvWSyjrGTBkfHNI8cRpZKA9n7yXxwuEWh3CQdCt80tZqJAyGIhUcqOWcrzvz1\r
+       QGDfN87e13685PFb43RYWwdNrwDY7X5X6mX2n+Ij69QKvC+bwdJte5AmfvwGS9zC2l\r
+       w+sYECARuwfwIAGZ7AL5ueDZ+DC/HnrTAcEt/uSaVRHe0SesiFdnyhq0+HZ8djgOPk\r
+       Z2Gl5DvUyPG4w==\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+       <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Mon, 10 Mar 2014 00:31:06 -0000\r
+\r
+With two branches getting fetched (master and config), the branch\r
+referenced by FETCH_HEAD is ambiguous.  For example, I have:\r
+\r
+  $ cat FETCH_HEAD\r
+  41d7bfa7184cc93c9dac139d1674e9530799e3b0 \\r
+    not-for-merge   branch 'config' of http://nmbug.tethera.net/git/nmbug-tags\r
+  acd379ccb973c45713eee9db177efc530f921954 \\r
+    not-for-merge   branch 'master' of http://nmbug.tethera.net/git/nmbug-tags\r
+\r
+(where I wrapped the line by hand).  This means that FETCH_HEAD\r
+references the config branch:\r
+\r
+  $ git rev-parse FETCH_HEAD\r
+  41d7bfa7184cc93c9dac139d1674e9530799e3b0\r
+\r
+which breaks all of the FETCH_HEAD logic in nmbug (where FETCH_HEAD is\r
+assumed to point to the master branch).\r
+\r
+Instead of relying on FETCH_HEAD, use @{upstream} as the\r
+remote-tracking branch that should be merged/diffed/integrated into\r
+HEAD.  @{upstream} was added in Git v1.7.0 (2010-02-12) [1], so\r
+relying on it should be fairly safe.  One tricky bit is that bare\r
+repositories don't set upstream tracking branches by default:\r
+\r
+  $ git clone --bare http://nmbug.tethera.net/git/nmbug-tags.git nmbug-bare\r
+  $ cd nmbug-bare\r
+  $ git remote show origin\r
+  * remote origin\r
+    Fetch URL: http://nmbug.tethera.net/git/nmbug-tags.git\r
+    Push  URL: http://nmbug.tethera.net/git/nmbug-tags.git\r
+    HEAD branch: master\r
+    Local refs configured for 'git push':\r
+      config pushes to config (up to date)\r
+      master pushes to master (up to date)\r
+\r
+While in a non-bare clone:\r
+\r
+  $ git clone http://nmbug.tethera.net/git/nmbug-tags.git\r
+  $ cd nmbug-tags\r
+  $ git remote show origin\r
+  * remote origin\r
+    Fetch URL: http://nmbug.tethera.net/git/nmbug-tags.git\r
+    Push  URL: http://nmbug.tethera.net/git/nmbug-tags.git\r
+    HEAD branch: master\r
+    Remote branches:\r
+      config tracked\r
+      master tracked\r
+    Local branch configured for 'git pull':\r
+      master merges with remote master\r
+    Local ref configured for 'git push':\r
+      master pushes to master (up to date)\r
+\r
+>From the clone docs [2]:\r
+\r
+  --bare::\r
+        Make a 'bare' Git repository…\r
+        Also the branch heads at the remote are copied directly\r
+        to corresponding local branch heads, without mapping\r
+        them to `refs/remotes/origin/`.  When this option is\r
+        used, neither remote-tracking branches nor the related\r
+        configuration variables are created.\r
+\r
+To use @{upstream}, we need to the local vs. remote-tracking\r
+distinction, so this commit adds 'nmbug clone', replacing the\r
+previously suggested --bare clone with a non-bare --no-checkout\r
+--separate-git-dir clone into a temporary work directory.  After\r
+which:\r
+\r
+  $ git rev-parse @{upstream}\r
+  acd379ccb973c45713eee9db177efc530f921954\r
+\r
+gives us the master-branch commit.  Existing nmbug users will have to\r
+run the configuration tweaks and re-fetch by hand.  If you don't have\r
+any local commits, you could also blow away your NMBGIT repository and\r
+re-clone from scratch:\r
+\r
+  $ nmbug clone http://nmbug.tethera.net/git/nmbug-tags.git\r
+\r
+Besides removing the ambiguity of FETCH_HEAD, this commit allows users\r
+to configure which upstream branch they want nmbug to track via 'git\r
+config', in case they want to change their upstream repository.\r
+\r
+[1]: http://git.kernel.org/cgit/git/git.git/tree/Documentation/RelNotes/1.7.0.txt\r
+[2]: http://git.kernel.org/cgit/git/git.git/tree/Documentation/git-clone.txt\r
+---\r
+\r
+Changes since v1 [1]:\r
+\r
+* Replaced confusing post-clone fetch with a single --no-checkout\r
+  --separate-git-dir clone [2].  This avoids a bare repository's lack of\r
+  remote-tracking branches by not creating a bare repository in the\r
+  first place, while v1 tried to “fix” the bare repo.\r
+\r
+Cheers,\r
+Trevor\r
+\r
+[1]: id:80db13662a0313711ca3a91e338a63d35e64a83c.1391898002.git.wking@tremily.us\r
+     http://article.gmane.org/gmane.mail.notmuch.general/17154\r
+[2]: id:20140308162633.GM16433@odin.tremily.us\r
+     http://article.gmane.org/gmane.mail.notmuch.general/17409\r
+\r
+ devel/nmbug/nmbug | 45 ++++++++++++++++++++++++++++++++-------------\r
+ 1 file changed, 32 insertions(+), 13 deletions(-)\r
+\r
+diff --git a/devel/nmbug/nmbug b/devel/nmbug/nmbug\r
+index 90d98b6..d6f5213 100755\r
+--- a/devel/nmbug/nmbug\r
++++ b/devel/nmbug/nmbug\r
+@@ -26,6 +26,7 @@ my $ESCAPED_RX =     qr{$ESCAPE_CHAR([A-Fa-f0-9]{2})};\r
+ my %command = (\r
+            archive    => \&do_archive,\r
+            checkout   => \&do_checkout,\r
++           clone      => \&do_clone,\r
+            commit     => \&do_commit,\r
+            fetch      => \&do_fetch,\r
+            help       => \&do_help,\r
+@@ -125,6 +126,15 @@ sub do_archive {\r
+   system ('git', "--git-dir=$NMBGIT", 'archive', 'HEAD');\r
+ }\r
\r
++sub do_clone {\r
++  my $repository = shift;\r
++\r
++  my $tempwork = tempdir ('/tmp/nmbug-clone.XXXXXX', CLEANUP => 1);\r
++  system ('git', 'clone', '--no-checkout', '--separate-git-dir', $NMBGIT,\r
++          $repository, $tempwork) == 0\r
++    or die "'git clone' exited with nonzero value\n";\r
++  git ('config', '--unset', 'core.worktree');\r
++}\r
\r
+ sub is_committed {\r
+   my $status = shift;\r
+@@ -332,21 +342,24 @@ To discard your changes,  run 'nmbug checkout'\r
\r
+ sub do_pull {\r
+   my $remote = shift || 'origin';\r
++  my $branch = shift || 'master';\r
\r
+   git ( 'fetch', $remote);\r
\r
+-  do_merge ();\r
++  do_merge ("$remote/$branch");\r
+ }\r
\r
\r
+ sub do_merge {\r
++  my $commit = shift || '@{upstream}';\r
++\r
+   insist_committed ();\r
\r
+   my $tempwork = tempdir ('/tmp/nmbug-merge.XXXXXX', CLEANUP => 1);\r
\r
+   git ( { GIT_WORK_TREE => $tempwork }, 'checkout', '-f', 'HEAD');\r
\r
+-  git ( { GIT_WORK_TREE => $tempwork }, 'merge', 'FETCH_HEAD');\r
++  git ( { GIT_WORK_TREE => $tempwork }, 'merge', $commit);\r
\r
+   do_checkout ();\r
+ }\r
+@@ -407,11 +420,10 @@ sub do_status {\r
\r
\r
+ sub is_unmerged {\r
++  my $commit = shift || '@{upstream}';\r
\r
+-  return 0 if (! -f $NMBGIT.'/FETCH_HEAD');\r
+-\r
+-  my $fetch_head = git ('rev-parse', 'FETCH_HEAD');\r
+-  my $base = git ( 'merge-base', 'HEAD', 'FETCH_HEAD');\r
++  my $fetch_head = git ('rev-parse', $commit);\r
++  my $base = git ( 'merge-base', 'HEAD', $commit);\r
\r
+   return ($base ne $fetch_head);\r
\r
+@@ -473,7 +485,7 @@ sub diff_index {\r
+ sub diff_refs {\r
+   my $filter = shift;\r
+   my $ref1 = shift || 'HEAD';\r
+-  my $ref2 = shift || 'FETCH_HEAD';\r
++  my $ref2 = shift || '@{upstream}';\r
\r
+   my $fh= git_pipe ( 'diff', "--diff-filter=$filter", '--name-only',\r
+                $ref1, $ref2);\r
+@@ -561,10 +573,11 @@ git. Any extra arguments are used (one per line) as a commit message.\r
\r
+ push local nmbug git state to remote repo\r
\r
+-=item  B<pull> [remote]\r
++=item  B<pull> [remote] [branch]\r
\r
+ pull (merge) remote repo changes to notmuch. B<pull> is equivalent to\r
+-B<fetch> followed by B<merge>.\r
++B<fetch> followed by B<merge>.  The default remote is C<origin>, and\r
++the default branch is C<master>.\r
\r
+ =back\r
\r
+@@ -572,6 +585,12 @@ B<fetch> followed by B<merge>.\r
\r
+ =over 8\r
\r
++=item B<clone> repository\r
++\r
++Create a local nmbug repository from a remote source.  This wraps\r
++C<git clone>, adding some options to avoid creating a working tree\r
++while preserving remote-tracking branches and upstreams.\r
++\r
+ =item B<checkout>\r
\r
+ Update the notmuch database from git. This is mainly useful to discard\r
+@@ -589,12 +608,12 @@ print help [for subcommand]\r
+ =item B<log> [parameters]\r
\r
+ A simple wrapper for git log. After running C<nmbug fetch>, you can\r
+-inspect the changes with C<nmbug log HEAD..FETCH_HEAD>\r
++inspect the changes with C<nmbug log HEAD..@{upstream}>\r
\r
+-=item B<merge>\r
++=item B<merge> [commit]\r
\r
+-Merge changes from FETCH_HEAD into HEAD, and load the result into\r
+-notmuch.\r
++Merge changes from C<commit> into HEAD, and load the result into\r
++notmuch.  The default commit is C<@{upstream}>.\r
\r
+ =item  B<status>\r
\r
+-- \r
+1.8.5.2.8.g0f6c0d1\r
+\r