Skip to content

Commit 35818fe

Browse files
committed
Refactored git fetch and pull APIs
1 parent 9382fcf commit 35818fe

File tree

5 files changed

+103
-35
lines changed

5 files changed

+103
-35
lines changed

git/gitFetchPullApi.js

Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,47 @@ const execPromisified = util.promisify(exec);
44

55
const fetchRepopath = require("../global/fetchGitRepoPath");
66

7-
const gitFetchApi = async (repoId) => {
8-
return await execPromisified(`git fetch`, {
7+
const getRemoteName = async (repoId, remoteUrl) => {
8+
return await execPromisified(`git remote -v`, {
9+
cwd: fetchRepopath.getRepoPath(repoId),
10+
windowsHide: true,
11+
})
12+
.then(({ stdout, stderr }) => {
13+
if (stdout && !stderr) {
14+
console.log(stdout);
15+
const localName = stdout.trim().split("\n");
16+
return localName
17+
.filter((item) => {
18+
if (item.includes(remoteUrl) && item.includes("fetch")) {
19+
return true;
20+
}
21+
})
22+
.join()
23+
.split(/\s/gi)[0];
24+
} else {
25+
console.log(stderr);
26+
return "";
27+
}
28+
})
29+
.catch((err) => {
30+
console.log(err);
31+
return "";
32+
});
33+
};
34+
35+
const gitFetchApi = async (repoId, remoteUrl, remoteBranch) => {
36+
const remoteName = await getRemoteName(repoId, remoteUrl);
37+
console.log("Selected remote name : ", remoteName);
38+
39+
if (!remoteName) {
40+
console.log("NO REMOTE MATCHING THE URL");
41+
42+
return {
43+
status: "FETCH_ERROR",
44+
};
45+
}
46+
47+
return await execPromisified(`git fetch ${remoteName} ${remoteBranch}`, {
948
cwd: fetchRepopath.getRepoPath(repoId),
1049
windowsHide: true,
1150
})
@@ -41,14 +80,25 @@ const gitFetchApi = async (repoId) => {
4180
});
4281
};
4382

44-
const gitPullApi = async (repoId) => {
45-
return await execPromisified(`git pull`, {
83+
const gitPullApi = async (repoId, remoteUrl, remoteBranch) => {
84+
const remoteName = await getRemoteName(repoId, remoteUrl);
85+
console.log("Selected remote name : ", remoteName);
86+
87+
if (!remoteName) {
88+
console.log("NO REMOTE MATCHING THE URL");
89+
90+
return {
91+
status: "PULL_ERROR",
92+
};
93+
}
94+
95+
return await execPromisified(`git pull ${remoteName} ${remoteBranch}`, {
4696
cwd: fetchRepopath.getRepoPath(repoId),
4797
windowsHide: true,
4898
})
4999
.then(async ({ stdout, stderr }) => {
50-
if (stdout && !stderr) {
51-
const pullResponse = stdout.trim().split("\n");
100+
if (stdout || stderr) {
101+
const pullResponse = stderr.trim().split("\n");
52102

53103
if (pullResponse && pullResponse.length > 0) {
54104
return {
@@ -57,19 +107,19 @@ const gitPullApi = async (repoId) => {
57107
};
58108
} else {
59109
return {
60-
status: "PULL_EMPTY",
110+
status: "PULL_ABSENT",
61111
};
62112
}
63113
} else {
64114
return {
65-
status: "PULL_FAILED",
115+
status: "PULL_ERROR",
66116
};
67117
}
68118
})
69119
.catch((err) => {
70120
console.log(err);
71121
return {
72-
status: "PULL_FAILED",
122+
status: "PULL_ERROR",
73123
};
74124
});
75125
};

git/gitRemoveStagedItems.js

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,40 @@ const gitRemoveStagedItemApi = async (repoId, item) => {
99
return await execPromisified(`git reset ${item}`, {
1010
cwd: fetchRepopath.getRepoPath(repoId),
1111
windowsHide: true,
12-
}).then(({ stdout, stderr }) => {
13-
if (stderr) {
14-
console.log(stderr);
12+
})
13+
.then(({ stdout, stderr }) => {
14+
if (stderr) {
15+
console.log(stderr);
16+
return "STAGE_REMOVE_FAILED";
17+
} else {
18+
console.log(stdout);
19+
return "STAGE_REMOVE_SUCCESS";
20+
}
21+
})
22+
.catch((err) => {
23+
console.log(err);
1524
return "STAGE_REMOVE_FAILED";
16-
} else {
17-
console.log(stdout);
18-
return "STAGE_REMOVE_SUCCESS";
19-
}
20-
});
25+
});
2126
};
2227

2328
const gitRemoveAllStagedItemApi = async (repoId) => {
2429
return await execPromisified(`git reset`, {
2530
cwd: fetchRepopath.getRepoPath(repoId),
2631
windowsHide: true,
27-
}).then(({ stdout, stderr }) => {
28-
if (stderr) {
29-
console.log(stderr);
32+
})
33+
.then(({ stdout, stderr }) => {
34+
if (stderr) {
35+
console.log(stderr);
36+
return "STAGE_ALL_REMOVE_FAILED";
37+
} else {
38+
console.log(stdout);
39+
return "STAGE_ALL_REMOVE_SUCCESS";
40+
}
41+
})
42+
.catch((err) => {
43+
console.log(err);
3044
return "STAGE_ALL_REMOVE_FAILED";
31-
} else {
32-
console.log(stdout);
33-
return "STAGE_ALL_REMOVE_SUCCESS";
34-
}
35-
});
45+
});
3646
};
3747

3848
module.exports.gitRemoveStagedItemApi = gitRemoveStagedItemApi;

global/globalAPIHandler.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,12 @@ app.use(
127127
return await gitRemoveAllStagedItems(repoId);
128128
},
129129
fetchFromRemote: async (args) => {
130-
const { repoId } = args;
131-
return await gitFetchFromRemote(repoId);
130+
const { repoId, remoteUrl, remoteBranch } = args;
131+
return await gitFetchFromRemote(repoId, remoteUrl, remoteBranch);
132132
},
133133
pullFromRemote: async (args) => {
134-
const { repoId } = args;
135-
return await gitPullFromRemote(repoId);
134+
const { repoId, remoteUrl, remoteBranch } = args;
135+
return await gitPullFromRemote(repoId, remoteUrl, remoteBranch);
136136
},
137137
deleteRepo: async (args) => {
138138
const { repoId } = args;

global/globalFunctionStore.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,12 +229,20 @@ module.exports.gitRemoveAllStagedItems = gitRemoveAllStagedItems = async (
229229
return await gitRemoveAllStagedItemApi(repoId);
230230
};
231231

232-
module.exports.gitFetchFromRemote = gitFetchFromRemote = async (repoId) => {
233-
return await gitFetchApi(repoId);
232+
module.exports.gitFetchFromRemote = gitFetchFromRemote = async (
233+
repoId,
234+
remoteUrl,
235+
remoteBranch
236+
) => {
237+
return await gitFetchApi(repoId, remoteUrl, remoteBranch);
234238
};
235239

236-
module.exports.gitPullFromRemote = gitPullFromRemote = async (repoId) => {
237-
return await gitPullApi(repoId);
240+
module.exports.gitPullFromRemote = gitPullFromRemote = async (
241+
repoId,
242+
remoteUrl,
243+
remoteBranch
244+
) => {
245+
return await gitPullApi(repoId, remoteUrl, remoteBranch);
238246
};
239247

240248
module.exports.deleteRepo = deleteRepo = async (

global/gqlGlobalAPISchema.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ const globalAPISchema = new buildSchema(
103103
removeStagedItem(repoId: String!, item: String!): String!
104104
removeAllStagedItem(repoId: String!): String!
105105
addBranch(repoId: String!, branchName: String!): String!
106-
fetchFromRemote(repoId: String!): gitFetchStatus!
107-
pullFromRemote(repoId: String!): gitPullStatus!
106+
fetchFromRemote(repoId: String!, remoteUrl: String!, remoteBranch: String!): gitFetchStatus!
107+
pullFromRemote(repoId: String!, remoteUrl: String!, remoteBranch: String!): gitPullStatus!
108108
updateRepoDataFile(newDbFile: String!): String!
109109
deleteRepo(repoId: String!): deleteRepoStatus!
110110
addRemoteRepo(repoId: String!, remoteName: String!, remoteUrl: String!): String!

0 commit comments

Comments
 (0)