Skip to content

Instantly share code, notes, and snippets.

@mathieucarbou
Last active November 1, 2018 12:22
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mathieucarbou/fd5c619816b5efc0d2a8 to your computer and use it in GitHub Desktop.
Save mathieucarbou/fd5c619816b5efc0d2a8 to your computer and use it in GitHub Desktop.
--- git.js 2015-05-11 16:19:29.114852900 -0400
+++ git-patched.js 2015-05-11 16:19:35.788234600 -0400
@@ -12,14 +12,23 @@
, git = npm.config.get("git")
, assert = require("assert")
, log = require("npmlog")
+ , win32 = process.platform === "win32"
+ , cygwin = win32 && (process.env.ORIGINAL_PATH || '').indexOf('/cygdrive/') != -1
function prefixGitArgs () {
- return process.platform === "win32" ? ["-c", "core.longpaths=true"] : []
+ return win32 ? ["-c", "core.longpaths=true"] : []
}
function execGit (args, options, cb) {
- log.info('git', args)
+ if(cygwin && args) {
+ for(var i=0; i<args.length; i++) {
+ if(':\\'.indexOf(args[i]) != 1) {
+ args[i] = args[i].replace(/\\/g, '/').replace(/^([A-Za-z])\:\//, '/cygdrive/$1/');
+ }
+ }
+ }
var fullArgs = prefixGitArgs().concat(args || [])
+ log.info('git', fullArgs)
return exec(git, fullArgs, options, cb)
}
@gene-pavlovsky
Copy link

Didn't work for me for two reasons.
ORIGINAL_PATH is not set (by /etc/profile) if a shell is a non-login shell (I'm running my commands via bash from a file manager called Far).
There's function spawnGit in git.js which also needs the same paths treatment (not sure when npm uses which function).

Here's my version of the patch that works on my system. I'm also checking if a system has both Cygwin's git and GfW git installed, which one is earlier in the PATH. Another note: I've replaced /cygdrive/ to /proc/cygdrive/ since on my system I've changed the default prefix to /mnt (by adding a line to /etc/fstab). Using Cygwin:

To simplify scripting, Cygwin also provides a /proc/cygdrive symlink, which allows to use a fixed path in scripts, even if the actual cygdrive prefix has been changed, or is different between different users. So, in scripts, conveniently use the /proc/cygdrive symlink to successfully access files independently from the current cygdrive prefix.

--- git.js.orig	2017-07-07 09:34:12.398470700 +0200
+++ git.js	2017-07-07 12:49:34.880033400 +0200
@@ -12,20 +12,38 @@
 var assert = require('assert')
 var log = require('npmlog')
 var noProgressTillDone = require('./no-progress-while-running.js').tillDone
+var win32 = process.platform === 'win32'
+var cygwin = false
+if(win32) {
+  var gfwGitIndex = process.env.PATH.indexOf('\\Git\\bin')
+  var cygGitIndex = process.env.PATH.indexOf('\\cygwin64\\bin')
+  if (cygGitIndex == -1)
+    cygGitIndex = process.env.PATH.indexOf('\\cygwin\\bin')
+  cygwin = (cygGitIndex != -1) && ((gfwGitIndex == -1) || (cygGitIndex < gfwGitIndex))
+}
 
 function prefixGitArgs () {
-  return process.platform === 'win32' ? ['-c', 'core.longpaths=true'] : []
+  return win32 ? ['-c', 'core.longpaths=true'] : []
+}
+
+function fixCygwinArgs (args) {
+  for(var i=0; i<args.length; i++) {
+    if(':\\'.indexOf(args[i]) != 1) {
+      args[i] = args[i].replace(/\\/g, '/').replace(/^([A-Za-z])\:\//, '/proc/cygdrive/$1/');
+    }
+  }
+  return args;
 }
 
 function execGit (args, options, cb) {
-  log.info('git', args)
-  var fullArgs = prefixGitArgs().concat(args || [])
+  var fullArgs = prefixGitArgs().concat(((cygwin && args) ? fixCygwinArgs(args) : args) || [])
+  log.info('git', fullArgs)
   return exec(git, fullArgs, options, noProgressTillDone(cb))
 }
 
 function spawnGit (args, options) {
   log.info('git', args)
-  return spawn(git, prefixGitArgs().concat(args || []), options)
+  return spawn(git, prefixGitArgs().concat(((cygwin && args) ? fixCygwinArgs(args) : args) || []), options)
 }
 
 function chainableExec () {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment