Before you start work, compare your fork with the original project. Each commit has a short hash; compare the two histories.
What to build
A program that compares the commits of upstream and a fork and says how to sync.
Requirements
- Line 1 has the commit hashes of upstream, oldest first, separated by spaces (it may be empty). Line 2 has the commits of the fork in the same way.
- Print
behind: HASHES, the upstream commits that the fork lacks, in upstream order and separated by a comma and a space, orbehind: none. - Print
ahead: HASHES, the fork commits that upstream lacks, in fork order, orahead: none. - Print
action: up to dateif both are none. If only behind has commits printaction: fast-forward. If only ahead has commits printaction: ready for a pull request. If both have commits printaction: rebase needed.
Input
Two lines of space-separated hashes.
Output
Three lines.
Constraints
Each line has at most 50 hashes of up to 10 letters or digits.
Example
Input:
a1 b2 c3 d4
a1 b2
Output:
behind: c3, d4
ahead: none
action: fast-forward
Why it works
The two differences are just two filtered lists, and the action follows from which are empty: no own commits means the branch can be moved forward, and no missing commits means there is nothing to bring in, so the changes can be proposed.
Examples
a1 b2 c3 d4 a1 b2
behind: c3, d4 ahead: none action: fast-forward
a1 b2 a1 b2 x9
behind: none ahead: x9 action: ready for a pull request
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Use the other list for membership: [h for h in upstream if h not in fork].
Hint 2
Both lines may be empty, so use split() without arguments.
Hint 3
Decide the action by which of the two lists is empty.
Solution
Show one correct solution (try it yourself first)
upstream = input().split()
fork = input().split()
behind = [h for h in upstream if h not in fork]
ahead = [h for h in fork if h not in upstream]
print("behind: " + (", ".join(behind) if behind else "none"))
print("ahead: " + (", ".join(ahead) if ahead else "none"))
if not behind and not ahead:
print("action: up to date")
elif not ahead:
print("action: fast-forward")
elif not behind:
print("action: ready for a pull request")
else:
print("action: rebase needed")
▶ Open this solution in the compiler, then press Visualize to watch it run step by step