DEV Community

Andy Yang
Andy Yang

Posted on

lab 4 - reinvent PR by remote repos.

lab 4 - reinvent PR by remote repos.

In this lab, we are going to simulate the pull request by using git remote. I first filed an issue on this repo https://github.com/MSTEWARDSON/LinkStatus. Then, I forked the repo to my own Github account https://github.com/yzwdroid/LinkStatus. I also made a new branch named issue-16 based on the master branch.

First problem

After I got the permission to work on this issue(add a new feature - ignore URL patterns).
I started to work on the program linkStatus. The first problem is go run
test.txt
didn't work on my computer. I realize there may be some issues with go module. So I asked Matthew on slack, He did some change on the upstream repo. So my forked version is behind the upstream repo. What I do is below:

// add remote repo
git add remote upstream https://github.com/MSTEWARDSON/LinkStatus.git
git fetch upstream
// rebase issue-16
git checkout issue-16
git rebase issue-16 upstream/master
Enter fullscreen mode Exit fullscreen mode

The issue-16 branch is updated.

Work on the feature.

The code structure is different from mine, although we all wrote this in go. I
read the code several times, and got a plan on how to add the feature.

  • add the flag -i or --ignore
var ignore = flag.BoolP("ignore", "i", false, "ignore url patterns")
Enter fullscreen mode Exit fullscreen mode
  • add the function to parse the ignore file ignore.txt
// extract the urls from ignore.txt
func ignoreURL(fileName string) []string {
    // urls to ignore
    var s []string
    file, err := os.Open(fileName)
    if err != nil {
        log.Fatal(err)
    }
    defer file.Close()
    scanner := bufio.NewScanner(file)
    re := regexp.MustCompile("^(#|https?://)")
    for scanner.Scan() {
        if !re.Match(scanner.Bytes()) {
            fmt.Println("Ignore file invalid")
            os.Exit(1)
        }
        if line := scanner.Text(); string(line[0]) != "#" {
            s = append(s, line)
        }
    }

    if err := scanner.Err(); err != nil {
        log.Fatal(err)
    }
    return s
}
Enter fullscreen mode Exit fullscreen mode
  • integrate the ignored slice to the url slice.
    // TODO: ignore links here.
    if ignore {
        var tp []string
        s := ignoreURL("ignore.txt")
        fmt.Println("ignored urls ", s)
        for _, link := range result {
            valid := true
            for _, url := range s {
            if strings.HasPrefix(link, url) {
                    valid = false
                    break
                }
            }
            if valid {
                tp = append(tp, link)
            }
        }
        result = tp
    }
Enter fullscreen mode Exit fullscreen mode
git commit -m "ignore feature done"
git push issue-16 origin
Enter fullscreen mode Exit fullscreen mode

How we collaborate without a pull request.

After I have done all the coding part, I told Matthew to check out my work by
adding my repo as a remote. Then Matthew noticed that I forgot to update the
readme file about the new feature. I added one more commit. Finally, Matthew
committed my issue-16 branch to the master branch.

The best way to learn something

The best way to learn coding or software like git is by building small programs
that you are using in your everyday life. This is the way I learned Go and Python.

Top comments (0)