Skip to content

Commit

Permalink
Merge pull request #1142 from juanmv94/main
Browse files Browse the repository at this point in the history
Implemented --force for fetch/pull. Force using +src:dest
  • Loading branch information
pcottle committed Apr 28, 2024
2 parents b10a6f8 + d1a0832 commit 2749d9c
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 18 deletions.
26 changes: 24 additions & 2 deletions src/js/git/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ var commandConfig = {
pull: {
regex: /^git +pull($|\s)/,
options: [
'--force',
'--rebase'
],
execute: function(engine, command) {
Expand All @@ -237,6 +238,7 @@ var commandConfig = {
}

var commandOptions = command.getOptionsMap();
var force = !!commandOptions['--force'];
var generalArgs = command.getGeneralArgs();
if (commandOptions['--rebase']) {
generalArgs = commandOptions['--rebase'].concat(generalArgs);
Expand All @@ -261,6 +263,10 @@ var commandConfig = {
var firstArg = generalArgs[1];
// COPY PASTA validation code from fetch. maybe fix this?
if (firstArg && isColonRefspec(firstArg)) {
if (firstArg[0] == '+') {
force = true;
firstArg = firstArg.substr(1);
}
var refspecParts = firstArg.split(':');
source = refspecParts[0];
assertRefNoModifiers(source);
Expand Down Expand Up @@ -291,6 +297,7 @@ var commandConfig = {
engine.pull({
source: source,
destination: destination,
force: force,
isRebase: !!commandOptions['--rebase']
});
}
Expand Down Expand Up @@ -382,6 +389,9 @@ var commandConfig = {

fetch: {
regex: /^git +fetch($|\s)/,
options: [
'--force',
],
execute: function(engine, command) {
if (!engine.hasOrigin()) {
throw new GitError({
Expand All @@ -391,12 +401,18 @@ var commandConfig = {

var source;
var destination;
var commandOptions = command.getOptionsMap();
var force = !!commandOptions['--force'];
var generalArgs = command.getGeneralArgs();
command.twoArgsForOrigin(generalArgs);
assertOriginSpecified(generalArgs);

var firstArg = generalArgs[1];
if (firstArg && isColonRefspec(firstArg)) {
if (firstArg[0] == '+') {
force = true;
firstArg = firstArg.substr(1);
}
var refspecParts = firstArg.split(':');
source = refspecParts[0];
assertRefNoModifiers(source);
Expand All @@ -420,7 +436,8 @@ var commandConfig = {

engine.fetch({
source: source,
destination: destination
destination: destination,
force: force
});
}
},
Expand Down Expand Up @@ -808,6 +825,7 @@ var commandConfig = {
var source;
var sourceObj;
var commandOptions = command.getOptionsMap();
var force = !!commandOptions['--force'];
var isDelete = commandOptions['-d'] || commandOptions['--delete'];

// git push is pretty complex in terms of
Expand Down Expand Up @@ -849,6 +867,10 @@ var commandConfig = {
}

if (firstArg && isColonRefspec(firstArg)) {
if (firstArg[0] == '+') {
force = true;
firstArg = firstArg.substr(1);
}
var refspecParts = firstArg.split(':');
source = refspecParts[0];
destination = validateBranchName(engine, refspecParts[1]);
Expand Down Expand Up @@ -893,7 +915,7 @@ var commandConfig = {
// are always, always strings. very important :D
destination: destination,
source: source,
force: !!commandOptions['--force']
force: force
});
}
},
Expand Down
42 changes: 26 additions & 16 deletions src/js/git/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,12 +393,12 @@ GitEngine.prototype.makeRemoteBranchIfNeeded = function(branchName) {
return this.makeRemoteBranchForRemote(branchName);
};

GitEngine.prototype.makeBranchIfNeeded = function(branchName) {
GitEngine.prototype.makeBranchIfNeeded = function(branchName, originName) {
if (this.doesRefExist(branchName)) {
return;
}

return this.validateAndMakeBranch(branchName, this.rootCommit);
var originTarget = this.findCommonAncestorWithRemote(this.origin.getCommitFromRef(originName).get('id'));
return this.validateAndMakeBranch(branchName, this.getCommitFromRef(originTarget));
};

GitEngine.prototype.makeRemoteBranchForRemote = function(branchName) {
Expand Down Expand Up @@ -1240,7 +1240,7 @@ GitEngine.prototype.fetch = function(options) {
return;
} else if (options.source) {
var sourceDestPairs = [];
didMakeBranch = didMakeBranch || this.makeRemoteBranchIfNeeded(options.source);
didMakeBranch = this.makeRemoteBranchIfNeeded(options.source);
var source = this.origin.resolveID(options.source);
if (source.get('type') == 'branch') {
sourceDestPairs.push({
Expand All @@ -1249,7 +1249,7 @@ GitEngine.prototype.fetch = function(options) {
});
}
if (options.destination) {
didMakeBranch = didMakeBranch || this.makeBranchIfNeeded(options.destination);
didMakeBranch = this.makeBranchIfNeeded(options.destination, options.source) || didMakeBranch;
sourceDestPairs.push({
destination: options.destination,
source: options.source
Expand Down Expand Up @@ -1278,14 +1278,16 @@ GitEngine.prototype.fetchCore = function(sourceDestPairs, options) {
// first check if our local remote branch is upstream of the origin branch set.
// this check essentially pretends the local remote branch is in origin and
// could be fast forwarded (basic sanity check)
sourceDestPairs.forEach(function (pair) {
this.checkUpstreamOfSource(
this,
this.origin,
pair.destination,
pair.source
);
}, this);
if (!options.force) {
sourceDestPairs.forEach(function (pair) {
this.checkUpstreamOfSource(
this,
this.origin,
pair.destination,
pair.source
);
}, this);
}

// then we get the difference in commits between these two graphs
var commitsToMake = [];
Expand All @@ -1304,9 +1306,16 @@ GitEngine.prototype.fetchCore = function(sourceDestPairs, options) {
}, this);

if (!commitsToMake.length && !options.dontThrowOnNoFetch) {
throw new GitError({
msg: intl.str('git-error-origin-fetch-uptodate')
});
var ge = this;
if (!options.force || !sourceDestPairs.some(function(pair) {
var sourceCommit = ge.getCommitFromRef(ge.origin.resolveID(pair.source));
var destinationCommit = ge.getCommitFromRef(ge.resolveID(pair.destination));
return sourceCommit.id !== destinationCommit.id;
})) {
throw new GitError({
msg: intl.str('git-error-origin-fetch-uptodate')
});
}
}

// we did this for each remote branch, but we still need to reduce to unique
Expand Down Expand Up @@ -1406,6 +1415,7 @@ GitEngine.prototype.pull = function(options) {
var pendingFetch = this.fetch({
dontResolvePromise: true,
dontThrowOnNoFetch: true,
force: options.force,
source: options.source,
destination: options.destination
});
Expand Down

0 comments on commit 2749d9c

Please sign in to comment.