"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/git-utils/lib/git.js" (7 Feb 2017, 11222 Bytes) of archive /windows/misc/atom-windows.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Javascript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 (function() {
    2   var Repository, deletedStatusFlags, fs, indexStatusFlags, isRootPath, modifiedStatusFlags, newStatusFlags, openRepository, openSubmodules, path, realpath, statusIgnored, statusIndexDeleted, statusIndexModified, statusIndexNew, statusIndexRenamed, statusIndexTypeChange, statusWorkingDirDelete, statusWorkingDirModified, statusWorkingDirNew, statusWorkingDirTypeChange;
    3 
    4   path = require('path');
    5 
    6   fs = require('fs-plus');
    7 
    8   Repository = require('../build/Release/git.node').Repository;
    9 
   10   statusIndexNew = 1 << 0;
   11 
   12   statusIndexModified = 1 << 1;
   13 
   14   statusIndexDeleted = 1 << 2;
   15 
   16   statusIndexRenamed = 1 << 3;
   17 
   18   statusIndexTypeChange = 1 << 4;
   19 
   20   statusWorkingDirNew = 1 << 7;
   21 
   22   statusWorkingDirModified = 1 << 8;
   23 
   24   statusWorkingDirDelete = 1 << 9;
   25 
   26   statusWorkingDirTypeChange = 1 << 10;
   27 
   28   statusIgnored = 1 << 14;
   29 
   30   modifiedStatusFlags = statusWorkingDirModified | statusIndexModified | statusWorkingDirDelete | statusIndexDeleted | statusWorkingDirTypeChange | statusIndexTypeChange;
   31 
   32   newStatusFlags = statusWorkingDirNew | statusIndexNew;
   33 
   34   deletedStatusFlags = statusWorkingDirDelete | statusIndexDeleted;
   35 
   36   indexStatusFlags = statusIndexNew | statusIndexModified | statusIndexDeleted | statusIndexRenamed | statusIndexTypeChange;
   37 
   38   Repository.prototype.release = function() {
   39     var submodulePath, submoduleRepo, _ref;
   40     _ref = this.submodules;
   41     for (submodulePath in _ref) {
   42       submoduleRepo = _ref[submodulePath];
   43       if (submoduleRepo != null) {
   44         submoduleRepo.release();
   45       }
   46     }
   47     return this._release();
   48   };
   49 
   50   Repository.prototype.getWorkingDirectory = function() {
   51     var _ref;
   52     return this.workingDirectory != null ? this.workingDirectory : this.workingDirectory = (_ref = this._getWorkingDirectory()) != null ? _ref.replace(/\/$/, '') : void 0;
   53   };
   54 
   55   Repository.prototype.getShortHead = function() {
   56     var head;
   57     head = this.getHead();
   58     if (head == null) {
   59       return head;
   60     }
   61     if (head.indexOf('refs/heads/') === 0) {
   62       return head.substring(11);
   63     }
   64     if (head.indexOf('refs/tags/') === 0) {
   65       return head.substring(10);
   66     }
   67     if (head.indexOf('refs/remotes/') === 0) {
   68       return head.substring(13);
   69     }
   70     if (head.match(/[a-fA-F0-9]{40}/)) {
   71       return head.substring(0, 7);
   72     }
   73     return head;
   74   };
   75 
   76   Repository.prototype.isStatusModified = function(status) {
   77     if (status == null) {
   78       status = 0;
   79     }
   80     return (status & modifiedStatusFlags) > 0;
   81   };
   82 
   83   Repository.prototype.isPathModified = function(path) {
   84     return this.isStatusModified(this.getStatus(path));
   85   };
   86 
   87   Repository.prototype.isStatusNew = function(status) {
   88     if (status == null) {
   89       status = 0;
   90     }
   91     return (status & newStatusFlags) > 0;
   92   };
   93 
   94   Repository.prototype.isPathNew = function(path) {
   95     return this.isStatusNew(this.getStatus(path));
   96   };
   97 
   98   Repository.prototype.isStatusDeleted = function(status) {
   99     if (status == null) {
  100       status = 0;
  101     }
  102     return (status & deletedStatusFlags) > 0;
  103   };
  104 
  105   Repository.prototype.isPathDeleted = function(path) {
  106     return this.isStatusDeleted(this.getStatus(path));
  107   };
  108 
  109   Repository.prototype.isPathStaged = function(path) {
  110     return this.isStatusStaged(this.getStatus(path));
  111   };
  112 
  113   Repository.prototype.isStatusIgnored = function(status) {
  114     if (status == null) {
  115       status = 0;
  116     }
  117     return (status & statusIgnored) > 0;
  118   };
  119 
  120   Repository.prototype.isStatusStaged = function(status) {
  121     if (status == null) {
  122       status = 0;
  123     }
  124     return (status & indexStatusFlags) > 0;
  125   };
  126 
  127   Repository.prototype.getUpstreamBranch = function(branch) {
  128     var branchMerge, branchRemote, shortBranch;
  129     if (branch == null) {
  130       branch = this.getHead();
  131     }
  132     if (!((branch != null ? branch.length : void 0) > 11)) {
  133       return null;
  134     }
  135     if (branch.indexOf('refs/heads/') !== 0) {
  136       return null;
  137     }
  138     shortBranch = branch.substring(11);
  139     branchMerge = this.getConfigValue("branch." + shortBranch + ".merge");
  140     if (!((branchMerge != null ? branchMerge.length : void 0) > 11)) {
  141       return null;
  142     }
  143     if (branchMerge.indexOf('refs/heads/') !== 0) {
  144       return null;
  145     }
  146     branchRemote = this.getConfigValue("branch." + shortBranch + ".remote");
  147     if (!((branchRemote != null ? branchRemote.length : void 0) > 0)) {
  148       return null;
  149     }
  150     return "refs/remotes/" + branchRemote + "/" + (branchMerge.substring(11));
  151   };
  152 
  153   Repository.prototype.getAheadBehindCount = function(branch) {
  154     var counts, headCommit, mergeBase, upstream, upstreamCommit;
  155     if (branch == null) {
  156       branch = 'HEAD';
  157     }
  158     if (branch !== 'HEAD' && branch.indexOf('refs/heads/') !== 0) {
  159       branch = "refs/heads/" + branch;
  160     }
  161     counts = {
  162       ahead: 0,
  163       behind: 0
  164     };
  165     headCommit = this.getReferenceTarget(branch);
  166     if (!((headCommit != null ? headCommit.length : void 0) > 0)) {
  167       return counts;
  168     }
  169     upstream = this.getUpstreamBranch();
  170     if (!((upstream != null ? upstream.length : void 0) > 0)) {
  171       return counts;
  172     }
  173     upstreamCommit = this.getReferenceTarget(upstream);
  174     if (!((upstreamCommit != null ? upstreamCommit.length : void 0) > 0)) {
  175       return counts;
  176     }
  177     mergeBase = this.getMergeBase(headCommit, upstreamCommit);
  178     if (!((mergeBase != null ? mergeBase.length : void 0) > 0)) {
  179       return counts;
  180     }
  181     counts.ahead = this.getCommitCount(headCommit, mergeBase);
  182     counts.behind = this.getCommitCount(upstreamCommit, mergeBase);
  183     return counts;
  184   };
  185 
  186   Repository.prototype.checkoutReference = function(branch, create) {
  187     if (branch.indexOf('refs/heads/') !== 0) {
  188       branch = "refs/heads/" + branch;
  189     }
  190     return this.checkoutRef(branch, create);
  191   };
  192 
  193   Repository.prototype.relativize = function(path) {
  194     var lowerCasePath, workingDirectory;
  195     if (!path) {
  196       return path;
  197     }
  198     if (process.platform === 'win32') {
  199       path = path.replace(/\\/g, '/');
  200     } else {
  201       if (path[0] !== '/') {
  202         return path;
  203       }
  204     }
  205     if (this.caseInsensitiveFs) {
  206       lowerCasePath = path.toLowerCase();
  207       workingDirectory = this.getWorkingDirectory();
  208       if (workingDirectory) {
  209         workingDirectory = workingDirectory.toLowerCase();
  210         if (lowerCasePath.indexOf("" + workingDirectory + "/") === 0) {
  211           return path.substring(workingDirectory.length + 1);
  212         } else if (lowerCasePath === workingDirectory) {
  213           return '';
  214         }
  215       }
  216       if (this.openedWorkingDirectory) {
  217         workingDirectory = this.openedWorkingDirectory.toLowerCase();
  218         if (lowerCasePath.indexOf("" + workingDirectory + "/") === 0) {
  219           return path.substring(workingDirectory.length + 1);
  220         } else if (lowerCasePath === workingDirectory) {
  221           return '';
  222         }
  223       }
  224     } else {
  225       workingDirectory = this.getWorkingDirectory();
  226       if (workingDirectory) {
  227         if (path.indexOf("" + workingDirectory + "/") === 0) {
  228           return path.substring(workingDirectory.length + 1);
  229         } else if (path === workingDirectory) {
  230           return '';
  231         }
  232       }
  233       if (this.openedWorkingDirectory) {
  234         if (path.indexOf("" + this.openedWorkingDirectory + "/") === 0) {
  235           return path.substring(this.openedWorkingDirectory.length + 1);
  236         } else if (path === this.openedWorkingDirectory) {
  237           return '';
  238         }
  239       }
  240     }
  241     return path;
  242   };
  243 
  244   Repository.prototype.submoduleForPath = function(path) {
  245     var submodulePath, submoduleRepo, _ref, _ref1;
  246     path = this.relativize(path);
  247     if (!path) {
  248       return null;
  249     }
  250     _ref = this.submodules;
  251     for (submodulePath in _ref) {
  252       submoduleRepo = _ref[submodulePath];
  253       if (path === submodulePath) {
  254         return submoduleRepo;
  255       } else if (path.indexOf("" + submodulePath + "/") === 0) {
  256         path = path.substring(submodulePath.length + 1);
  257         return (_ref1 = submoduleRepo.submoduleForPath(path)) != null ? _ref1 : submoduleRepo;
  258       }
  259     }
  260     return null;
  261   };
  262 
  263   Repository.prototype.isWorkingDirectory = function(path) {
  264     var lowerCasePath, _ref, _ref1;
  265     if (!path) {
  266       return false;
  267     }
  268     if (process.platform === 'win32') {
  269       path = path.replace(/\\/g, '/');
  270     } else {
  271       if (path[0] !== '/') {
  272         return false;
  273       }
  274     }
  275     if (this.caseInsensitiveFs) {
  276       lowerCasePath = path.toLowerCase();
  277       if (lowerCasePath === ((_ref = this.getWorkingDirectory()) != null ? _ref.toLowerCase() : void 0)) {
  278         return true;
  279       }
  280       if (lowerCasePath === ((_ref1 = this.openedWorkingDirectory) != null ? _ref1.toLowerCase() : void 0)) {
  281         return true;
  282       }
  283     } else {
  284       if (path === this.getWorkingDirectory()) {
  285         return true;
  286       }
  287       if (path === this.openedWorkingDirectory) {
  288         return true;
  289       }
  290     }
  291     return false;
  292   };
  293 
  294   realpath = function(unrealPath) {
  295     var e;
  296     try {
  297       return fs.realpathSync(unrealPath);
  298     } catch (_error) {
  299       e = _error;
  300       return unrealPath;
  301     }
  302   };
  303 
  304   isRootPath = function(repositoryPath) {
  305     if (process.platform === 'win32') {
  306       return /^[a-zA-Z]+:[\\\/]$/.test(repositoryPath);
  307     } else {
  308       return repositoryPath === path.sep;
  309     }
  310   };
  311 
  312   openRepository = function(repositoryPath) {
  313     var repository, symlink, workingDirectory;
  314     symlink = realpath(repositoryPath) !== repositoryPath;
  315     if (process.platform === 'win32') {
  316       repositoryPath = repositoryPath.replace(/\\/g, '/');
  317     }
  318     repository = new Repository(repositoryPath);
  319     if (repository.exists()) {
  320       repository.caseInsensitiveFs = fs.isCaseInsensitive();
  321       if (symlink) {
  322         workingDirectory = repository.getWorkingDirectory();
  323         while (!isRootPath(repositoryPath)) {
  324           if (realpath(repositoryPath) === workingDirectory) {
  325             repository.openedWorkingDirectory = repositoryPath;
  326             break;
  327           }
  328           repositoryPath = path.resolve(repositoryPath, '..');
  329         }
  330       }
  331       return repository;
  332     } else {
  333       return null;
  334     }
  335   };
  336 
  337   openSubmodules = function(repository) {
  338     var relativePath, submodulePath, submoduleRepo, _i, _len, _ref, _results;
  339     repository.submodules = {};
  340     _ref = repository.getSubmodulePaths();
  341     _results = [];
  342     for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  343       relativePath = _ref[_i];
  344       if (!(relativePath)) {
  345         continue;
  346       }
  347       submodulePath = path.join(repository.getWorkingDirectory(), relativePath);
  348       if (submoduleRepo = openRepository(submodulePath)) {
  349         if (submoduleRepo.getPath() === repository.getPath()) {
  350           _results.push(submoduleRepo.release());
  351         } else {
  352           openSubmodules(submoduleRepo);
  353           _results.push(repository.submodules[relativePath] = submoduleRepo);
  354         }
  355       } else {
  356         _results.push(void 0);
  357       }
  358     }
  359     return _results;
  360   };
  361 
  362   exports.open = function(repositoryPath) {
  363     var repository;
  364     repository = openRepository(repositoryPath);
  365     if (repository != null) {
  366       openSubmodules(repository);
  367     }
  368     return repository;
  369   };
  370 
  371 }).call(this);